ХӀокху модулах лаьцна хааман Модуль:Wikidata/P166/doc агӀо кхолла мега

-- https://www.mediawiki.org/wiki/Wikibase/DataModel
-- https://www.wikidata.org/wiki/Wikidata:Glossary/ru
-- https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua
-- https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual/ru

local p = {}

local function getMedalsOld(entity)
	local result = {}
	local medalsStr = entity:formatPropertyValues('P166').value
	
	for str in string.gmatch(medalsStr, '([А-яёЁ0-9с-я%.%-— ]+),?%s?') do
		if str ~= 'неизвестное значение' and str ~= 'значение не задано' then
			table.insert(result, str)
		end
	end

	return result
end

local function formattedTableOfMedals(medals, frame)
	local result = '{|  style="background:transparent"\n'
	
	for i, medal in ipairs(medals) do
		local title = mw.title.new('Кеп:' .. medal)
		if title then
			if title.exists then
				result = result .. '|' .. frame:expandTemplate{ title = medal, args = {} } .. '\n'
			else
				result = result .. '| [[Кеп:' .. medal .. ']]\n'
			end
		end
		if math.fmod(i, 4) == 0 then
			result = result .. '|-\n'
		end
	end
	return result .. '|}'
end

local function getMedals(frame)
	local result = {}
	local entity = mw.wikibase.getEntityObject()
	local medalsStr = entity:formatPropertyValues('P1668').value
	return medalsStr
	--[[
	for key, value in pairs(frame.args) do
		table.insert(result, value)
	end
	return {
		src   = 0,
		items = result
	}
	]]
end

local function sortMedals(medals)
	local result = {
		src = medals.src,
		items = {}
	}
	if medals.src == 0 then
		result.items = {
			medals = medals.items,
			orders = {},
			others = {}
		}
	else
		local m_medals, m_orders, m_others = {}, {}, {}
		for i = 1, #medals.items do
			if string.find(medals.items[i], '[Мм]едаль') ~= nil then
				table.insert(m_medals, medals.items[i])
			end
			if string.find(medals.items[i], '[Оо]рден') ~= nil then
				table.insert(m_orders, medals.items[i])
			else
				table.insert(m_others, medals.items[i])
			end
		end
		result.items = {
			medals = m_medals,
			orders = m_orders,
			others = m_others
		}
	end
	return result
end

local function printOneTypeMedal(medals)
	local str = ''
	for i = 1, #medals do
		if string.len(medals[i]) ~= 0 then
			str = str .. '|' .. medals[i] .. '\n'
			if math.fmod(i, 4) == 0 then
				str = str .. '|-\n'
			end
		end
	end
	return str .. '|-\n';
end

local function formattedMedals(medals)
	local startStr, str, endStr = '{|  style="background:transparent"\n', '', '|}'
	
	str = str .. printOneTypeMedal(medals.items.medals)
	str = str .. printOneTypeMedal(medals.items.orders)
	str = str .. printOneTypeMedal(medals.items.others)

	return startStr .. str .. endStr
end

function p.display(frame)
	local medals = getMedals(frame)
	return '0' .. medals .. '0'
	--local sortedMedals = sortMedals(medals)
	--return formattedMedals(sortedMedals)
	
	
	
	--return table.concat(medals, ', ')
	--local entity = mw.wikibase.getEntityObject()
	--return formattedTableOfMedals(getMedalsOld(entity), frame)
end

return p