local export = {}
local u = mw.ustring.char
local a, b, c, d, e = u(0xF000), u(0xF001), u(0xF002), u(0xF003), u(0xF004)

local oneChar = {
	["ё"] = "е" .. a
}

local twoChars = {
	["аь"] = "а" .. a, ["гъ"] = "г" .. a, ["гь"] = "г" .. b, ["кк"] = "к" .. a, ["къ"] = "к" .. b, ["кь"] = "к" .. c, ["кӏ"] = "к" .. d, ["оь"] = "о" .. a, ["пп"] = "п" .. a, ["пӏ"] = "п" .. b, ["сс"] = "с" .. a, ["тт"] = "т" .. a, ["тӏ"] = "т" .. b, ["хх"] = "х" .. a, ["хъ"] = "х" .. b, ["хь"] = "х" .. c, ["хӏ"] = "х" .. e, ["цц"] = "ц" .. a, ["цӏ"] = "ц" .. b, ["чч"] = "ч" .. a, ["чӏ"] = "ч" .. b
}

local fourChars = {
	["хьхь"] = "х" .. d
}

function export.makeSortKey(text, lang, sc)
	text = mw.ustring.lower(text)
	
	for from, to in pairs(fourChars) do
		text = mw.ustring.gsub(text, from, to)
	end
	
	for from, to in pairs(twoChars) do
		text = mw.ustring.gsub(text, from, to)
	end
	
	return mw.ustring.upper(mw.ustring.gsub(text, ".", oneChar))
end

return export