モジュール:Jctint/USA

local p = {}
local format = mw.ustring.format
local concat = table.concat
local insert = table.insert
local roadDataModule = require("モジュール:Road data")
-- TODO transition
-- begin transition code
local subConfig = {
	county = {
		group = "county"
	},
	township = {
		sub2area = "township",
		group = "township",
		single = true,
		firstnonum = true
	}
}
local function sub_special(args, subTypeConfig, specialPrefix, subType)
	-- Find parameter.
	local subParam
	for _,p in ipairs(subTypeConfig) do
		if argsp .. 1 or argsp then
			subParam = p
			break
		end
	end
	if not subParam then
		return argsspecialPrefix .. "_special"
	end
	local config = subConfigsubParam or {}
	local subs = {}
	-- Retrieve wikilinks for subdivisions.
	local num = 1
	while num == 1 or argssubParam .. num do
		-- Save parameters.
		local saved = {}
		savedsubType= argssubType
		argssubType = argssubParam .. num or num == 1 and argssubParam
		saved.sub1dab = args.sub1dab
		args.sub1dab = args"ctdab" .. num or args.ctdab or num == 1 and config.firstnonum and args.county
		saved.area = args.area
		args.area = not (config.nosub1dab and args.sub1dab)
			and (config.sub2area or args"area" .. num or num == 1 and config.firstnonum and args.area)
		local locns = roadDataModule.locations(args, "jctint", true)
		-- Restore parameters.
		argssubType = savedsubType
		args.sub1dab = saved.sub1dab
		args.area = saved.area
		subsnum = locnssubType
		num = num + 1
	end
	if #subs > 1 then
		-- Construct wikitext for multiple subs.
		local textParts = {}
		insert(textParts, subs1)
		for i = 2, #subs do
			insert(textParts, "–")
			if i % 2 ~= 0 then
				-- Odd subs after first begin a new line.
				insert(textParts, "")
			end
			insert(textParts, subsi)
		end
		local groupSuffix = config.group
		if groupSuffix then
			insert(textParts,
				format("%s%s", #subs % 2 == 0 and "" or " ", groupSuffix))
		end
		if #subs == 2 then
			insert(textParts, " line")
		elseif #subs == 3 then
			insert(textParts, " tripoint")
		elseif #subs == 4 then
			insert(textParts, " quadripoint")
		else
			insert(textParts, " multipoint")
		end
		return concat(textParts)
	elseif #subs == 1 and config.single then
		-- TODO transition
		-- Save parameters.
		local saved = {}
		savedsubType= argssubType
		argssubType = argssubParam .. 1 or argssubParam
		saved.sub1dab = args.sub1dab
		args.sub1dab = args.ctdab1 or args.ctdab or config.firstnonum and args.county
		saved.area = args.area
		args.area = not (config.nosub1dab and args.sub1dab)
			and (config.sub2area or args.area1 or config.firstnonum and args.area)
		local locns = roadDataModule.locations(args, "jctint")
		-- Restore parameters.
		argssubType = savedsubType
		args.sub1dab = saved.sub1dab
		args.area = saved.area
		return locnssubType
	end
end
-- end transition code
function p._jctint(args)
	-- Extra parameters
	args.country = "USA"
	-- TODO transition
	-- begin transition code
	args.primary_topic = "no"
	args.sub1name = args.sub1name or "County"
	args.region = mw.loadData("モジュール:Jct/statename")args.state
	args.sub1_special = sub_special(args, {"county"}, "county", "sub1")
	local sub2Config = {}
	if args.sub2param then
		insert(sub2Config, args.sub2param)
	end
	insert(sub2Config, "location")
	args.sub2_special = sub_special(args, sub2Config, "location", "sub2")
	-- end transition code
	args.unitdef = "mi"
	-- Tracked parameters
	local msgs = {}
	local trackedParams = {R = "length_ref", Y = "county_special", Z = "location_special"}
	for key,param  in pairs(trackedParams) do
		if argsparam then
			insert(msgs, format("", key))
		end
	end
	if args.ctdab2 and not (args.ctdab or args.ctdab1) then
		insert(msgs, format("", "D"))
	end
	if args.township2 and args.township then
		insert(msgs, format("", "T"))
	end
	local msg
	if #msgs > 0 then
		local page = mw.title.getCurrentTitle().prefixedText -- Get transcluding page's title
		msg = mw.ustring.gsub(concat(msgs), "%%page%%", page)
	end
	-- Parameters to be renamed
	local paramSubst = {
		region_special = "state_special",
		regionspan = "sspan",
		sub1 = args.sub1param or "county",
		sub1_note = "county_note",
		sub1span = "cspan",
		sub1dab = "ctdab",
		sub2 = "location",
		sub2span = "lspan",
		unit = "mile",
		unit2 = "mile2",
		unit_ref = {"mile_ref", --TODO transition "length_ref"},
		uspan = "mspan"
	}
	-- Rename parameters.
	for tgt,src in pairs(paramSubst) do
		if type(src) == "table" then
			for _,param in ipairs(src) do
				argstgt = argstgt or argsparam
				argsparam = nil
			end
		else
			argstgt = argstgt or argssrc
			argssrc = nil
		end
	end
	-- Parameters to be removed
	local nilParams = {
		"county_special", "location_special"
	}
	-- Remove parameters.
	for _,param  in ipairs(nilParams) do
		argsparam = nil
	end
	-- Parameter groups to be removed
	local nilParamPrefixes = {
		"county", "location", "township"
	}
	-- Remove parameters.
	for _,paramPrefix  in ipairs(nilParamPrefixes) do
		argsparamPrefix = nil
		local i = 1
		while i == 1 or argsparamPrefix .. i do
			argsparamPrefix .. i = nil
			i = i + 1
		end
	end
	local coreModule = require("モジュール:Jctint/core")
	return coreModule._jctint(args) .. (msg or "")
end
function p.jctint(frame)
	-- Import module function to work with passed arguments
	local getArgs = require('モジュール:Arguments').getArgs
	local args = getArgs(frame)
	return p._jctint(args)
end
return p