Jump to content

Module:ⵜⴰⴼⵍⵓⵖⵎⵉⵙⵜ ⵜⴰⵎⵓⵔⵜ

ⵙⴳ ⵡⵉⴽⵉⴱⵉⴷⵢⴰ
local p = {}
local wikibase = mw.wikibase

local function trimWhitespace(s)
	if type(s) ~= 'string' then return nil end
	s = s:match('^%s*(.-)%s*$')
	if s == '' then return nil end
	return s
end

local function getArgs(frame)
	local args = {}
	
	local function processArgs(t)
		if type(t) == 'table' or type(t) == 'userdata' then
			for k, v in pairs(t) do
				local trimmed = trimWhitespace(v)
				if trimmed then args[k] = trimmed end
			end
		end
	end

	local success, parent = pcall(function() return frame:getParent() end)
	if success and parent and parent.args then
		processArgs(parent.args)
	end
	
	if frame and frame.args then
		processArgs(frame.args)
	end
	
	return args
end

local function getSmartLink(entityId)
	if not entityId then return nil end
	
	local sitelink = mw.wikibase.getSitelink(entityId)
	
	if sitelink then
		return '[[' .. sitelink .. ']]'
	end
	
	local label = mw.wikibase.getLabelByLang(entityId, 'zgh')
	
	if not label then
		label = mw.wikibase.getLabel(entityId)
	end
	
	if not label then
		local entity = mw.wikibase.getEntity(entityId)
		if entity and entity.labels and entity.labels.en then
			label = entity.labels.en.value
		end
	end
	
	label = label or entityId

	local title = mw.title.new(label)
	if title and title.exists then
		return '[[' .. label .. ']]'
	else
		return '[[:d:' .. entityId .. '|' .. label .. ']]'
	end
end

local function getEntity(entityId)
	if not mw.wikibase then return nil end
	if entityId == '' then entityId = nil end
	entityId = entityId or mw.wikibase.getEntityIdForCurrentPage()
	if not entityId or entityId == '' then return nil end

	local success, entity = pcall(function() return mw.wikibase.getEntity(entityId) end)
	if success and entity then
		entity.getBestStatements = function(self, propertyId)
			if not self.claims or not self.claims[propertyId] then return {} end
			local statements = {}
			for _, claim in pairs(self.claims[propertyId]) do
				if claim.mainsnak.snaktype == 'value' then
					table.insert(statements, claim)
				end
			end
			return statements
		end
		return entity
	end
	return nil
end

local function getCoordinates(entity)
	local statements = entity:getBestStatements('P625')
	if #statements > 0 then
		local val = statements[1].mainsnak.datavalue.value
		return val.latitude, val.longitude
	end
	return nil, nil
end

local function formatDateValue(datavalue)
	if not datavalue or not datavalue.value then return nil end
	local timeValue = datavalue.value.time
	
	local yearStr = string.match(timeValue, "^([%+%-]%d+)")
	if yearStr then
		local year = tonumber(yearStr)
		if year < 0 then
			return math.abs(year) .. ' ⴷⴰⵜ ⵏ ⵜⵍⴰⵍⵉⵜ'
		else
			return tostring(year)
		end
	end
	return nil
end

local function getOldestDate(entity, propertyId)
	local claims = entity:getBestStatements(propertyId)
	if #claims == 0 then return nil end

	local bestClaim = nil
	local minYear = 99999
	
	for _, claim in ipairs(claims) do
		if claim.mainsnak.datavalue and claim.mainsnak.datavalue.value.time then
			local timeValue = claim.mainsnak.datavalue.value.time
			local yearStr = string.match(timeValue, "^[%+%-](%d+)")
			if yearStr then 
				local year = tonumber(yearStr)
				if year < minYear then
					minYear = year
					bestClaim = claim
				end
			end
		end
	end

	if bestClaim then
		return formatDateValue(bestClaim.mainsnak.datavalue)
	end
	return nil
end

local function getPreviousValue(entity, propertyId, currentYear)
	if not entity or not entity.id then return nil end
	local claims = wikibase.getAllStatements(entity.id, propertyId)
	if not claims or #claims == 0 then return nil end
	
	local prevValue = nil
	local prevYear = -9999

	for _, claim in ipairs(claims) do
		if claim.mainsnak.snaktype == 'value' and claim.rank ~= 'deprecated' then
			local claimYear = 0
			if claim.qualifiers and claim.qualifiers['P585'] then
				local timeValue = claim.qualifiers['P585'][1].datavalue.value.time
				local yearStr = string.match(timeValue, "^[%+%-](%d+)")
				if yearStr then claimYear = tonumber(yearStr) end
			end

			if claimYear < currentYear and claimYear > prevYear then
				local val = claim.mainsnak.datavalue.value
				if type(val) == 'table' and val.amount then
					prevValue = tonumber(val.amount)
				elseif type(val) == 'number' then
					prevValue = val
				end
				prevYear = claimYear
			end
		end
	end
	return prevValue
end

local function formatBigNumber(amount)
	if not amount then return '' end
	local lang = mw.getContentLanguage()
	
	if amount >= 1000000000000 then
		local val = amount / 1000000000000
		return lang:formatNum(tonumber(string.format("%.2f", val))) .. ' ⵜⵔⵉⵍⵢⵓⵏ'
	elseif amount >= 1000000000 then
		local val = amount / 1000000000
		return lang:formatNum(tonumber(string.format("%.2f", val))) .. ' ⵎⵍⵢⴰⵔ'
	elseif amount >= 1000000 then
		local val = amount / 1000000
		return lang:formatNum(tonumber(string.format("%.2f", val))) .. ' ⵎⵍⵢⵓⵏ'
	else
		local res = lang:formatNum(amount)
		res = res:gsub('%s+', ',')
		return res:gsub(' ', ',')
	end
end

local function getEconomicIcon(entity, property, value, year)
	if not value then return '' end
	
	local GreenUp = '<span style="color:#11CC11; font-weight:bold; font-size:130%;">▲</span>'
	local RedDown = '<span style="color:red; font-weight:bold; font-size:130%;">▼</span>'
	local GrayUp = '<span style="color:gray; font-weight:bold; font-size:130%;">▲</span>'
	local GrayDown = '<span style="color:gray; font-weight:bold; font-size:130%;">▼</span>'
	local GrayEqual = '<span style="color:gray; font-weight:bold; font-size:130%;">▼</span>'

	local prev = getPreviousValue(entity, property, year)
	
	if prev then
		if property == 'P1125' then
			if value < prev then
				return GreenUp
			elseif value > prev then
				if value > 0.45 then
					return RedDown
				else
					return GrayUp
				end
			end
		elseif property == 'P2131' or property == 'P1081' or property == 'P2132' then
			if value > prev then
				return GreenUp
			elseif value < prev then
				if value > 1000000000000 then
					return GrayUp
				else
					return RedDown
				end
			end
		elseif property == 'P1082' then
			if value > prev then
				return GreenUp
			elseif value < prev then
				if value > 100000000 then
					return GrayUp
				elseif value < (prev * 0.9) then
					return RedDown
				else
					return GrayDown
				end
			end
		else
			if value > prev then
				return GreenUp
			elseif value < prev then
				return RedDown
			end
		end
		return GrayEqual
	end

	return ''
end

