Documentația acestui modul poate fi creată la Modul:InfoboxUNESCOWHS/doc

local infobox = require('Modul:Infobox')
local getArgs = require('Modul:Arguments').getArgs
local wikidata = require('Modul:Wikidata')
local infoboxImage = require('Modul:InfoboxImage').InfoboxImage
local TableTools = require('Modul:TableTools')
local nameAndFlag = require('Modul:NameAndImage')._nameAndImage
local coords = require('Modul:Coordonate').coord
local join = require('Modul:Separated entries')._main
local DateUtils = require('Modul:DateUtils')
local EditAtWikidata = require('Modul:EditAtWikidata')
local StringUtils = require('Modul:StringUtils')

local appendToString = StringUtils._appendToString
local encloseString = StringUtils._encloseString
local prependToString = StringUtils._prependToString

local p = {}

local function computeSessionId(year)
	local nYear = tonumber(year)
	if nYear == 1977 then return '1' end
	local nSession = nYear - 1976
	return 'a ' .. tostring(nSession) .. '-a'
end

local function appendEditAtWikidata(wdparams, propertyIndex, paramData)
	if wdparams[propertyIndex] and paramData and paramData ~= '' then
		return ' ' .. EditAtWikidata.displayMessage(wdparams[propertyIndex])
	end
	return ''
end

p.fromArgs = function(child, title, image, caption, country, subregionType, town, subregion, whsType, criteria, whsId, region, year, session, extension, danger, pushpin, lat, long, tipCoords, embedded, wdparams)
	local infoboxArgs = {aboveclass = 'unesco' }
	infoboxArgs['child'] = child
	infoboxArgs['caption'] = child ~= "yes" and caption
	infoboxArgs['subheader'] = child ~= "yes" and "'''[[Locuri din Patrimoniul Mondial UNESCO|Patrimoniul Mondial UNESCO]]'''"
	infoboxArgs['title'] = child == "yes" and "Patrimoniu mondial UNESCO" or title
	if child ~= "yes" then
		infoboxArgs['image'] = image
	end
	infoboxArgs['culoare cadru'] = 'FFF'
	infoboxArgs['culoare text'] = '000'
	infoboxArgs['below'] = tostring(mw.html.create('small'):wikitext('* [http://whc.unesco.org/en/list Lista Patrimonului Mondial]' .. tostring(mw.html.create('br')) .. '** [http://whc.unesco.org/en/list/?search=&search_by_country=&type=&media=&region=&order=region Regiunile după clasificarea UNESCO]'))
	infoboxArgs['belowstyle'] = 'border-top-style: solid; border-color:#aaa; border-top-width: 1px'
	infoboxArgs['doc'] = 'Infocaseta Patrimoniu UNESCO'
	infoboxArgs['wikidata'] = 'y'
	
	local lineIdx = 1
	
	infoboxArgs['label' ..tostring(lineIdx)] = 'Țara'
	infoboxArgs['data' ..tostring(lineIdx)] = appendToString({country, appendEditAtWikidata(wdparams, 'country', country)})
	lineIdx = lineIdx + 1
	
	infoboxArgs['label' ..tostring(lineIdx)] = 'Localitate'
	infoboxArgs['data' .. tostring(lineIdx)] = appendToString({town, appendEditAtWikidata(wdparams, 'town', town)})
	lineIdx = lineIdx + 1
	
	infoboxArgs['label' ..tostring(lineIdx)] = subregionType
	infoboxArgs['data' ..tostring(lineIdx)] = appendToString({subregion, appendEditAtWikidata(wdparams, 'subregion', subregion)})
	lineIdx = lineIdx + 1
	
	infoboxArgs['label' ..tostring(lineIdx)] = 'Tip'
	infoboxArgs['data' ..tostring(lineIdx)] = whsType
	lineIdx = lineIdx + 1

	if criteria then 
		infoboxArgs['label' ..tostring(lineIdx)] = '[[Patrimoniul Mondial UNESCO#Criterii de selecție|Criterii]]'
		infoboxArgs['data' ..tostring(lineIdx)] = appendToString({
			encloseString({
				criteria,
				'[http://whc.unesco.org/en/criteria/ ',
				']'
			}),
			appendEditAtWikidata(wdparams, 'criteria', criteria)
		})
		lineIdx = lineIdx + 1
	end

	if whsId then
		infoboxArgs['label' ..tostring(lineIdx)] = 'Referință'
		local formatterURL = wikidata.findOneValueNoRef('P1630', 'P757')
		infoboxArgs['data' ..tostring(lineIdx)] = '[' .. mw.ustring.gsub(formatterURL, '%$1', whsId) .. ' ' .. whsId .. ']' .. appendEditAtWikidata(wdparams, 'whsId', whsId)
		lineIdx = lineIdx + 1
	end

	infoboxArgs['label' ..tostring(lineIdx)] = 'Regiunea**'
	infoboxArgs['data' ..tostring(lineIdx)] = region
	lineIdx = lineIdx + 1
	
	if year then
		infoboxArgs['label' ..tostring(lineIdx)] = 'Anul'
		infoboxArgs['data' ..tostring(lineIdx)] = year .. ' ' .. tostring(mw.html.create('small'):wikitext('([[Comitetul Patrimoniului Mondial|Sesiunea]] ' .. session .. ')')) .. appendEditAtWikidata(wdparams, 'year', year)
		lineIdx = lineIdx + 1
	end

	infoboxArgs['label' ..tostring(lineIdx)] = 'Extindere'
	infoboxArgs['data' ..tostring(lineIdx)] = extension
	lineIdx = lineIdx + 1

	infoboxArgs['label' ..tostring(lineIdx)] = '[[Listă a locurilor din patrimoniul mondial aflate în pericol|În pericol]]'
	infoboxArgs['data' ..tostring(lineIdx)] = danger
	lineIdx = lineIdx + 1
	
	if pushpin and pushpin.map and lat ~= nil and long ~= nil then
		local pushpinMapArgs = {pushpin.map, lat = tostring(lat), long=tostring(long), label = title, float='none', border='none', position=pushpin.labelpos, width=pushpin.size}
		infoboxArgs['data' .. tostring(lineIdx)] = mw.getCurrentFrame():expandTemplate{title='Harta de localizare', args = pushpinMapArgs} .. tostring(mw.html.create('small'):wikitext(pushpin.descr))
		lineIdx = lineIdx + 1
	end
	
	local childFrame = mw.getCurrentFrame()
	local parentFrame = childFrame:getParent()
	while parentFrame ~= nil do
		childFrame = parentFrame
		parentFrame = parentFrame:getParent()
	end
		
	if lat ~= nil and long ~= nil then
		infoboxArgs['label' ..tostring(lineIdx)] = 'Coordonate' .. (tipCoords and (' ' .. tostring(tipCoords)) or '')
		infoboxArgs['data' ..tostring(lineIdx)] = coords(childFrame:newChild{title='Coordonate', args={lat, long, 'type:city', format = 'dms', display='inline,title'}}) .. appendEditAtWikidata(wdparams, 'coords', lat)
		lineIdx = lineIdx + 1
	end

	for embIdx, eachEmbedded in pairs(embedded) do
		infoboxArgs['data' .. tostring(lineIdx)] = eachEmbedded
		lineIdx = lineIdx + 1
	end

	return infobox.infobox(infoboxArgs)
