Документация
local p = {}

local brackets = "%[%[(.+)%]%]"

function p._debracket(s)
	return mw.ustring.match(s, brackets) or s
end

local countries = nil
local codes = nil

local function true_country_key(country)
	countries = countries or require('Модуль:CountryCases/data/песочница').countries
	if countries[country] ~= nil then
		return country
	end
	codes = codes or require('Модуль:CountryCases/data/песочница').codes
	return codes[country] or nil
end

function p._genitive(frame, s)
	local country = p._debracket(s)
	local true_key = true_country_key(country)
	countries = countries or require('Модуль:CountryCases/data/песочница').countries
	if true_key and countries[true_key] ~= nil and countries[true_key].gen ~= nil then
		return countries[true_key].gen
	end
	codes = codes or require('Модуль:CountryCases/data/песочница').codes
	local country_by_code = codes[country]
	if country_by_code ~= nil and countries[country_by_code] ~= nil and countries[country_by_code].gen ~= nil then
		return countries[country_by_code].gen
	end
	return frame:callParserFunction('#ifexist', {country, country})
end

function p._locative(frame, s)
	local country = p._debracket(s)
	local true_key = true_country_key(country)
	countries = countries or require('Модуль:CountryCases/data/песочница').countries
	if true_key and countries[true_key] ~= nil then
		return countries[true_key].loc
	end
	local ru = require('Модуль:Ru')
	return 'в ' .. ru.locative(frame)
end

function p._nominative(frame, s)
	local country = p._debracket(s)
	return true_country_key(country) or ''
end

local function wrapper(frame, func_name)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	local args = getArgs(frame)
	return p[func_name](frame, args[1])
end

function p.genitive(frame)
	return wrapper(frame, '_genitive')
end

function p.locative(frame)
	return wrapper(frame, '_locative')
end

function p.nominative(frame)
	return wrapper(frame, '_nominative')
end

return p