local p = {}

-- Change to "" upon deployment.
local moduleSuffix = ""

local parserModuleName = "Module:Road data/parser" .. moduleSuffix
local cityModuleName = "Module:Jct/city" .. moduleSuffix

local concat = table.concat
local insert = table.insert
local format = mw.ustring.format
local trim = mw.text.trim

local parserModule = require(parserModuleName)
local parser = parserModule.parser
local util = require("Module:Road data/util")
local displayModule = require("Module:Sandbox/BrandonXLF/4")

-- Links/abbreviations
local function routeText(route, frame)
	local link
	local type = route.type
	if not type or type == '' then
		link = route.route
	else
		link = displayModule.link(route)
	end

	local dir = route.dir and ' ' .. string.lower(route.dir) or ''
	local routeText = link .. dir

	local name = route.name
	if name and name ~= '' then
		local mainText = routeText
		local parenText = name

	return frame:expandTemplate{ title = 'BSsplit', args = {mainText, parenText, align = 'left', valign = 'middle' } }
	else
		return routeText
	end
end

local function parseArgs(args)
	local state = args.state or args.province or ''
	args.state = state
	local country
	if args.country and args.country ~= '' then
		country = string.upper(args.country)
	else
		local countryModule = mw.loadData("Module:Road data/countrymask")
		country = countryModule[state] or 'UNK'
	end
	args.country = country

	local params = {'denom', 'county', 'township', 'dab', 'nolink', 'noshield', 'to', 'dir', 'name'}
	local routes = {}
	local routeCount = 1
	local seenTo = false
	while true do
		local routeType = args[routeCount * 2 - 1]
		if not routeType then break end
		local route = {type = routeType, route = args[routeCount * 2]}
		for _,v in pairs(params) do
			route[v] = args[v .. routeCount]
		end
		route.country = country
		route.state = state

		-- Set the first .to to true.
		-- Set all following .to to ''.
		if seenTo then
			if route.to then
				-- Report duplicate to flag.
				route.toerror = true
			end
			route.to = ''
		elseif route.to then
			route.to = true
			seenTo = true
		end

		route.rdt = args.rdt
		insert(routes, route)
		routeCount = routeCount + 1
	end
	return routes
end

local function prefix(to, num)
	if to and to ~= '' then
		return num == 1 and 'To ' or ' to '
	end
	return num == 1 and '' or ' / '
end

function p._jctrdt(args, frame)
	local routes = parseArgs(args)
	local shields = {}
	local links = {}
	frame = frame or mw.getCurrentFrame()
	for num,route in ipairs(routes) do
		if not (args.noshield or route.noshield) then
			local shield = displayModule.shield(route, nil, 'rdt')
			insert(shields, shield)
		end
		local prefix = prefix(route.to, num)
		if prefix ~= '' then insert(links, prefix) end
		insert(links, routeText(route, frame))
	end
	local graphics = concat(shields) .. ' '
	local linkText = concat(links)
	local cities = ''
	if args.city1 or args.location1 then
		local citiesPrefix
		if args.citiesprefix then
			citiesPrefix = args.citiesprefix ~= '' and format(" %s ", args.citiesprefix) or ''
		else
			citiesPrefix = ' '
		end
		local cityModule = require(cityModuleName)
		cities = citiesPrefix .. cityModule.city(args)
	end

	return graphics .. linkText .. cities
end

function p.jctrdt(frame)
	-- Import module function to work with passed arguments
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame, {removeBlanks = false})
	return p._jctrdt(args, frame)
end

return p