local m_ja = require("Module:ja")
local kata_to_hira = m_ja.kata_to_hira
local normalize_kana = m_ja.normalize_kana

local data = mw.loadData("Module:Hrkt-sortkey/data")

local export = {}

-- Generate a sortkey from a kana input. This can be any combination of hiragana and katakana, and will not be normalized to hiragana or katakana first. Use [[Module:Hira-sortkey]] or [[Module:Kana-sortkey]] if that is required, which in turn call this module.
function export.makeSortKey(text, lang, sc)
	-- Normalize long vowel and iteration marks.
	text = normalize_kana(text)
	-- Middle dots and double hyphens become spaces.
	text = text:gsub("\227[\130\131][\160\187]", data.spaces)
	-- For each dakuten and handakuten, remove it and add 1 or 2 apostrophes to the end (respectively).
	local apos = 0
	text = text:gsub("\227\130[\153\154]", function(char)
		apos = apos + data.voicing[char]
		return ""
	end)
	return apos > 0 and (text .. ("'"):rep(apos)) or text
end

return export