local p = {}
local getArgs = require('Module:Arguments').getArgs
local util = require("Module:Road data/util")
local yesno = require('Module:Yesno')
local langModule = require('Module:Lang')
local routeModule = require("Module:Infobox road/route")
local parserModule = require("Module:Road data/parser")
local parser = parserModule.parser
local format = mw.ustring.format
local = mw.getCurrentFrame  
local function country(args)
local state = args.state or args.province
local country
local countryModule = mw.loadData("Module:Road data/countrymask")
local country = args.country or countryModule[state]  
return country
end  
-- HEADER COLORS  
function p.headerStyle
local args = getArgs
local header
if args.header_type == nil then
header = args.header_type
else
header = string.lower(args.header_type)
end  
local deleted = args.decommissioned or args.deleted or header == "former"
local uc = header == "under construction" or header == "const" or header == "uc"
local minor = header == "minor"
local hist = header == "hist" or header == "historic" or header == "historical" or header == "scenic"
local color = parser(args, 'color') or args['header_color']
local freeway = header == "freeway"  
if freeway then
return "header-MUTCDblue"
elseif uc then
return "header-uc"
elseif minor then
return "header-minor"
elseif deleted and header == 'hist' or deleted and color == 'hist' then
return "header-deleted-hist"
elseif deleted then
return "header-deleted"
elseif hist then
return "header-hist"
elseif color then
return "header-" .. color
elseif args.state or args.province or args.country then
local country = country(args)
return "header-" .. country or "header-default"
else
return "header-default"
end
end  
-- SECOND IMAGE  
local function photo(args)
local country = country(args)
local photo = args.photo or args.image
local width = args.photo_wide or args.image_width or ""
local alt = args.photo_alt or args.image_alt or photo  
if photo == nil then
return nil
elseif country == "USA" then
return nil
else return string.format('', photo, width, alt)
end
end  
function p.photo
local args = getArgs
return photo(args)
end  
-- TRANSLATION  
local function translate(args, lang)
local lang = parser(args, 'lang') or args.lang or ''
local translation = args.translation or parser(args, 'translation') or ''  
if country(args) == "PAK" and translation ~= '' then
local route = args.route  
if route ~= nil then
local arabicModule = require( "Module:Convert to eastern arabic numerals" )
local arabic = arabicModule._convert({route})
local translated = string.gsub( translation, route, arabic)  
return :expandTemplate{title = 'Nastaliq', args = {translated}}
else
return :expandTemplate{title = 'Nastaliq', args = {translation}}
end
elseif translation ~= * and lang == * then
return translation
elseif country(args) == "CHN" and args.type == "Expwy" then
local trans = require( "Module:Road data/masks/CHN/Expwy translations" )
local route = args.route  
return langModule._lang({lang, trans[route]})
elseif country(args) == "HUN" then
local nominal = require( "Module:Road data/masks/HUN" )
local routeNum
local leading  
if string.len(args.route) > 2 then
routeNum = string.match(args.route, "%d%d$", 0)
leading = string.match(args.route, "(%d*)%d%d$", 0)
else
routeNum = args.route
leading = ''
end  
if args.type == "M" then
return langModule._lang({"hu", "M" .. leading .. nominal[routeNum] .. " autópálya"})
elseif args.type == "Mb" then
return langModule._lang({"hu", "M" .. leading .. nominal[routeNum] .. " autóút"})
elseif args.type == "MR" then
return langModule._lang({"hu", leading .. nominal[routeNum] .. " főút"})
elseif args.type == "Mb" then
return langModule._lang({"hu", leading .. nominal[routeNum] .. " közút"})
end
elseif lang == "ar" and translation ~= '' then
local route = args.route
local arabicModule = require( "Module:Convert to eastern arabic numerals" )
local arabic = arabicModule._convert({route})
local translated = string.gsub( translation, route, arabic)  
return langModule._lang({lang, translated})
elseif translation ~= * and lang ~= * then
return langModule._lang({lang, translation})
else
return nil
end
end  
function p.translate
local pframe = :getParent
local config = .args -- the arguments passed BY the template, in the wikitext of the template itself
local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template  
local lang = config.lang
return translate(args, lang)
end  
-- MAINTAINED BY  
local function maint(args)
local maint = args.maint or parser(args, 'maint') or ''
if maint == "" or maint == '' then
return nil
else
return "Maintained by " .. maint
end
end  
function p.maint
local args = getArgs
return maint(args)
end  
-- DEFINED BY LAW  
local function law(args)
local law = parser(args, 'law') or ''
if args.section == '' then
return nil
else
return "Defined by " .. law
end
end  
function p.law
local args = getArgs
return law(args)
end  
-- EXISTED  
function p._existed(args)
local formed = args.established or args.formed or ''
local deleted = args.decommissioned or args.deleted or ''  
if formed == '' then
return nil
elseif deleted == '' then
return formed .. "–present"
else
return formed .. "–" .. deleted
end
end  
function p.existed
local args = getArgs
return p._existed(args)
end  
-- TIME PERIOD  
local function period(args)  
local infobox_args = {}  
infobox_args['bodystyle'] = 'display:inline-table;'
infobox_args['child'] = 'yes'
infobox_args['decat'] = 'yes'
infobox_args['label1'] = "Time period"
infobox_args['data1'] = args.time_period  
if args.time_period then
infobox_args['label2'] = "Cultural significance"
infobox_args['data2'] = args.significance
infobox_args['label3'] = "Known for"
infobox_args['data3'] = args.known_for
infobox_args['label4'] = "Related routes"
infobox_args['data4'] = args.related
else
infobox_args['label3'] = "Known for"
infobox_args['data3'] = args.known_for
end  
return :expandTemplate ({title='Infobox', args = infobox_args})
end  
local function period_params(args)  
if args.time_period or args.known_for then
return true
else
return false
end
end  
function p.period
local args = getArgs  
if period_params(args) == false then
return nil
else
return period(args)
end
end  
-- ALLOCATION  
local function allocation(args)
local country = country(args)  
if country == "AUS" then
return "Allocation"
else return "Component
highways"
end
end  
function p.allocation
local args = getArgs
return allocation(args)
end  
-- SPUR OF  
local function spurOf(args)
local state = args.state or args.province
local country = args.country
local county = args.county
local parentType = args.spur_type
local parentRoute = args.spur_of
local aux = parser(args, 'aux') or "Auxiliary route" or nil
local jct = :expandTemplate{
title = 'jct', args = {
state = state,
country = country,
county1 = county,
parentType,
parentRoute,
noshield1 = "yes"
}
}  
if not parentType and not parentRoute then
return nil
elseif type(aux) == "table" then
return "Auxiliary route of " .. jct
else return tostring(aux) .. " of " .. jct
end
end  
function p.spurOf
local args = getArgs
return spurOf(args)
end  
-- PART OF  
local function partOf(args)  
if args["e-road"] or args.ahn or args.tahn or args.mrn then  
local infobox_args = {}  
infobox_args['bodystyle'] = 'display:inline-table;'
infobox_args['child'] = 'yes'
infobox_args['decat'] = 'yes'  
if args["e-road"] then
local eshield = args["e-road-shield"] or ''
infobox_args['data1'] = eshield .. " Part of " .. args["e-road"]
end
if args.ahn then
local ashield = args["ahn-shield"] or ''
infobox_args['data2'] = ashield .. " Part of " .. args.ahn
end
if args.tahn then
local tshield = args["tahn-shield"] or ''
infobox_args['data3'] = tshield .. " Part of " .. args.tahn
end
if args.mrn then
local mshield = args["mrn-shield"] or ''
infobox_args['data4'] = mshield .. " Part of " .. args.mrn
end  
return :expandTemplate ({title='Infobox', args = infobox_args})  
else return nil
end