local function getLatestValue(entity, propertyId, raw)
	local claims = entity:getBestStatements(propertyId)
	if #claims == 0 then return nil end

	local bestClaim = nil
	local maxYear = -9999
	
	for _, claim in ipairs(claims) do
		local year = 0
		if claim.qualifiers and claim.qualifiers['P585'] then
			local timeValue = claim.qualifiers['P585'][1].datavalue.value.time
			local yearStr = string.match(timeValue, "^[%+%-](%d+)")
			if yearStr then year = tonumber(yearStr) end
		end

		if year > maxYear then
			maxYear = year
			bestClaim = claim
		elseif not bestClaim then
			bestClaim = claim
		end
	end

	if not bestClaim and #claims > 0 then
		bestClaim = claims[#claims]
	end

	if bestClaim then
		local val = bestClaim.mainsnak.datavalue.value
		local amount = nil
		
		if type(val) == 'table' and val.amount then
			amount = tonumber(val.amount)
		elseif type(val) == 'number' then
			amount = val
		end

		if raw then return amount end

		local formattedVal = ''
		
		if amount then
			if propertyId == 'P2131' or propertyId == 'P2132' or propertyId == 'P2133' or propertyId == 'P38' then
				formattedVal = formatBigNumber(amount)
				if propertyId == 'P2131' or propertyId == 'P2132' or propertyId == 'P2133' then
					formattedVal = formattedVal .. ' $'
				end
			else
				local lang = mw.getContentLanguage()
				formattedVal = lang:formatNum(amount)
				formattedVal = formattedVal:gsub('%s+', ',')
				formattedVal = formattedVal:gsub(' ', ',')
			end
		else
			return nil 
		end

		local icon = ''
		if propertyId == 'P2131' or propertyId == 'P2132' or propertyId == 'P2299' or propertyId == 'P1081' or propertyId == 'P1125' or propertyId == 'P1198' or propertyId == 'P1082' or propertyId == 'P2139' or propertyId == 'P1279' or propertyId == 'P2046' then
			icon = getEconomicIcon(entity, propertyId, amount, maxYear) .. ' '
		end
		
		if propertyId == 'P2131' or propertyId == 'P2132' then
			if maxYear > 0 then
				return icon .. formattedVal .. '___YEAR:' .. maxYear .. '___'
			else
				return icon .. formattedVal
			end
		else
			if maxYear > 0 then
				return icon .. formattedVal .. ' <small>(' .. maxYear .. ')</small>'
			else
				return icon .. formattedVal
			end
		end
	end
	return nil
end

local function getLatestDensity(entity)
	local claims = entity:getBestStatements('P7959')
	if #claims > 0 then
		local val = claims[1].mainsnak.datavalue.value
		local amount = tonumber(val.amount)
		local lang = mw.getContentLanguage()
		if amount then
			local res = lang:formatNum(amount)
			res = res:gsub('%s+', ',')
			res = res:gsub(' ', ',')
			return res
		end
		return nil
	end

	local pop = getLatestValue(entity, 'P1082', true)
	local area = getLatestValue(entity, 'P2046', true)

	if pop and area and area > 0 then
		local density = pop / area
		local mult = 100
		density = math.floor(density * mult + 0.5) / mult
		
		local lang = mw.getContentLanguage()
		local res = lang:formatNum(density)
		res = res:gsub('%s+', ',')
		res = res:gsub(' ', ',')
		return res
	end

	return nil
end

local function makeEditIcon(entityId, propertyId)
	if not entityId or not propertyId then return '' end
	return ' [[ⴰⴼⴰⵢⵍⵓ:Edit-icon.svg|15px|link=https://www.wikidata.org/wiki/' .. entityId .. '#' .. propertyId .. '|ⵙⵏⴼⵍ ⴳ ⵡⵉⴽⵉⴷⴰⵜⴰ]]'
end

local function makeRequest(entityId, property)
	local entity = getEntity(entityId)
	if not entity then return nil, false end
	
	if property == 'P571' then
		local val = getOldestDate(entity, 'P571')
		if val then return val, true else return nil, false end
	end

	if property == 'P1082' or property == 'P2573' or property == 'P2046' or property == 'P2131' or property == 'P2250' or property == 'P1081' or property == 'P3529' or property == 'P1198' or property == 'P1125' or property == 'P1540' or property == 'P1539' or property == 'P6343' or property == 'P6344' or property == 'P3001' or property == 'P1279' or property == 'P2997' or property == 'P2855' or property == 'P2133' or property == 'P2137' or property == 'P2139' or property == 'P2134' or property == 'P3087' or property == 'P2834' or property == 'P2299' or property == 'P4010' or property == 'P3864' or property == 'P2132' then
		local val = getLatestValue(entity, property)
		if val then 
			return val, true 
		else
			return nil, false
		end
	end

	if property == 'P7959' then
		local densityValue = getLatestDensity(entity)
		if densityValue then return densityValue, true end
		return nil, false
	end

	if property == 'P2295' then
		local claims = entity:getBestStatements('P2295')
		if #claims > 0 then
			for _, claim in ipairs(claims) do
				if claim.qualifiers and claim.qualifiers['P1013'] then
					for _, qual in ipairs(claim.qualifiers['P1013']) do
						if qual.datavalue and qual.datavalue.value.id == 'Q3892' then
							local amount = tonumber(claim.mainsnak.datavalue.value.amount)
							return amount and math.floor(amount) or nil, true
						end
					end
				end
			end
		end
		return nil, false
	end

	if property == 'P130' then
		local claims = entity:getBestStatements('P130')
		if #claims > 0 then
			local latestYear = 0
			
			for _, claim in ipairs(claims) do
				if claim.qualifiers and claim.qualifiers['P585'] then
					local timeValue = claim.qualifiers['P585'][1].datavalue.value.time
					local yearStr = string.match(timeValue, "^[%+%-](%d+)")
					if yearStr then
						local y = tonumber(yearStr)
						if y > latestYear then latestYear = y end
					end
				end
			end

			local stats = {}
			for _, claim in ipairs(claims) do
				local claimYear = 0
				if claim.qualifiers and claim.qualifiers['P585'] then
					local timeValue = claim.qualifiers['P585'][1].datavalue.value.time
					local yearStr = string.match(timeValue, "^[%+%-](%d+)")
					if yearStr then claimYear = tonumber(yearStr) end
				end
				
				
				if latestYear == 0 or claimYear == latestYear then
					if claim.qualifiers and claim.qualifiers['P1107'] then
						local amount = tonumber(claim.qualifiers['P1107'][1].datavalue.value.amount)
						local id = claim.mainsnak.datavalue.value.id
						local label = getSmartLink(id)
						
						table.insert(stats, { pct = amount * 100, lbl = label })
					end
				end
			end

			if #stats > 0 then
				table.sort(stats, function(a,b) return a.pct > b.pct end)
				local lines = {}
				for _, stat in ipairs(stats) do
					
					local pctStr = string.format("%.2f", stat.pct):gsub("%.?0+$", "") 
					table.insert(lines, pctStr .. '% ' .. stat.lbl)
				end
				local res = table.concat(lines, '<br />')
				
				if latestYear > 0 then
					res = res .. '___YEAR:' .. latestYear .. '___'
				end
				return res, true
			end
		end
		return nil, false
	end

	if property == 'P856' then
		local claims = entity:getBestStatements('P856')
		if #claims > 0 and claims[1].mainsnak.datavalue then
			local url = claims[1].mainsnak.datavalue.value
			local display = url:gsub('^https?://', '')
			if display:sub(-1) == '/' then display = display:sub(1, -2) end
			return '[' .. url .. ' ' .. display .. ']', true
		end
		return nil, false
	end

	local statements = entity:getBestStatements(property)
	local filteredStatements = {}

	if property == 'P6' or property == 'P35' or property == 'P36' or property == 'P1622' then
		for _, claim in ipairs(statements) do
			if not (claim.qualifiers and claim.qualifiers['P582']) then
				table.insert(filteredStatements, claim)
			end
		end
	else
		filteredStatements = statements
	end

	if #filteredStatements > 0 then
		local valuesWithLinks = {}
		for _, statement in ipairs(filteredStatements) do
			local datavalue = statement.mainsnak.datavalue
			local valueToAdd = nil

			if datavalue.type == 'string' then
				valueToAdd = datavalue.value
			elseif datavalue.type == 'wikibase-entityid' then
				local targetEntityId = datavalue.value.id
				valueToAdd = getSmartLink(targetEntityId)

				if property == 'P17' then
					local flags = mw.wikibase.getBestStatements(targetEntityId, 'P41')
					if #flags > 0 and flags[1].mainsnak.datavalue then
						valueToAdd = '[[ⴰⴼⴰⵢⵍⵓ:' .. flags[1].mainsnak.datavalue.value .. '|20px|border|link=]] ' .. valueToAdd
					end
				end

			elseif datavalue.type == 'quantity' then
				local amount = tonumber(datavalue.value.amount)
				local lang = mw.getContentLanguage()
				valueToAdd = amount and lang:formatNum(amount) or datavalue.value.amount
				if valueToAdd then valueToAdd = valueToAdd:gsub('%s+', ',') end
				if valueToAdd then valueToAdd = valueToAdd:gsub(' ', ',') end
			elseif datavalue.type == 'time' then
				valueToAdd = string.sub(datavalue.value.time, 9, 10) 
			end

			if valueToAdd then
				table.insert(valuesWithLinks, valueToAdd)
			end
		end
		
		if #valuesWithLinks > 0 then
			if ((property == 'P421' or property == 'P17' or property == 'P463') and #valuesWithLinks > 1) or (property == 'P47' and #valuesWithLinks > 4) then
				local container = mw.html.create('div')
				container:addClass('mw-collapsible mw-collapsed')
				container:tag('div')
					:css('font-weight', 'normal')
					:wikitext('ⵜⴰⵍⴳⴰⵎⵜ [[ⴰⴼⴰⵢⵍⵓ:Incomplete list.svg|17px|link=]]&nbsp; (' .. #valuesWithLinks .. ')') 
				local contentDiv = container:tag('div'):addClass('mw-collapsible-content')
				local ul = contentDiv:tag('ul')
				for _, v in ipairs(valuesWithLinks) do
					ul:tag('li'):wikitext(v)
				end
				return tostring(container), true
			end
			
			return table.concat(valuesWithLinks, ", "), true
		end
	end
	return nil, false
end

local function getValueOrWikidata(args, paramName, propertyId)
	local value = args[paramName]
	local fromWikidata = false
	if value then value = trimWhitespace(value) end
	if not value then
		if propertyId and args.qid ~= '' then
			value, fromWikidata = makeRequest(args.qid, propertyId)
		end
	end
	return value, fromWikidata
end

function p.main(frame)
	local args = getArgs(frame)
	local entityId = args.qid 
	if entityId == '' then entityId = nil end
	entityId = entityId or mw.wikibase.getEntityIdForCurrentPage()

	local entity = getEntity(entityId)

	frame:extensionTag('templatestyles', '', {src = 'ⴰⵍⴱⵓⴹ:ⵜⴰⴼⵍⵓⵖⵎⵉⵙⵜ ⵜⴰⵎⵓⵔⵜ/styles.css'})

	local infobox = mw.html.create('table')
	infobox:addClass('infobox geography vcard')
	infobox:css({
		['border'] = '1px solid #a2a9b1',
		['background-color'] = '#f8f9fa',
		['color'] = 'black',
		['margin'] = '0.5em 0 0.5em 1em',
		['float'] = 'right',
		['clear'] = 'right',
		['font-size'] = '90%',
		['width'] = '18em',
	})

	local headerValue = args['ⵉⵙⵎ ⵏ ⵜⵎⵓⵔⵜ'] or args['ⵉⵙⵎ ⵏ ⴰⵏⵚⵍⵉ'] or args['ⵉⵙⵎ']
	if not headerValue and entityId then
		headerValue = mw.wikibase.getLabelByLang(entityId, 'zgh') or mw.wikibase.getLabel(entityId)
		if not headerValue and entity and entity.labels and entity.labels.en then
			headerValue = entity.labels.en.value
		end
	end
	headerValue = headerValue or mw.title.getCurrentTitle().text

	local headerRow = infobox:tag('tr')
	local headerCell = headerRow:tag('th')
		:attr('colspan', 2)
		:css({
			['background-color'] = '#808080',
			['color'] = '#ffffff',
			['padding'] = '0.4em',                
			['text-align'] = 'center',
			['font-weight'] = 'bold',
			['font-size'] = '125%',
			['position'] = 'relative'
		})

	local headerContainer = mw.html.create('div')
		:css({
			['padding-right'] = '50px', 
			['padding-left'] = '50px',  
		})
		:wikitext(headerValue)
	
	headerCell:node(headerContainer)

	local iconContainer = mw.html.create('div')
		:css({
			['position'] = 'absolute',
			['top'] = '1px',
			['right'] = '1px',
		})
		:wikitext('[[ⴰⴼⴰⵢⵍⵓ:Picto_infobox_map.png|95px|alt=ⵜⴰⵢⴽⵓⵏⵜ|link=]]')
		
	headerCell:node(iconContainer)

	local officialNames = {}
	if entity then
		local officialLangs = {}
		local langClaims = entity:getBestStatements('P37')
		for _, claim in ipairs(langClaims) do
			if claim.mainsnak.datavalue then
				local langId = claim.mainsnak.datavalue.value.id
				local codes = mw.wikibase.getBestStatements(langId, 'P424')
				if #codes > 0 and codes[1].mainsnak.datavalue then
					officialLangs[codes[1].mainsnak.datavalue.value] = langId
				end
			end
		end

		local names = entity:getBestStatements('P1448')
		for _, claim in ipairs(names) do
			if claim.mainsnak.datavalue and claim.mainsnak.datavalue.value then
				local val = claim.mainsnak.datavalue.value
				if officialLangs[val.language] then
					local langQID = officialLangs[val.language]
					local langLink = getSmartLink(langQID)
					
					if langLink then
						if langLink:find('|') then
							langLink = langLink:gsub('|ⵜⵓⵜⵍⴰⵢⵜ%s+', '|')
						else
							langLink = langLink:gsub('^%[%[ⵜⵓⵜⵍⴰⵢⵜ%s+(.+)%]%]$', '[[ⵜⵓⵜⵍⴰⵢⵜ %1|%1]]')
						end
					end
					
					table.insert(officialNames, { text = val.text, langLink = langLink })
				end
			end
		end
	end

	if #officialNames > 0 then
		local nameRow = infobox:tag('tr')
		local nameCell = nameRow:tag('td')
			:attr('colspan', 2)
			:css({
				['text-align'] = 'center',
				['background-color'] = '#ddd',
				['padding'] = '0.4em 0',
				['font-weight'] = 'bold',
				['border-top'] = 'none'
			})
		
		if #officialNames > 3 then
			for i = 1, 3 do
				local item = officialNames[i]
				local nameText = item.text .. ' (' .. item.langLink .. ')'
				if entityId then
					nameText = nameText .. makeEditIcon(entityId, 'P1448')
				end
				nameCell:tag('div'):wikitext(nameText)
			end
			
			local container = nameCell:tag('div')
			container:addClass('mw-collapsible mw-collapsed')
			
			container:tag('div')
				:css('font-weight', 'normal')
				:css('font-size', '85%')
				:css('cursor', 'pointer')
				:wikitext('ⵙ ⵜⵓⵜⵍⴰⵢⵉⵏ ⵏⵏⵉⴹⵏ (' .. (#officialNames - 3) .. ')')
				
			local contentDiv = container:tag('div'):addClass('mw-collapsible-content')
			
			for i = 4, #officialNames do
				local item = officialNames[i]
				local nameText = item.text .. ' (' .. item.langLink .. ')'
				if entityId then
					nameText = nameText .. makeEditIcon(entityId, 'P1448')
				end
				contentDiv:tag('div'):wikitext(nameText)
			end
		else
			for _, item in ipairs(officialNames) do
				local nameText = item.text .. ' (' .. item.langLink .. ')'
				if entityId then
					nameText = nameText .. makeEditIcon(entityId, 'P1448')
				end
				nameCell:tag('div'):wikitext(nameText)
			end
		end
	end

	local flagImage = args['ⴰⵛⵏⵢⴰⵍ'] or args['flag'] or args['ⵜⴰⵡⵍⴰⴼⵜ ⵏ ⵓⵛⵏⵢⴰⵍ']
	local emblemImage = args['ⵜⴰⵡⵎⵔⵉⴳⵜ'] or args['ⵜⴰⵡⵍⴰⴼⵜ ⵏ ⵜⴰⵡⵎⵔⵉⴳⵜ']
	local mottoText = args['ⵜⴰⵏⴼⴰⵍⵉⵜ ⵜⴰⵏⴰⵎⵓⵔⵜ']
	local anthemFile = args['ⵉⵣⵍⵉ ⴰⵏⴰⵎⵓⵔ']
	local anthemLink = nil
	
	if entity then
		if not flagImage then
			local flags = entity:getBestStatements('P41')
			if #flags > 0 then
				local bestFlag = nil
				for _, claim in ipairs(flags) do
					if not (claim.qualifiers and claim.qualifiers['P582']) then
						if claim.mainsnak.datavalue then
							bestFlag = claim.mainsnak.datavalue.value
							break
						end
					end
				end
				if not bestFlag and flags[1].mainsnak.datavalue then
					bestFlag = flags[1].mainsnak.datavalue.value
				end
				flagImage = bestFlag
			end
		end
		
		if not emblemImage then
			local emblems = entity:getBestStatements('P94')
			if #emblems > 0 and emblems[1].mainsnak.datavalue then
				emblemImage = emblems[1].mainsnak.datavalue.value
			end
		end

		if not mottoText then
			local mottos = entity:getBestStatements('P1451')
			if #mottos > 0 and mottos[1].mainsnak.datavalue then
				if mottos[1].mainsnak.datavalue.type == 'monolingualtext' then
					mottoText = mottos[1].mainsnak.datavalue.value.text
				elseif mottos[1].mainsnak.datavalue.type == 'string' then
					mottoText = mottos[1].mainsnak.datavalue.value
				end
			end
		end

		if not anthemFile then
			local anthems = entity:getBestStatements('P85')
			if #anthems > 0 then
				local anthemId = anthems[1].mainsnak.datavalue.value.id
				if anthemId then
					anthemLink = getSmartLink(anthemId)

					local anthemEntity = mw.wikibase.getEntity(anthemId)
					if anthemEntity then
						local audioStmts = anthemEntity:getBestStatements('P51')
						if #audioStmts > 0 and audioStmts[1].mainsnak.datavalue then
							anthemFile = audioStmts[1].mainsnak.datavalue.value
						end
					end
				end
			end
		end
	end

	if flagImage or emblemImage or mottoText or anthemFile then
		local imagesRow = infobox:tag('tr')
		local imagesCell = imagesRow:tag('td')
			:attr('colspan', 2)
			:css({
				['text-align'] = 'center',
				['padding'] = '10px 5px',
				['background-color'] = '#f8f9fa'
			})
		
		local container = mw.html.create('div')
			:css({
				['display'] = 'flex',
				['justify-content'] = 'space-around',
				['align-items'] = 'center',
				['width'] = '100%'
			})

		if flagImage then
			local flagDiv = container:tag('div'):css('text-align', 'center')
			flagDiv:wikitext('[[ⴰⴼⴰⵢⵍⵓ:' .. flagImage .. '|125px|border]]')
			local caption = 'ⴰⵛⵏⵢⴰⵍ'
			if entityId then caption = caption .. makeEditIcon(entityId, 'P41') end
			flagDiv:tag('div'):css('font-size', '85%'):wikitext(caption)
		end

		if emblemImage then
			local emblemDiv = container:tag('div'):css('text-align', 'center')
			emblemDiv:wikitext('[[ⴰⴼⴰⵢⵍⵓ:' .. emblemImage .. '|85px]]')
			local caption = 'ⵜⴰⵡⵎⵔⵉⴳⵜ'
			if entityId then caption = caption .. makeEditIcon(entityId, 'P94') end
			emblemDiv:tag('div'):css('font-size', '85%'):wikitext(caption)
		end
		
		imagesCell:node(container)

		if mottoText then
			local mottoDisplay = 'ⵜⴰⵏⴼⴰⵍⵉⵜ ⵜⴰⵏⴰⵎⵓⵔⵜ: ' .. mottoText
			if entityId then mottoDisplay = mottoDisplay .. makeEditIcon(entityId, 'P1451') end
			imagesCell:tag('div')
				:css('margin-top', '8px')
				:css('font-weight', 'bold')
				:css('font-size', '95%')
				:wikitext(mottoDisplay)
		end

		if anthemFile then
			local anthemDisplay = 'ⵉⵣⵍⵉ ⴰⵏⴰⵎⵓⵔ'
			if anthemLink then
				anthemDisplay = anthemDisplay .. ': ' .. anthemLink
			end
			if entityId then anthemDisplay = anthemDisplay .. makeEditIcon(entityId, 'P85') end
			imagesCell:tag('div')
				:css('margin-top', '8px')
				:wikitext(anthemDisplay .. '<br />[[ⴰⴼⴰⵢⵍⵓ:' .. anthemFile .. '|noicon]]')
		end
	end

	if entity then
		local maps = entity:getBestStatements('P242')
		if #maps > 0 then
			local lat, lon = getCoordinates(entity)
			local caption = 'ⴰⴷⵖⴰⵔ ⵏ ' .. headerValue .. ' ⴳ ⵜⴽⴰⵕⴹⴰ'
			
			if lat and lon then
				local coordStr = frame:expandTemplate{ title = 'Coord', args = { lat, lon, display='inline', format='dms' } }
				caption = caption .. '<br /><div style="display:inline-block; margin-top:2px;">[[ⴰⴼⴰⵢⵍⵓ:OOjs_UI_icon_mapPin-progressive.svg|12px]]&nbsp;' .. coordStr .. '</div>'
			end
			
			infobox:tag('tr'):tag('td')
				:attr('colspan', 2)
				:css('text-align', 'center')
				:wikitext('[[ⴰⴼⴰⵢⵍⵓ:' .. maps[1].mainsnak.datavalue.value .. '|260px|frameless]]')
				:tag('div')
				:css({
					['padding-top'] = '3px',
					['font-size'] = '90%',
					['line-height'] = '1.3'
				})
				:wikitext(caption)
		end
	end

	local sectionHeaderStyle = {
		['background-color'] = '#ddd',
		['text-align'] = 'center',
		['font-weight'] = 'normal',
		['padding'] = '2px',
	}

	local sections = {
		{
			title = 'ⵜⴰⵎⵙⵙⵓⴳⵓⵔⵜ',
			properties = {
				{ 'ⵜⴰⵎⴰⵥⵓⵏⵜ', 'ⵜⴰⵎⴰⵥⵓⵏⵜ', 'P36' },
				{ 'ⵜⵓⵜⵍⴰⵢⵜ ⵜⵓⵏⵚⵉⴱⵜ', 'ⵜⵓⵜⵍⴰⵢⵜ ⵜⵓⵏⵚⵉⴱⵜ', 'P37' },
				{ 'ⴰⴳⵎⴰⵎ ⴳ', 'ⴰⴳⵎⴰⵎ ⴳ', 'P463' },
			}
		},
		{
			title = 'ⴰⵏⴱⴰⴹ',
			properties = {
				{ 'ⴰⵏⴱⴱⴰⴹ', 'ⴰⵏⴱⴱⴰⴹ', 'P35' },
				{ 'ⵜⴰⵍⵖⴰ ⵏ ⵓⵏⴱⴰⴹ', 'ⵜⴰⵍⵖⴰ ⵏ ⵓⵏⴱⴰⴹ', 'P122' },
				{ 'ⵜⴰⵔⵙⵓⵜ ⵏ ⵢⵉⵖⴼ ⵏ ⵓⵡⴰⵏⴽ', 'ⵜⴰⵔⵙⵓⵜ ⵏ ⵢⵉⵖⴼ ⵏ ⵓⵡⴰⵏⴽ', 'P1906' },
			}
		},
		{
			title = 'ⵜⴰⵏⴱⴰⴹⵜ',
			properties = {
				{ 'ⴰⵏⵙⵙⵉⵅⴼ ⵏ ⵜⵏⴱⴰⴹⵜ', 'ⴰⵏⵙⵙⵉⵅⴼ ⵏ ⵜⵏⴱⴰⴹⵜ', 'P6' },
				{ 'ⵜⴰⵏⴱⴰⴹⵜ ⵜⴰⵎⵣⵣⴳⴰⵔⵜ', 'ⵜⴰⵏⴱⴰⴹⵜ ⵜⴰⵎⵣⵣⴳⴰⵔⵜ', 'P208' },
				{ 'ⵜⴰⵏⴱⴰⴹⵜ ⵜⴰⵏⵣⵔⴰⴼⵜ ⵜⴰⵏⴰⴼⵍⵍⴰⵜ', 'ⵜⴰⵏⴱⴰⴹⵜ ⵜⴰⵏⵣⵔⴰⴼⵜ ⵜⴰⵏⴰⴼⵍⵍⴰⵜ', 'P209' },
				{ 'ⵜⴰⵏⴱⴰⴹⵜ ⵜⴰⵣⵔⴼⴰⵏⵜ', 'ⵜⴰⵏⴱⴰⴹⵜ ⵜⴰⵣⵔⴼⴰⵏⵜ', 'P194' },
				{ 'ⴰⵏⴳⵎⴰⵎ ⵓⵣⵔⵉⴼ', 'ⴰⵏⴳⵎⴰⵎ ⵓⵣⵔⵉⴼ', 'P194' },
			}
		},
		{
			title = 'ⴰⵎⵣⵔⵓⵢ',
			properties = {
				{ 'ⴰⵙⴰⴽⵓⴷ ⵏ ⵓⵙⵙⵔⵙⵍ', 'ⴰⵙⴰⴽⵓⴷ ⵏ ⵓⵙⵙⵔⵙⵍ', 'P571' },
			}
		},
		{
			title = 'ⵜⴰⵔⴰⴽⴰⵍⵜ',
			properties = {
				{ 'ⵜⴰⵊⵓⵎⵎⴰ', 'ⵜⴰⵊⵓⵎⵎⴰ', 'P2046' },
				{ 'ⴰⵎⵏⵥⴰⵡ', 'ⴰⵎⵏⵥⴰⵡ', 'P30' },
				{ 'ⵜⴰⵜⵜⵓⵢⵜ', 'ⵜⴰⵜⵜⵓⵢⵜ', 'P2044' },
				{ 'ⵜⴰⵜⵜⵓⵢⵜ ⵅⴼ ⵓⵙⵡⵉⵔ ⵏ ⵢⵉⵍⵍ', 'ⵜⴰⵜⵜⵓⵢⵜ ⵅⴼ ⵓⵙⵡⵉⵔ ⵏ ⵢⵉⵍⵍ', 'P2048' },
				{ 'ⵉⵡⵜⵜⴰ ⴰⴽⴷ', 'ⵉⵡⵜⵜⴰ ⴰⴽⴷ', 'P47' },
				{ 'ⴰⴷⵖⴰⵔ ⴰⵏⴰⴼⵍⵍⴰ', 'ⴰⴷⵖⴰⵔ ⴰⵏⴰⴼⵍⵍⴰ', 'P610' },
				{ 'ⴰⴷⵖⴰⵔ ⴰⵏⴰⴷⴷⴰⵡ', 'ⴰⴷⵖⴰⵔ ⴰⵏⴰⴷⴷⴰⵡ', 'P1589' },
				{ 'ⵉⵏⵉⴳⵍ ⴰⵊⵢⵓⴳⵕⴰⴼⵉ', 'ⵉⵏⵉⴳⵍ ⴰⵊⵢⵓⴳⵕⴰⴼⵉ', 'P1566' },
			}
		},
		{
			title = 'ⵜⴰⵔⴰⵖⵔⴼⵜ',
			properties = {
				{ 'ⵓⵟⵟⵓⵏ ⵏ ⵉⵎⵣⴷⴰⵖⵏ', 'ⵓⵟⵟⵓⵏ ⵏ ⵉⵎⵣⴷⴰⵖⵏ', 'P1082' },
				{ 'ⴰⵙⴳⴷ', 'ⴰⵙⴳⴷ', 'P130' },
				{ 'ⵓⵟⵟⵓⵏ ⵏ ⵜⴰⵡⵊⵉⵡⵉⵏ', 'ⵓⵟⵟⵓⵏ ⵏ ⵜⴰⵡⵊⵉⵡⵉⵏ','ⵓⵟⵟⵓⵏ ⵏ ⵜⵡⴰⵛⵓⵏⵉⵏ', 'P1538' },
				{ 'ⴰⵙⵎⵖⵔⴼ', 'ⴰⵙⵎⵖⵔⴼ', 'P1549' },
				{ 'ⵉⵎⵣⴷⴰⵖⵏ ⵏ ⵜⵎⴷⵉⵏⵉⵏ', 'ⵉⵎⵣⴷⴰⵖⵏ ⵏ ⵜⵎⴷⵉⵏⵉⵏ', 'P6343' },
				{ 'ⵉⵎⵣⴷⴰⵖⵏ ⵏ ⵓⴼⴰⵔⴰ', 'ⵉⵎⵣⴷⴰⵖⵏ ⵏ ⵓⴼⴰⵔⴰ', 'P6344' },
				{ 'ⵜⴰⵏⵥⵥⵉ', 'ⵜⴰⵏⵥⵥⵉ', 'P7959' },
				{ 'ⵉⵙⵎ ⵏ ⵉⵎⵣⴷⴰⵖⵏ', 'ⵉⵙⵎ ⵏ ⵉⵎⵣⴷⴰⵖⵏ', 'P1549' },
				{ 'ⴰⵏⴰⵎⵎⴰⵙ ⵏ ⵓⵡⵜⴰⵢ', 'ⴰⵏⴰⵎⵎⴰⵙ ⵏ ⵓⵡⵜⴰⵢ', 'P2250' },
				{ 'ⴰⵡⵜⴰⵢ ⵓⵍⴳⵉⵏ', 'ⴰⵡⵜⴰⵢ ⵓⵍⴳⵉⵏ', 'P2997' },
				{ 'ⴰⵎⴹⴰⵏ ⵏ ⵉⵎⵣⴷⴰⵖⵏ ⵉⵇⵓⴱⴱⴰⵏⵏ', 'ⴰⵎⴹⴰⵏ ⵏ ⵉⵎⵣⴷⴰⵖⵏ ⵉⵇⵓⴱⴱⴰⵏⵏ', 'P6498' },
				{ 'ⴰⵎⴹⴰⵏ ⵏ ⵉⵎⵣⴷⴰⵖⵏ ⵖⵔⴰⵏⵉⵏ', 'ⴰⵎⴹⴰⵏ ⵏ ⵉⵎⵣⴷⴰⵖⵏ ⵖⵔⴰⵏⵉⵏ', 'P6499' },
				{ 'ⵜⴰⵎⵏⵛⴽⵜ ⵏ ⵓⵎⵏⵖⵉⵎⴰⵏ', 'ⵜⴰⵎⵏⵛⴽⵜ ⵏ ⵓⵎⵏⵖⵉⵎⴰⵏ', 'P3864' },
			}
		},
		{
			title = 'ⴰⴼⴰⵔⵙ ⴰⵎⴰⴷⴷⴰⵢ ⵓⵙⵜⵉⵢ',
			properties = {
				{ 'ⴰⵙⵏⵉⵎⴰⵍ ', 'ⴰⵙⵏⵉⵎⴰⵍ ⵏ ⵓⴼⴰⵔⵙ ⴰⵎⴰⴷⴷⴰⵢ ⵓⵙⵜⵉⵢ', 'nil' },
				{ 'ⴰⵖⵔⵓⴷ', 'ⴰⵖⵔⵓⴷ', 'P2131' },
				{ 'ⵉ ⵢⴰⵏ ', 'ⴰⴼⴰⵔⵙ ⴰⵎⴰⴷⴷⴰⵢ ⵓⵙⵜⵉⵢ ⵉ ⵢⴰⵏ', 'P2299' },
				{ 'ⵖⵓⵔ ⵜⴰⴽⵙⵍⵉ ⵏ ⵜⵥⴹⴰⵕⵜ ⵏ ⵜⵉⵙⵖⵉ', 'ⵖⵓⵔ ⵜⴰⴽⵙⵍⵉ ⵏ ⵜⵥⴹⴰⵕⵜ ⵏ ⵜⵉⵙⵖⵉ', 'P4010' },
				{ 'ⵜⴰⵙⴽⵯⴼⵍⵜ ', 'ⵜⴰⵙⴽⵯⴼⵍⵜ ⵏ ⵓⴼⴰⵔⵙ ⴰⵎⴰⴷⴷⴰⵢ ⵓⵙⵜⵉⵢ', 'nil' },
			}
			},
			{
			title = 'ⴰⴼⴰⵔⵙ ⴰⵎⴰⴷⴷⴰⵢ ⵓⵙⵜⵉⵢ ⴰⵙⵎⴰⵡⴰⵏ',
			properties = {
				{ 'ⴰⵙⵏⵉⵎⴰⵍ ', 'ⴰⵙⵏⵉⵎⴰⵍ ⵏ ⵓⴼⴰⵔⵙ ⴰⵎⴰⴷⴷⴰⵢ ⵓⵙⵜⵉⵢ ⴰⵙⵎⴰⵡⴰⵏ ⵉ ⵢⴰⵏ', 'nil' },
				{ 'ⵉ ⵢⴰⵏ ', 'ⴰⴼⴰⵔⵙ ⴰⵎⴰⴷⴷⴰⵢ ⵓⵙⵜⵉⵢ ⴰⵙⵎⴰⵡⴰⵏ ⵉ ⵢⴰⵏ', 'P2132' },
				{ 'ⵜⴰⵎⵏⵛⴽⵜ ⵏ ⵜⴳⵎⵉ ⵏ ⵓⴼⴰⵔⵙ ⴰⵎⴰⴷⴷⴰⵢ ⵓⵙⵜⵉⵢ ⴰⵙⵎⴰⵡⴰⵏ ⴰⵏⵉⵍⴰⵡ', 'ⵜⴰⵎⵏⵛⴽⵜ ⵏ ⵜⴳⵎⵉ ⵏ ⵓⴼⴰⵔⵙ ⴰⵎⴰⴷⴷⴰⵢ ⵓⵙⵜⵉⵢ ⴰⵙⵎⴰⵡⴰⵏ ⴰⵏⵉⵍⴰⵡ', 'P2219' },
				{ 'ⵜⴰⵙⴽⵯⴼⵍⵜ ', 'ⵜⴰⵙⴽⵯⴼⵍⵜ ⵏ ⵓⴼⴰⵔⵙ ⴰⵎⴰⴷⴷⴰⵢ ⵓⵙⵜⵉⵢ ⴰⵙⵎⴰⵡⴰⵏ ⵉ ⵢⴰⵏ', 'P2132' },
			}
		},
		{
			title = 'ⵜⴰⴷⴰⵎⵙⴰ',
			properties = {
				{ 'ⴰⴷⵔⵉⵎ', 'ⴰⴷⵔⵉⵎ', 'P38' },
				{ 'ⵜⵉⴳⵎⵉⴹⵉ ⵏ ⵓⴼⵓⴽⵜⵉ', 'ⵜⵉⴳⵎⵉⴹⵉ ⵏ ⵓⴼⵓⴽⵜⵉ', 'P1279' },
				{ 'ⴰⵎⵙⵎⵓⵏ ⵏ ⵜⵓⵛⵛⵓⵜⵉⵏ', 'ⴰⵎⵙⵎⵓⵏ ⵏ ⵜⵓⵛⵛⵓⵜⵉⵏ', 'P2139' },
				{ 'ⵜⵓⵛⵛⵓⵜ ⵏ ⵓⵔⴳⴰⵢ', 'ⵜⵓⵛⵛⵓⵜ ⵏ ⵓⵔⴳⴰⵢ', 'P3087' },
				{ 'ⴰⵎⵙⵎⵓⵏ ⵏ ⵓⵕⵟⵟⴰⵍ', 'ⴰⵎⵙⵎⵓⵏ ⵏ ⵓⵕⵟⵟⴰⵍ', 'P2133' },
				{ 'ⴰⵎⵙⵎⵓⵏ ⵏ ⵓⵣⴰⵍ ⴰⵥⵕⴼⴰⵏ', 'ⴰⵎⵙⵎⵓⵏ ⵏ ⵓⵣⴰⵍ ⴰⵥⵕⴼⴰⵏ', 'P2137' },
				{ 'ⴰⵎⵙⵎⵓⵏ ⵏ ⵓⵖⴹⴰⴼⴰⵏ', 'ⴰⵎⵙⵎⵓⵏ ⵏ ⵓⵖⴹⴰⴼⴰⵏ', 'P2134' },
				{ 'ⵜⵉⴳⵎⵉⴹⵉ ⵏ ⵓⵔⴳⴰⵢ ⵓⴼⵔⵉⴷ', 'ⵜⵉⴳⵎⵉⴹⵉ ⵏ ⵓⵔⴳⴰⵢ ⵓⴼⵔⵉⴷ', 'P2834' },
				{ 'ⴰⵡⵜⴰⵢ ⵏ ⵜⴰⵏⴰⴼⵜ', 'ⴰⵡⵜⴰⵢ ⵏ ⵜⴰⵏⴰⴼⵜ', 'P3001' },
				{ 'ⴰⴱⴰⵏⴽ ⴰⵏⴰⵎⵎⴰⵙ', 'ⴰⴱⴰⵏⴽ ⴰⵏⴰⵎⵎⴰⵙ', 'P1304' },
				{ 'ⵜⴰⵎⵏⵛⴽⵜ ⵏ ⵜⵢⵓⵙⵉ ⵜⴰⵡⵍⴳⴰⵏⵜ (VAT)', 'ⵜⴰⵎⵏⵛⴽⵜ ⵏ ⵜⵢⵓⵙⵉ ⵜⴰⵡⵍⴳⴰⵏⵜ (VAT)', 'P2855' },
				{ 'ⴰⵎⵙⵎⵓⵏ ⵏ ⵓⴽⵛⵉⵎⵏ', 'ⴰⵎⵙⵎⵓⵏ ⵏ ⵓⴽⵛⵉⵎⵏ', 'P2136' },
				{ 'ⴰⵎⵙⵎⵓⵏ ⵏ ⵉⵙⵉⴼⴹⵏ', 'ⴰⵎⵙⵎⵓⵏ ⵏ ⵉⵙⵉⴼⴹⵏ', 'P2135' },
			}
		},
		{
			title = 'ⵜⴰⵎⴰⵜⴰⵔⵜ ⵏ ⵜⵏⴼⵍⵉⵜ ⵜⴰⵏⴼⴳⴰⵏⵜ',
			properties = {
				{ 'ⴰⵏⵎⵎⴰⵍ', 'ⵜⴰⵎⴰⵜⴰⵔⵜ ⵏ ⵜⵏⴼⵍⵉⵜ ⵜⴰⵏⴼⴳⴰⵏⵜ', 'P1081' },
				{ 'ⵜⴰⵙⴽⵯⴼⵍⵜ', 'ⵜⴰⵙⴽⵯⴼⵍⵜ', 'P2295' },
				{ 'ⴰⵏⴰⵎⵎⴰⵙ ⵏ ⵜⵏⵓⴷⴰⴼ', 'ⴰⵏⴰⵎⵎⴰⵙ ⵏ ⵜⵏⵓⴷⴰⴼ', 'P3529' },
				{ 'ⵜⵉⴳⵎⵉⴹⵉ ⵏ ⵜⴰⵔⵡⵓⵔⵉ', 'ⵜⵉⴳⵎⵉⴹⵉ ⵏ ⵜⴰⵔⵡⵓⵔⵉ', 'P1198' },
			}
		},
		{
			title = 'ⵜⴰⵎⴰⵜⴰⵔⵜ ⵏ ⵊⵉⵏⵉ',
			properties = {
				{ 'ⴰⵏⵎⵎⴰⵍ', 'ⴰⵏⵎⵎⴰⵍ', 'P1125' },
				{ 'ⵜⴰⵙⴽⵯⴼⵍⵜ', 'ⵜⴰⵙⴽⵯⴼⵍⵜ ⵏ ⵊⵉⵏⵉ', nil },
			}
		},
		{
			title = 'ⵜⵉⵏⴳⴰⵍⵉⵏ ⵏ ⵜⵎⵓⵔⵜ',
			properties = {
				{ 'ⵜⴰⵏⴳⴰⵍⵜ ⵏ ⵓⵜⵉⵍⵉⴼⵓⵏ', 'ⵜⴰⵏⴳⴰⵍⵜ ⵏ ⵓⵜⵉⵍⵉⴼⵓⵏ', 'P474' },
				{ 'ⵓⵟⵟⵓⵏ ⵏ ⵓⵜⵉⵍⵉⴼⵓⵏ ⵏ ⵜⵓⵥⵡⴰⵢⵜ', 'ⵓⵟⵟⵓⵏ ⵏ ⵓⵜⵉⵍⵉⴼⵓⵏ ⵏ ⵜⵓⵥⵡⴰⵢⵜ', 'P2852' },
				{ 'ⵜⴰⵏⴳⴰⵍⵜ ⵏ ⵜⵎⵓⵔⵜ', 'ⵜⴰⵏⴳⴰⵍⵜ ⵏ ⵜⵎⵓⵔⵜ', 'P297' },
				{ 'ⵉⵏⵉⴳⵍ ⵏ ⵓⵙⴰⵡⴰⵢ', 'ⵉⵏⵉⴳⵍ ⵏ ⵓⵙⴰⵡⴰⵢ', 'P281' },
				{ 'ⵜⴰⵎⴰⵜⴰⵔⵜ ⵏ ⵢⵓⵏⵉⴽⵓⴷ', 'ⵜⴰⵎⴰⵜⴰⵔⵜ ⵏ ⵢⵓⵏⵉⴽⵓⴷ', 'P487'},
				{ 'ⵓⵟⵟⵓⵏⴻⵏ ⵏ ⵓⵙⵉⵙⵙⵏ ⴰⵢⵍⴰⵏ', 'ⵓⵟⵟⵓⵏⴻⵏ ⵏ ⵓⵙⵉⵙⵙⵏ ⴰⵢⵍⴰⵏ', 'P2979' },
				{ 'ⴰⴳⵍⴰ ⵏ ⵡⴰⵏⵜⵉⵔⵏⵉⵜ', 'ⴰⴳⵍⴰ ⵏ ⵡⴰⵏⵜⵉⵔⵏⵉⵜ', 'P78' },
			}
		},
		{
			title = 'ⵉⵙⴼⴽⴰ ⵢⴰⴹⵏ',
			properties = {
				{ 'ⵜⴰⵙⴳⴰ ⵏ ⵓⵏⴷⴰⵀ', 'ⵜⴰⵙⴳⴰ ⵏ ⵓⵏⴷⴰⵀ', 'P1622' },
				{ 'ⵜⴰⵙⴳⴰ ⵏ ⵜⵡⴰⴷⴰ ⵏ ⵓⵍⴰⵡⴰⵢ', 'ⵜⴰⵙⴳⴰ ⵏ ⵜⵡⴰⴷⴰ ⵏ ⵓⵍⴰⵡⴰⵢ', 'P5658' },
				{ 'ⵉⵏⵉⴳⵍ ⴰⵎⴰⴷⴷⵓⴷ', 'ⵉⵏⵉⴳⵍ ⴰⵎⴰⴷⴷⵓⴷ', 'P2238' },
				{ 'ⴰⵏⵙⴰ ⵏ ⵓⵡⵉⴱ ⵓⵏⵚⵉⴱ', 'ⴰⵏⵙⴰ ⵏ ⵓⵡⵉⴱ ⵓⵏⵚⵉⴱ', 'P856' },
				{ 'ⴰⴳⵎⵎⴰⴹ ⴰⴽⵓⴷⴰⵏ', 'ⴰⴳⵎⵎⴰⴹ ⴰⴽⵓⴷⴰⵏ', 'P421' },
			}
		}
	}

	for _, section in ipairs(sections) do
		local sectionContent = mw.html.create('')
		local hasData = false

		for _, prop in ipairs(section.properties) do
			local label, param, pid = unpack(prop)
			local value, fromWikidata = getValueOrWikidata(args, param, pid)

			if pid == 'P37' and entity then
				local stmts = entity:getBestStatements('P37')
				if #stmts > 1 then
					label = 'ⵜⵓⵜⵍⴰⵢⵉⵏ ⵜⵓⵏⵚⵉⴱⵉⵏ'
				else
					label = 'ⵜⵓⵜⵍⴰⵢⵜ ⵜⵓⵏⵚⵉⴱⵜ'
				end
			end
			
			if param == 'ⴰⵙⵏⵉⵎⴰⵍ ⵏ ⵓⴼⴰⵔⵙ ⴰⵎⴰⴷⴷⴰⵢ ⵓⵙⵜⵉⵢ' and not value and entityId then
				local v, _ = makeRequest(entityId, 'P2131')
				if v then value = v:match('___YEAR:(%d+)___') end
			end
			if param == 'ⴰⵙⵏⵉⵎⴰⵍ ⵏ ⵓⴼⴰⵔⵙ ⴰⵎⴰⴷⴷⴰⵢ ⵓⵙⵜⵉⵢ ⴰⵙⵎⴰⵡⴰⵏ ⵉ ⵢⴰⵏ' and not value and entityId then
				local v, _ = makeRequest(entityId, 'P2132')
				if v then value = v:match('___YEAR:(%d+)___') end
			end

			if value then
				hasData = true

				if pid == 'P2852' or pid == 'P2979' then
					if value:find(',') or value:find('mw%-collapsible') then
						label = label:gsub('^ⵓⵟⵟⵓⵏ%s', 'ⵓⵟⵟⵓⵏⴻⵏ ')
					else
						label = label:gsub('^ⵓⵟⵟⵓⵏⴻⵏ%s', 'ⵓⵟⵟⵓⵏ ')
					end
				end
				
				if param == 'ⴰⵙⴳⴷ' and value then
					local y = value:match('___YEAR:(%d+)___')
					if y then
						label = label .. ' <small>(' .. y .. ')</small>'
						value = value:gsub('___YEAR:%d+___', '')
					end
				end

				
				if value and value:find('___YEAR:') then
					value = value:gsub('___YEAR:%d+___', '')
				end

				local row = sectionContent:tag('tr')
				row:tag('th')
					:wikitext(label)
					:css({
						['text-align'] = 'left',
						['width'] = '50%',
						['padding-right'] = '10px',
						['font-weight'] = 'normal',
						['background-color'] = '#F3F3F3',
						['vertical-align'] = 'top'
					})
				
				local cell = row:tag('td'):css({['padding'] = '2px', ['width'] = '50%'})
				
				if param == 'ⵜⴰⵏⵥⵥⵉ' then
					value = value .. ' ⵣⴷⵖ/ⴽⵎ²'
				end

				if param == 'ⵜⴰⵊⵓⵎⵎⴰ' then
					value = value .. ' ⴽⵎ²'
				end

				if param == 'ⵉⵏⵉⴳⵍ ⴰⵊⵢⵓⴳⵕⴰⴼⵉ' then
					value = '[https://www.geonames.org/' .. value .. ' ' .. value .. ']'
				end

				if param == 'ⵜⵉⴳⵎⵉⴹⵉ ⵏ ⵜⴰⵔⵡⵓⵔⵉ' or param == 'ⵜⵉⴳⵎⵉⴹⵉ ⵏ ⵓⴼⵓⴽⵜⵉ' or param == 'ⵜⴰⵎⵏⵛⴽⵜ ⵏ ⵜⵢⵓⵙⵉ ⵜⴰⵡⵍⴳⴰⵏⵜ (VAT)' or param == 'ⵜⵉⴳⵎⵉⴹⵉ ⵏ ⵓⵔⴳⴰⵢ ⵓⴼⵔⵉⴷ' then
					if value:find("<small>") then
						value = value:gsub(" (<small>)", "%% %1")
					else
						value = value .. '%'
					end
				end

				if param == 'ⴰⵏⴰⵎⵎⴰⵙ ⵏ ⵓⵡⵜⴰⵢ' or param == 'ⴰⵡⵜⴰⵢ ⵏ ⵜⴰⵏⴰⴼⵜ' or param == 'ⴰⵡⵜⴰⵢ ⵓⵍⴳⵉⵏ' then
					if value:find('<small>') then
						value = value:gsub('(<small>)', ' ⵏ ⵓⵙⴳⴳⵯⴰⵙ %1')
					else
						value = value .. ' ⵏ ⵓⵙⴳⴳⵯⴰⵙ'
					end
				end
				
				if fromWikidata and pid and entityId then
					cell:wikitext(value .. makeEditIcon(entityId, pid))
				else
					cell:wikitext(value)
				end

				if param == 'ⵓⵟⵟⵓⵏ ⵏ ⵉⵎⵣⴷⴰⵖⵏ' and entityId then
					local maleCount, _ = makeRequest(entityId, 'P1540')
					local femaleCount, _ = makeRequest(entityId, 'P1539')
					
					if maleCount or femaleCount then
						local collapseContainer = mw.html.create('div')
						collapseContainer:addClass('mw-collapsible mw-collapsed')
						
						local header = collapseContainer:tag('div')
							:css('font-weight', 'bold')
							:css('font-size', '90%')
							:wikitext('ⵙ ⵓⵔⵉⵙ') 

						local content = collapseContainer:tag('div'):addClass('mw-collapsible-content')
						local ul = content:tag('ul')
						
						if maleCount then
							ul:tag('li'):wikitext('ⵉⵡⵜⵎⴰⵏ: ' .. maleCount .. makeEditIcon(entityId, 'P1540'))
						end
						if femaleCount then
							ul:tag('li'):wikitext('ⵜⵉⵡⵜⵎⵉⵏ: ' .. femaleCount .. makeEditIcon(entityId, 'P1539'))
						end
						
						cell:node(collapseContainer)
					end
				end
			end
		end

		if hasData then
			infobox:tag('tr'):tag('th')
				:attr('colspan', 2)
				:css(sectionHeaderStyle)
				:wikitext(section.title)
			infobox:node(sectionContent)
		end
	end

	if entityId then
		local lat, lon = getCoordinates(entity)
		if lat and lon then
			local mapFeatures = {}

			table.insert(mapFeatures, {
				type = "ExternalData",
				service = "geoshape",
				ids = entityId,
				properties = {
					["stroke-width"] = 2,
					stroke = "#FF0000",
					["fill-opacity"] = 0.2
				}
			})

			table.insert(mapFeatures, {
				type = "Feature",
				geometry = {
					type = "Point",
					coordinates = { lon, lat }
				},
				properties = {
					--["marker-symbol"] = "star",
					["marker-color"] = "#016DFF",
					["marker-size"] = "small"
				}
			})

			local mapFrame = frame:extensionTag{
				name = 'mapframe',
				content = mw.text.jsonEncode(mapFeatures),
				args = {
					width = "290",
					height = "190",
					zoom = "2",
					align = "center",
					frameless = "frameless"
				}
			}
			
			infobox:tag('tr'):tag('th')
				:attr('colspan', 2)
				:css(sectionHeaderStyle)
				:wikitext('ⴰⴷⵖⴰⵔ ⴰⵔⴰⴽⴰⵍ') 
			
			infobox:tag('tr'):tag('td')
				:attr('colspan', 2)
				:css('text-align', 'center')
				:wikitext(mapFrame)
		end
	end

	local separatorRow = infobox:tag('tr')
	local separatorCell = separatorRow:tag('td')
		:attr('colspan', 2)
		:css({
			['padding'] = '0',
			['border-top'] = '2px dashed #DDD',
		})
	separatorCell:tag('div'):css({['height'] = '1px', ['background-color'] = 'transparent'})

	local currentTitle = mw.title.getCurrentTitle()
	local editSourceUrl = currentTitle:fullUrl({action='edit', section='0'})
	local editVisualUrl = currentTitle:fullUrl({veaction='edit'})
	local templateDocUrl = mw.title.new('ⴰⵍⴱⵓⴹ:ⵜⴰⴼⵍⵓⵖⵎⵉⵙⵜ ⵜⴰⵎⵓⵔⵜ'):fullUrl()

	local footerRow = infobox:tag('tr')
	local footerCell = footerRow:tag('td')
		:attr('colspan', 2)
		:css({
			['padding'] = '0.2em',
			['font-size'] = '85%',
			['text-align'] = 'left',
			['background-color'] = '#f8f8f8',
		})

	local footerContainer = mw.html.create('div')
		:css({
			['display'] = 'flex',
			['justify-content'] = 'space-between',
			['align-items'] = 'center',
		})

	local editLinksSpan = footerContainer:tag('span'):addClass('plainlinks')
	if entityId and entityId ~= '' then
		local wikidataUrl = 'https://www.wikidata.org/wiki/Special:EntityPage/' .. entityId
		editLinksSpan:wikitext('[' .. editSourceUrl .. ' <span style="color:#002bb8;">ⵙⵏⴼⵍ ⴰⵙⴰⴳⵎ</span>] - [' ..
			editVisualUrl .. ' <span style="color:#002bb8;">ⵙⵏⴼⵍ</span>] - [' ..
			wikidataUrl .. ' <span style="color:#002bb8;">ⵡⵉⴽⵉⴷⴰⵜⴰ</span>]')
	else
		editLinksSpan:wikitext('[' .. editSourceUrl .. ' <span style="color:#002bb8;">ⵙⵏⴼⵍ ⴰⵙⴰⴳⵎ</span>] - [' ..
			editVisualUrl .. ' <span style="color:#002bb8;">ⵙⵏⴼⵍ</span>]')
	end

	footerContainer:tag('span')
		:css({['float'] = 'right', ['margin-left'] = '5px'})
		:wikitext('[[ⴰⴼⴰⵢⵍⵓ:Info Simple.svg|15px|ⵥⵕ ⵓⴳⴳⴰⵔ ⵅⴼ ⵡⴰⵍⴱⵓⴹ ⴰⴷ |link=' .. templateDocUrl .. ']]')

	footerCell:node(footerContainer)

	return tostring(infobox)
end

return p