local p = {}

function p.monument(frame)

 local pArgs = frame:getParent().args
 local lang = pArgs['lang'] or 'kaa' 
 local country = pArgs['country']
 local region = pArgs['region'] or 
 local llanguage = 'kaa'
 local campaign = 'wlm-uz'
 local noupload = 'yes'
 local dcat = 'Cultural heritage monuments in Uzbekistan'
 
 local rcat = {
 	["Q487384"] = "Cultural heritage monuments in Andijan Region",

["Q487656"] = "Cultural heritage monuments in Andijan", ["Q487372"] = "Cultural heritage monuments in Bukhara Region", ["Q5764"] = "Cultural heritage monuments in Bukhara", ["Q1658752"] = "Cultural heritage monuments in Kagan", ["Q487089"] = "Cultural heritage monuments in Fergana Region", ["Q489890"] = "Cultural heritage monuments in Kokand", ["Q488811"] = "Cultural heritage monuments in Jizzax Region", ["Q474876"] = "Cultural heritage monuments in Namangan Region", ["Q492552"] = "Cultural heritage monuments in Namangan", ["Q487570"] = "Cultural heritage monuments in Navoiy Region", ["Q487577"] = "Cultural heritage monuments in Qashqadaryo Region", ["Q569766"] = "Cultural heritage monuments in Shahrisabz", ["Q484245"] = "Cultural heritage monuments in Karakalpakstan", ["Q487532"] = "Cultural heritage monuments in Samarqand Region", ["Q5753"] = "Cultural heritage monuments in Samarqand", ["Q487547"] = "Cultural heritage monuments in Sirdaryo Region", ["Q487537"] = "Cultural heritage monuments in Surxondaryo Region", ["Q491879"] = "Cultural heritage monuments in Termez", ["Q269"] = "Cultural heritage monuments in Tashkent", ["Q487585"] = "Cultural heritage monuments in Tashkent Region", ["Q487561"] = "Cultural heritage monuments in Xorazm Region", ["Q486195"] = "Cultural heritage monuments in Xiva", [""] = "Cultural heritage monuments in Uzbekistan"

 }
 