end

p.fromArray = function(args)
	local infoboxArgs = {}
	local child = args['embed']
	local title = args['WHS'] or args['Name'] or args['nume'] or StringUtils._capitalize({wikidata.findLabel(nil, 'ro')})
	local wdparams = {}
	
	local image = nil
	local imageName = args['Image'] or args['imagine']
	local caption = args['descriere']
	if not imageName then
		imageName, caption = wikidata.findImageAndCaption()
	end
	if imageName then
		image = infoboxImage{image=imageName, sizedefault='frameless'}
	end
	local country = args['Country'] or args['țara'] or args['țară'] or args['State Party']
	local countryQIds = {}
	
	if country == nil or args['pushpin_map'] == nil then
		local countries = {}
		local countryClaims = wikidata.findBestClaimsForProperty(nil, 'P17')
		if countryClaims then
			for countryClaimIdx, eachCountryClaim in pairs(countryClaims) do
				if eachCountryClaim.type == 'statement' and eachCountryClaim.mainsnak and eachCountryClaim.mainsnak.snaktype == 'value' then
					local eachCountryQId = eachCountryClaim.mainsnak.datavalue.value.id
					table.insert(countryQIds, eachCountryQId)
					if country == nil then
						table.insert(countries, nameAndFlag(eachCountryQId, 'P41'))
					end
				end
			end
		end
		if #countries > 0 then
			country = table.concat(countries, tostring(mw.html.create('br')))
			wdparams['country'] = 'P17'
		end
	end
	
	local town = wikidata.loadOneValueInChain({'P276'})
	wdparams['town'] = 'P276'
	
	local subregionType = args['subregiune_tip'] or 'Unitate administrativă'			
	local subregion = args['Subregion'] or args['subregiune'] 
	if not subregion then
		local subregionClaims = wikidata.findBestClaimsForProperty(nil, 'P131')
		local subsubregions = {}
		if subregionClaims and #subregionClaims > 0 then
			wdparams['subregion'] = 'P131'
			for subregionIdx,subregionClaim in ipairs(subregionClaims) do
				local subsubregion = wikidata.loadOneValueInChain({subregionClaim.mainsnak.datavalue.value.id, 'P131'})
				if subsubregion then
					subsubregions[subregionIdx] = appendToString({wikidata.printSnak(subregionClaim.mainsnak), prependToString({subsubregion, ', '})})
				else
					subsubregions[subregionIdx] = wikidata.printSnak(subregionClaim.mainsnak)
				end
			end
			subregion = table.concat(subsubregions, tostring(mw.html.create('br')))
		end
	end
	local whsType = args['Type'] or args['tip']
	local criteria = args['Criteria'] or args['criterii']

	local claimsList = wikidata.findBestClaimsForProperty(nil, 'P2614')
	local crits = {}
	if claimsList then
		for _,eachClaim in pairs(claimsList) do
			if eachClaim.type == 'statement' and eachClaim.mainsnak and eachClaim.mainsnak.snaktype == 'value' then
				table.insert(crits, wikidata.findLabel('Q' .. tostring(eachClaim.mainsnak.datavalue.value['numeric-id']), 'ro'))
			end
		end
	end
	if #crits > 0 then
		crits.separator = ', '
		criteria = join(crits)
		wdparams['criteria'] = 'P2614'
	end
	
	local whsId = args['ID'] 
	if not whsId then
		whsId = wikidata.findOneValueNoRef('P757')
		wdparams['whsId'] = 'P757'
	end
	local region = args['Region'] or args['regiunea']
	local year = args['Year'] or args['an']
	if not year then
		local heritageStatusClaims = wikidata.findBestClaimsForProperty(nil, 'P1435')
		if heritageStatusClaims then for _,eachHeritageStatusClaim in ipairs(heritageStatusClaims) do
			if eachHeritageStatusClaim.mainsnak.snaktype == 'value' and eachHeritageStatusClaim.mainsnak.datavalue.value['numeric-id'] == 9259 then
				if eachHeritageStatusClaim.qualifiers and eachHeritageStatusClaim.qualifiers['P580'] then
					local heritageStartDate = DateUtils.extractDateFromWikidataSnak(eachHeritageStatusClaim.qualifiers['P580'][1])
					year = tostring(heritageStartDate.year)
					wdparams['year'] = 'P1435'
				end
			end
		end	end
	end
	local session = args['Session'] or args['sesiunea']
	if not session then
		session = year and computeSessionId(year)
		wdparams['session'] = 'P1435'
	end
	local extension = args['Extension'] or args['extindere']
	local danger = args['Danger'] or args['pericol']
	
	local pushpin = {}
	if child ~= "yes" then 
		pushpin.map = args['pushpin_map']
		if pushpin.map == nil and #countryQIds > 0 then
			pushpin.map = wikidata.findLabel(countryQIds[1], 'ro')
		end
	end

	local tipCoords = args['tip_coordonate']
	local lat = nil
	local long = nil
	if args['latd'] and args['longd'] then
		local latd = tonumber(args['latd'] or args['latitude'] or 0)
		local latm = tonumber(args['latm'] or 0)
		local lats = tonumber(args['lats'] or 0)
		lat = latd + (latm/60) + (lats/3600)
		if args['latNS'] == 'S' then lat = -lat end
		
		local longd = tonumber(args['longd'] or args['longitude'] or 0)
		local longm = tonumber(args['longm'] or 0)
		local longs = tonumber(args['longs'] or 0)
		long = longd + (longm/60) + (longs/3600)
		if args['longEV'] == 'S' then long = -long end
	end
	if child ~= 'yes' and (lat == nil or long == nil) then
		local coordsClaim = wikidata.findBestClaimsForProperty(nil, 'P625')
		if coordsClaim and coordsClaim[1] and coordsClaim[1].type == 'statement' and coordsClaim[1].mainsnak and coordsClaim[1].mainsnak.snaktype == 'value' then
			lat = coordsClaim[1].mainsnak.datavalue.value.latitude
			long = coordsClaim[1].mainsnak.datavalue.value.longitude
			wdparams['coords'] = 'P625'
		end
	end
	pushpin.descr = args['pushpin_descriere_harta'] or 'Poziția geografică'
	pushpin.size = args['pushpin_dimensiune_hartă'] or '290'
	pushpin.labelpos = args['pushpin_label_position'] or 'right'
	local embedded = {}
	local embeddedListIndices = TableTools.affixNums(args, 'embedded')
	for embededListIndicesIdx, embeddedListIndicesEntry in pairs(embeddedListIndices) do
		if args['embedded' .. embeddedListIndicesEntry] then table.insert(embedded, args['embedded' .. embeddedListIndicesEntry]) end
	end
	return p.fromArgs(child, title, image, caption, country, subregionType, town, subregion, whsType, criteria, whsId, region, year, session, extension, danger, pushpin, lat, long, tipCoords, embedded, wdparams)
end

p.fromFrame = function(frame)
	local args = getArgs(frame)
	return p.fromArray(args)
end

return p