local p = {}

local format = mw.ustring.format

local typesModule = require "Module:Infobox road/abbrev/defs" Country = typesModule.Country Type = typesModule.Type Abbr = typesModule.Abbr

local abbrevs = {} do

   function abbrevs:abbr(args)
       local countryArg = args.country
       local success, country = pcall(self.country, self, countryArg)
       if not(success) then
           return 
       else
           local success, abbr = pcall(country.abbr, country, args)
           if success then
               return abbr
           elseif abbr == 'Invalid type' then
               local page = mw.title.getCurrentTitle()
               local pagename = page.prefixedText
               local ns = page.namespace
               return (ns == 0) and format("", pagename) or error(abbr, 0)
           else
               error(abbr, 0)
           end
       end
   end
   function abbrevs:country(name)
       local abbr = self[name]
       if abbr then return abbr end
       local module = require(format("Module:Infobox road/abbrev/%s", name))
       return module.abbrs
   end
   
   abbrevs.AUT = Country:newWithSimpleAbbrs{["A"] = "A%s", ["S"] = "S%s"} -- Austria
   abbrevs.BGD = Country:newWithSimpleAbbrs{["N"] = "N%s", ["R"] = "R%s", ["Z"] = "Z%s"} -- Bangladesh
   abbrevs.CHE = Country:newWithSimpleAbbrs{["A"] = "A%s"} -- Switzerland
   abbrevs.CHN = Country:newWithSimpleAbbrs{[{"G", "Ex", "Expwy"}] = "G%s", ["S"] = "S%s", ["X"] = "X%s"} -- China
   abbrevs.CYP = Country:newWithSimpleAbbrs{["A"] = "A%s", ["B"] = "B%s"} -- Cyprus
   abbrevs.CZE = Country:newWithSimpleAbbrs{["D"] = "D%s", ["R"] = "R%s"} -- Czech Republic
   abbrevs.DEU = Country:newWithSimpleAbbrs{["A"] = "A %s", ["B"] = "B %s"} -- Germany
   abbrevs.ESP = Country:newWithSimpleAbbrs{["A"] = "A-%s", ["E"] = "E-%s", ["M"] = "M-%s", ["R"] = "R-%s"} -- Spain
   abbrevs.EUR = Country:newWithSimpleAbbrs{["E"] = "E%s"} -- Europe
   abbrevs.GBR = Country:newWithSimpleAbbrs{["A"] = "A%s", ["M"] = "M%s"} -- United Kingdom
   abbrevs.GEO = Country:newWithSimpleAbbrs{["A"] = "A %s", ["B"] = "B %s"} -- Georgia
   abbrevs.GRC = Country:newWithSimpleAbbrs{["A"] = "A%s", ["EO"] = "EO%s"} -- Greece
   abbrevs.HKG = Country:newWithSimpleAbbrs{[{"Route", "route"}] = "Route %s"} -- Hong Kong
   abbrevs.HRV = Country:newWithSimpleAbbrs{["A"] = "A%s", ["D"] = "D%s"} -- Croatia
   abbrevs.HUN = Country:newWithSimpleAbbrs{["M"] = "M%s"} -- Hungary
   abbrevs.IDN = Country:newWithSimpleAbbrs{[{"N", "National", "National Highway"}] = "National Route %s"} -- Indonesia
   abbrevs.IND = Country:newWithSimpleAbbrs{["NE"] = "NE %s", ["NH"] = "NH %s", ["SH"] = "SH %s"} -- India
   abbrevs.ITA = Country:newWithSimpleAbbrs{["A"] = "A%s"} -- Italy
   abbrevs.JPN = Country:newWithSimpleAbbrs{[{"National", "Route"}] = "Route %s"} -- Japan
   abbrevs.KOR = Country:newWithSimpleAbbrs{[{"National", "NR"}] = "NR %s"} -- South Korea
   abbrevs.LKA = Country:newWithSimpleAbbrs{["E"] = "E%s", ["A"] = "A%s", ["B"] = "B%s"} -- Sri Lanka
   abbrevs.LVA = Country:newWithSimpleAbbrs{["A"] = "A%s"} -- Latvia
   abbrevs.MEX = Country:newWithSimpleAbbrs{[{"Federal Highway", "Hwy", "FH"}] = "Fed. %s", ["SH"] = "SH %s"} -- Mexico
   abbrevs.NLD = Country:newWithSimpleAbbrs{["A"] = "A%s", ["N"] = "N%s"} -- Netherlands
   abbrevs.NOR = Country:newWithSimpleAbbrs{["National"] = "NR %s", [{"Fylkesvei", "fylkesvei", "Fv", "fv"}] = "Fv %s"} -- Norway
   abbrevs.NZL = Country:newWithSimpleAbbrs{["SH"] = "SH %s"} -- New Zealand
   abbrevs.POL = Country:newWithSimpleAbbrs{["A"] = "A%s", ["S"] = "S%s", ["DK"] = "DK %s", ["DW"] = "DW %s"} -- Poland
   abbrevs.ROU = Country:newWithSimpleAbbrs{["A"] = "A%s", ["DN"] = "DN%s"} -- Romania
   abbrevs.RUS = Country:newWithSimpleAbbrs{["M"] = "M%s"} -- Russia
   abbrevs.SVK = Country:newWithSimpleAbbrs{["D"] = "D%s", ["R"] = "R%s"} -- Slovakia
   abbrevs.SVN = Country:newWithSimpleAbbrs{["A"] = "A%s", ["H"] = "H%s"} -- Slovenia
   abbrevs.TUR = Country:newWithSimpleAbbrs{[{"otoyol", "O"}] = "O-%s", [{"state", "D"}] = "D.%s"} -- Turkey
   abbrevs.TWN = Country:newWithSimpleAbbrs{[{"NH", "Fwy"}] = "Fwy. %s", ["PH"] = "PH %s"} -- Taiwan
   abbrevs.UKR = Country:newWithSimpleAbbrs{["M"] = "Highway M%s", ["H"] = "Highway H%s"} -- Ukraine
   abbrevs.URY = Country:newWithSimpleAbbrs{[{"Route", "route"}] = "Route %s"} -- Uruguay
   abbrevs.ZAF = Country:newWithSimpleAbbrs{["N"] = "N%s", ["R"] = "R%s"} -- South Africa

end

function p._abbr(args)

   return abbrevs:abbr(args)

end

function p.abbr(frame)

   local pframe = frame:getParent()
   local config = frame.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 function emptyParam(param)
       local empty = {[] = true, ['¬'] = true}
       if empty[param] then
           return nil
       else
           return param
       end
   end
   
   local country = args.country or config.country
   local type = args.type or config.type
   local state = args.state or config.state
   state = emptyParam(state)
   local province = args.province or config.province
   province = emptyParam(province)
   local route = args.route or config.route
   local county = args.county or config.county
   local dab = args.dab or config.dab
   
   return p._abbr{country=country, type=type, state=state, province=province, route=route, county=county, dab=dab}

end

return p