local title = [=[

]=]

 local regtitle = {
 	["kaa-án"] = [=[Ándijan wálayatı:
 	["kaa-bu"] = [=[

Buxara wálayatı: [

 	["kaa-ji"] = [=[Jizzaq wálayatı :
 	["kaa-nw"] = [=[

Nawayı wálayatı:

 	["kaa-qa"] = [=[Qashqadárya wálayatı:
 	["kaa-sa"] = [=[Samarqand wálayatı:
 	["kaa-su"] = [=[

Surxandárya wálayatı:

 	["kaa-ta"] = [=[

Tashkent wálayatı:

 }
 local t = title .. (regtitle[region] or ) .. '\n'
 
 if (pArgs['headeronly'] and pArgs['headeronly'] == 'yes') then
   return t
 end
 
 local params = (pArgs['years'] and mw.text.jsonDecode(pArgs['years'] or , mw.text.JSON_TRY_FIXING)) or {}
 local year = {}
 local mtype = {}
 local images = {}
 for i, j in ipairs(params) do
 	year[j.id] = j.year or 
 	mtype[j.id] = j["type"] or 
 	images[j.id] = j.image
 end
 local background_color = "#F8F8F8;"
 local mwl = mw.language.new( lang )
 local sort = pArgs['sort'] or 'no'
 local sortlevel = tonumber(pArgs['sortlevel']) or 1
 local sections = pArgs['sections'] or 
  
 local wdids = pArgs['wdid'] or 
 wdids = mw.text.split( wdids, ",", true) or {}
 local wdids_for_map = ""
 
 values = {}
 for i, wdid in ipairs(wdids) do
 	wdids_for_map = wdids_for_map .. "wd:" .. wdid .. " "
   values[i] = {}

values[i]['wdid'] = wdid local p2186 = mw.wikibase.getBestStatements(wdid, 'P2186')

   values[i]['id'] = (p2186[1] and p2186[1]['mainsnak']['datavalue']['value']) or 
   values[i]['name'] = mw.wikibase.getLabelByLang(wdid, lang) or mw.wikibase.getLabelByLang(wdid, llanguage) or 
   values[i]['wiki'] = mw.wikibase.getSitelink(wdid, lang .. 'wiki') or 
   values[i]['image'] = mw.wikibase.getBestStatements(wdid, 'P18')
   values[i]['image'] = images[values[i]['id']] or (values[i]['image'][1] and values[i]['image'][1]['mainsnak']['datavalue']['value'])
   values[i]['commonscat'] = mw.wikibase.getBestStatements(wdid, 'P373')
   values[i]['commonscat'] = (values[i]['commonscat'][1] and values[i]['commonscat'][1]['mainsnak']['datavalue']['value']) or 
   local coords = mw.wikibase.getBestStatements(wdid, 'P625')
   values[i]['lat'] = (coords[1] and coords[1]['mainsnak']['datavalue']['value'].latitude) or 
   values[i]['long'] = (coords[1] and coords[1]['mainsnak']['datavalue']['value'].longitude) or 
   local p17 = mw.wikibase.getBestStatements(wdid, 'P17')
   p17 = (p17[1] and p17[1]['mainsnak']['datavalue']['value']['id']) or 
   values[i]['municipality'] = mw.wikibase.getBestStatements(wdid, 'P131') or 
   values[i]['defaultcat'] = rcat[(values[i]['municipality'][1] and values[i]['municipality'][1]['mainsnak']['datavalue']['value']['id']) or ] or 
   values[i]['municipality'] = (values[i]['municipality'][1] and mw.wikibase.getLabelByLang(values[i]['municipality'][1]['mainsnak']['datavalue']['value']['id'], lang)) or 
   values[i]['first_level'] = values[i]['municipality']
   values[i]['munlevels'] = values[i]['municipality']
   if values[i]['municipality'] ~=  then
     local x = mw.wikibase.getBestStatements(wdid, 'P131')
     x = mw.wikibase.getBestStatements(x[1]['mainsnak']['datavalue']['value']['id'], 'P131')
     while x and next(x) and x[1]['mainsnak']['datavalue']['value']['id'] ~= p17 do

values[i]['first_level'] = mw.wikibase.getLabelByLang(x[1]['mainsnak']['datavalue']['value']['id'], lang) or

       values[i]['municipality'] =  values[i]['first_level'] .. ", " .. values[i]['municipality']
       values[i]['munlevels'] =  values[i]['first_level'] .. "#%" .. values[i]['munlevels']
       if values[i]['defaultcat'] ==  and rcat[x[1]['mainsnak']['datavalue']['value']['id']] then
       	values[i]['defaultcat'] = rcat[x[1]['mainsnak']['datavalue']['value']['id']]
       end
       x = mw.wikibase.getBestStatements(x[1]['mainsnak']['datavalue']['value']['id'], 'P131')
     end
     values[i]['munlevels'] = mw.text.split(values[i]['munlevels'], "#%", true)
   end

local p1435 = mw.wikibase.getBestStatements(wdid, 'P1435')

   local p2795 = mw.wikibase.getBestStatements(wdid, 'P2795')
   values[i]['address'] = (p2795[1] and p2795[1]['mainsnak']['datavalue']['value']['text']) or 
   values[i]['year'] = year[values[i]['id']] or 
   values[i]['mtype'] = mtype[values[i]['id']] or 
 end
 local csection = 
 if sort and sort == "yes" then
   function comp(a, b)

if (a.munlevels[sortlevel] or ) < (b.munlevels[sortlevel] or ) or ((a.munlevels[sortlevel] or ) == (b.munlevels[sortlevel] or ) and (b.address or ) > (a.address or )) then return true else return false end end

   table.sort( values, comp )
 end

t = t .. '

'
 t = t .. frame:extensionTag{ name = 'maplink', content = [=[ {
 "type": "ExternalData",
 "service": "geopoint",
 "query": "SELECT DISTINCT ?id ?geo ?title (CONCAT('[', STR(?id), ' ', SUBSTR(STR(?id), 32, 400), ']', coalesce(sample(?file), )) AS ?description) (coalesce(max(?marker_color), '#444444') as ?marker_color) WHERE {?id wdt:P625 ?geo . VALUES ?id {]=] .. wdids_for_map .. [=[} OPTIONAL { ?id wdt:P18 ?image. BIND(COALESCE(CONCAT('\\nFayl:', substr(str(?image), 52, 400), ''), ) as ?file)} OPTIONAL { ?id wdt:P31 ?type . } OPTIONAL {VALUES (?type ?marker_color) { (wd:Q839954 '#008000') (wd:Q4989906 '#800000') (wd:Q2319498 '#000080') (wd:Q570116 '#AAAA00') }} SERVICE wikibase:label { bd:serviceParam wikibase:language ']=] .. lang .. [=[' . ?id rdfs:label ?title.} } GROUP BY ?id ?geo ?title " } ]=], 
args = { ["text"] = "Xaritani ochish"} } .. '\n

'

 local layout = require( "Modul:Monument/layout" )
 for i, m in ipairs(values) do
  if sections and sections == 'yes' and (m.munlevels[sortlevel] or ) ~= csection then
     t = t .. "\n== " .. (m.munlevels[sortlevel] or ) .. " ==\n"

csection = (m.munlevels[sortlevel] or ) end

   t = t .. layout.monument_header(m.id, "", m.image, "", background_color)
   
   local icon = 
   local iconsize = '20'
   
   if m.mtype == 'architecture' then
   	icon = 'PorticoIcon.svg'
   elseif m.mtype == 'sight' then
   	icon = 'PorticoIcon.svg'
   elseif m.mtype == 'archeology' then
   	icon = 'ArcheologyIcon .svg'
   	iconsize = '22'
   elseif m.mtype == 'monument' then
   	icon = 'MonumentIcon .svg'
   end
   t = t .. layout.monument_title(m.name, icon, iconsize)
   t = t .. layout.makelabel("Manzil", m.address, "
") t = t .. layout.makelabel("ID", '' .. m.id .. "", "    ") local links = if m.wiki and m.wiki ~= then links = links .. layout.wikipedialink(lang, m.wiki, "Wikipedia maqalası") end if m.commonscat and m.commonscat ~= then links = links .. layout.commonslink(m.commonscat, "Wikiqoyma toifasi") end links = links .. layout.wikidatalink(m.wdid, "Vikimaʼlumotlar bandi") if m.lat and m.lat ~= and m.long and m.long ~= then links = links .. ' ' .. frame:extensionTag{ name = 'maplink', content = [=[{ "type": "Feature", "geometry": { "type": "Point", "coordinates": []=] .. m.long .. [=[, ]=] .. m.lat .. [=[] },

}]=], args = { ["latitude"] = m.lat, ["longitude"] = m.long, ["zoom"] = 15} } .. '\n'

   end    
   t = t .. layout.makelabel("Havolalar", links, "
") t = t .. layout.makelabel("Tavsifi", m.year, "
") if m.commonscat == then if m.defaultcat == then m.commonscat = dcat else m.commonscat = m.defaultcat end end if m.lat ~= and m.long ~= then t = t .. '' end t = t .. layout.uploadlink(campaign, "Fayl yuklash", m.wdid, "", m.name .. ": " .. m.municipality .. ", " .. m.address, m.commonscat) end return t

end

return p;