模組:PlatformMap/Shared

---@module "PlatformMapStrings"
local strs = require('Module:PlatformMap/Strings')
local emsp14 = mw.ustring.char(0x2005)

local p = {
	icon = {
		map = {
			['uSTRq'] = {
				open = { on_left = 'uSTRq', on_right = 'uSTRq' },
				soft = { on_left = 'uENDExaq', on_right = 'uENDExeq' },
				hard = { on_left = 'uENDEaq', on_right = 'uENDEeq' }
			},
			['uSTRq+c1'] = {
				open = { on_left = 'uSTRq+c1', on_right = 'uSTRq+c1' },
				soft = { on_left = 'uENDExaq!~uSTRc1', on_right = 'uENDExeq!~uSTRc1' },
				hard = { on_left = 'uENDEaq!~uSTRc1', on_right = 'uENDEeq!~uSTRc1' }
			},
			['uSTRq+c2'] = {
				open = { on_left = 'uSTRq+c2', on_right = 'uSTRq+c2' },
				soft = { on_left = 'uENDExaq!~uSTRc2', on_right = 'uENDExeq!~uSTRc2' },
				hard = { on_left = 'uENDEaq!~uSTRc2', on_right = 'uENDEeq!~uSTRc2' }
			},
			['uSTRq+c3'] = {
				open = { on_left = 'uSTRq+c3', on_right = 'uSTRq+c3' },
				soft = { on_left = 'uENDExaq!~uSTRc3', on_right = 'uENDExeq!~uSTRc3' },
				hard = { on_left = 'uENDEaq!~uSTRc3', on_right = 'uENDEeq!~uSTRc3' }
			},
			['uSTRq+c4'] = {
				open = { on_left = 'uSTRq+c4', on_right = 'uSTRq+c4' },
				soft = { on_left = 'uENDExaq!~uSTRc4', on_right = 'uENDExeq!~uSTRc4' },
				hard = { on_left = 'uENDEaq!~uSTRc4', on_right = 'uENDEeq!~uSTRc4' }
			},
			['uCONTgq'] = { left = 'uCONTg@Gq', right = 'uCONTfaq' },
			['uCONTfq'] = { left = 'uCONTgeq', right = 'uCONTf@Fq' },
			['uexCONTgq'] = { left = 'uexCONTg@Gq', right = 'uexCONTfaq' },
			['uexCONTfq'] = { left = 'uexCONTgeq', right = 'uexCONTf@Fq' },
		}
	},
	matrix = {},
	text = {}
}

---@param target string[][]
---@param m string[][]
function p.matrix.append(target, m)
	for i = 1, #target do
		for j = 1, #m[i] do
			table.insert(target[i], m[i][j])
		end
	end
end

---@param target string[][]
---@param m string[][]
function p.matrix.prepend(target, m)
	for i = 1, #target do
		for j = #m[i], 1, -1 do
			table.insert(target[i], 1, m[i][j])
		end
	end
end

---@param args { system: string, line: string, type: string?, left: boolean, inverse: boolean }
---@return string|string[]
local function get_terminus(args)
	local data = require('Module:Adjacent stations/' .. args.system)
	local terminus = (args.left ~= args.inverse) and 'left terminus' or 'right terminus'
	if args.type and args.type ~= '' then
		return data.lines[args.line].types[args.type][terminus]
	else
		return data.lines[args.line][terminus]
	end
end

---@param args { system: string, line: string, type: string?, next: string, left: boolean, inverse: boolean }
---@return string
function p.text.adjacent(args)
	if args.line == '' or args.next == '' then
		return ''
	end

	local data = require('Module:Adjacent stations/' .. args.system)
	local loop
	if args.type and args.type ~= '' then
		loop = data.lines[args.line].types[args.type]['circular'] or false
	else
		loop = data.lines[args.line]['circular'] or false
	end

	local nxt = mw.ustring.format('{{stl|%s|%s}}', args.system, args.next)
	local terminus = get_terminus(args)

	if loop then
		if type(terminus) == 'table' then
			terminus = table.concat(terminus, emsp14)
		else
			terminus = terminus
		end
	else
		if type(terminus) == 'table' then
			for i, _ in ipairs(terminus) do terminus[i] = mw.ustring.format('{{stl|%s|%s}}', args.system, terminus[i]) end
			terminus = table.concat(terminus, emsp14)
		else
			terminus = mw.ustring.format('{{stl|%s|%s}}', args.system, terminus)
		end
		if nxt == terminus then terminus = strs.terminus else terminus = mw.ustring.format(strs.towards, terminus) end
	end

	return mw.ustring.format(
		'{{PlatformMap/Adjacent|%s|{{rcb|%s|%s|route|type=%s}}|%s|%s}}',
		args.left and 'left' or 'right',
		args.system,
		args.line,
		args.type or '',
		nxt,
		terminus
	)
end

return p