Ҳуҷҷатгузорӣ
local p = {}

local function FormatWhereLinkFromCatName(cat_link_text, sitelink)
	local prep, linktext = string.match(cat_link_text, '^Гурӯҳ:[^ ]+ ([^ ]+) (.+)$');
	
	if sitelink then
		return prep .. ' ' .. "[[" .. sitelink .. "|" .. linktext .. "]]";
	else
		return prep .. ' ' .. linktext;
	end
end

local function MakeWherePlace(place, propertyOfProperty)
	if place.mainsnak.datavalue then -- если значение свойства "значение неизвестно", то datavalue nil
		local id = 'Q' .. place.mainsnak.datavalue.value['numeric-id']
		local placeEntity = mw.wikibase.getEntity(id)
		local categories = placeEntity.claims[propertyOfProperty]
		if categories ~= nil then
			local sitelink = mw.wikibase.sitelink(id)
			local cat_qid = 'Q' .. categories[1].mainsnak.datavalue.value['numeric-id']
			local cat_link_text = mw.wikibase.label(cat_qid)
			return FormatWhereLinkFromCatName(cat_link_text, sitelink)
		end
	end
	return nil
end

local function MakeWherePlaces(places, propertyOfProperty)
	local res = ''
	
	for i, place in pairs(places) do
		if i > 1 then
			if next(places, _) == nil then
			    res = res .. ' и ';
			else
			    res = res .. ', ';
			end
		end
		
		local text = MakeWherePlace(place, propertyOfProperty)
		
		if text then
			res = res .. text
		end
	end
    return res
end

function p.mainFunction(frame)
	local args = {}
	if frame == mw.getCurrentFrame() then
		args = frame.args;
	else
		args = frame
	end

	local result = ''
	local entity = mw.wikibase.getEntity()
	
	if entity ~= nil and entity.claims ~= nil then
		local countries = entity.claims[ args[1] ]
		if countries ~= nil then
			result = result .. MakeWherePlaces(countries, args[2])
		end
	end
	return result
end
 
return p