local export = {}
local PAGENAME = mw.title.getCurrentTitle().text

local m_IPA = require("Module:IPA")
local lang = require("Module:languages").getByCode("mch")

-- single characters that map to IPA sounds
local phonetic_chars_map = {
	["m"] = "m",
	["n"] = "n",
	["ñ"] = "ɲ",
	["t"] = "t",
	["k"] = "k",
	["'"] = "ʔ",
	["s"] = "s",
	["j"] = "h",
	["d"] = "ɾ",
	["y"] = "j",
	["w"] = "w",
	["a"] = "a",
	["e"] = "e",
	["i"] = "i",
	["o"] = "o",
	["u"] = "u",
	["ö"] = "ə",
	["ü"] = "ɨ",
}

-- character sequences of two that map to IPA sounds
local phonetic_2chars_map = {
	["n'"] = "ŋ",
	["ch"] = "c",
	["kw"] = "kʷ",
	["sh"] = "ʃ",
	["jh"] = "ç",
	["dh"] = "ð",
	["aa"] = "aː",
	["ee"] = "eː",
	["ii"] = "iː",
	["oo"] = "oː",
	["uu"] = "uː",
	["öö"] = "əː",
	["üü"] = "ɨː",
}

-- character sequences of three that map to IPA sounds
local phonetic_3chars_map = {
	["cch"] = "tc",
	["ssh"] = "ʃʃ",
	["jjh"] = "çç",
}

function export.to_IPA(word)
	word = mw.ustring.lower(word)

	local phonetic = word

	for pat, repl in pairs(phonetic_3chars_map) do
		phonetic = phonetic:gsub(pat, repl)
	end

	for pat, repl in pairs(phonetic_2chars_map) do
		phonetic = phonetic:gsub(pat, repl)
	end

	phonetic = mw.ustring.gsub(phonetic, '.', phonetic_chars_map)

	-- assimilation and word-initial allophones
	phonetic = mw.ustring.gsub(phonetic, "%f[%a]([aeiouəɨ])", "q%1")
	phonetic = mw.ustring.gsub(phonetic, "([^aeiouəɨ][aeiouəɨ][mnɲŋtckʔsçʃðhɾjw]ʷ?[aeiouəɨ])([^ʔŋː%s%-][^mnɲŋtckʔsçʃðhɾjw])", "%1ː%2", 1)
	phonetic = mw.ustring.gsub(phonetic, "([^aeiouəɨ][aeiouəɨ][mnɲŋtckʔsçʃðhɾjw]ʷ?[aeiouəɨ])([aeoəɨ])", "%1ː%2")
	phonetic = mw.ustring.gsub(phonetic, "([^aeiouəɨ][aeiouəɨ][mnɲŋtckʔsçʃðhɾjw]ʷ?[aeiouəɨ])([^ʔŋː%s%-][^mnɲŋtckʔsçʃðhɾjw])", "%1ː%2", 1)
	phonetic = mw.ustring.gsub(phonetic, "([^aeiouəɨ][aeiouəɨ][mnɲŋtckʔsçʃðhɾjw]ʷ?[aeiouəɨ])([^ʔŋː%s%-][^mnɲŋtckʔsçʃðhɾjw])", "%1ː%2", 1)
	phonetic = mw.ustring.gsub(phonetic, "c", "t͡ʃ")
	phonetic = mw.ustring.gsub(phonetic, "ɾ", "ɾ̠")
	phonetic = mw.ustring.gsub(phonetic, "q", "")

	phonetic = mw.ustring.gsub(phonetic, "n([kw]+)", "ŋ%1")
	phonetic = mw.ustring.gsub(phonetic, "[mnɲŋ]h", "mm̥")
	phonetic = mw.ustring.gsub(phonetic, "([ij]ʔ?ː?)h", "%1ç")
	phonetic = mw.ustring.gsub(phonetic, "%f[%a%-]h", "hʷ")
	phonetic = mw.ustring.gsub(phonetic, "%f[%a%-]ɾ̠", "d")
	phonetic = mw.ustring.gsub(phonetic, "ʔɾ̠", "ʔd")
	phonetic = mw.ustring.gsub(phonetic, "ɾ̠d", "dd")
	phonetic = mw.ustring.gsub(phonetic, "%f[%a%-]j", "ɟ")
	phonetic = mw.ustring.gsub(phonetic, "ʔj", "ʔɟ")
	phonetic = mw.ustring.gsub(phonetic, "([aeiouəɨː])i", "%1j")
	phonetic = mw.ustring.gsub(phonetic, "([aeiouəɨː])u", "%1w")
	phonetic = mw.ustring.gsub(phonetic, "([aeiouəɨː])wi", "%1vi")
	phonetic = mw.ustring.gsub(phonetic, "wo", "βo")
	phonetic = mw.ustring.gsub(phonetic, "wɾ̠", "wɾ̠ʷ")
	phonetic = mw.ustring.gsub(phonetic, "([ʔouw]ː?)h", "%1hʷ")
	phonetic = mw.ustring.gsub(phonetic, "ŋ([aeiouəɨ])", "ŋŋ%1")

	return "[" .. phonetic .. "]"
end

function export.pronunciation(word)
	if type(word) == "table" then
		word = word.args[1] or word:getParent().args[1]
	end
	if not word or (word == "") then
		word = PAGENAME
	end
	local items = {}
	table.insert(items, {pron = export.to_IPA(word), note = nil})
	return m_IPA.format_IPA_full { lang = lang, items = items }
end

return export