local export = {}

local m_IPA = require("Module:IPA")
local lang = require("Module:languages").getByCode("xsl")
local rsub = mw.ustring.gsub
local rlower = mw.ustring.lower

local C = "ʰ?ⁿ?ᵐ?[ðθbdʒʃɣtkpsɬɮxwɾjmnzh]ʰ?ʼ?"
local V = "[aeiouɛɪ]M?L?H?"

local di = {
	["dh"]="ð",		["th"]="θ",		["mb"]="ᵐB",	["nd"]="ⁿd",
	["dz"]="ds",	["ts"]="dsʰ",	["dl"]="dɬ",	["tl"]="dɬʰ",
	["ch"]="dʃʰ",	["zh"]="ʒ",		["sh"]="ʃ",		["gh"]="ɣ",
}

local mo = {
	["d"]="t",		["g"]="k",		["b"]="p",		["j"]="tʃ",
	["t"]="tʰ",		["k"]="kʰ",		["ɂ"]="ʔ",		["h"]="h",
	["s"]="s",		["ł"]="ɬ",		["l"]="ɮ",		["x"]="x",
	["w"]="w",		["r"]="ɾ",		["y"]="j",		["ʼ"]="ʼ",
	["m"]="m",		["n"]="n",		["z"]="z",		["B"]="b",
	["i"]="iL",		["e"]="eL",		["a"]="aL",		["o"]="oL",
	["u"]="uL",
	["í"]="iH",		["é"]="eH",		["á"]="aH",		["ó"]="oH",
	["ú"]="uH",
	["į"]="iML",    ["ę"]="eML",	["ą"]="aML",	["ǫ"]="oML",
	["ų"]="uML",
	["I"]="i",		["E"]="e",		["A"]="a",		["O"]="o",
	["U"]="u",
	["-"]="D"
}

local function phonetic(text)
	text = rlower(text)
	if mw.ustring.find(text, "~") == nil then
		text = "~" .. text
	end
	-- double diacritics
	text = rsub(text, "į́", "IMH")
	text = rsub(text, "ę́", "EMH")
	text = rsub(text, "ą́", "AMH")
	text = rsub(text, "ǫ́", "OMH")
	text = rsub(text, "ų́", "UMH")
	-- digraphs
	local i = 0
	while i <= 10 do
		text = rsub(text, "..", di)
		text = rsub("Q" .. text, "..", di)
		text = rsub(text, "Q", "")
		i = i + 1
	end
	-- monographs
	text = rsub(text, ".", mo)
	text = rsub(text, "tʰθ", "tθʰ")
	text = rsub(text, "ʰʼ", "ʼ")
	-- pre-aspiration
	text = rsub(text, "h(" .. C .. ")", "ʰ%1")
	-- syllable splits (twice?)
	text = rsub(text, "(" .. V .. ")(" .. V .. ")", "%1.%2")
	text = rsub(text, "(" .. V .. ")([ʰⁿᵐʔðθbdʒʃɣtkpsɬɮxwɾjmnzhʰʼ]*" .. V .. ")", "%1.%2")
	text = rsub(text, "(" .. V .. ")(" .. V .. ")", "%1.%2")
	text = rsub(text, "(" .. V .. ")([ʰⁿᵐʔðθbdʒʃɣtkpsɬɮxwɾjmnzhʰʼ]*" .. V .. ")", "%1.%2")
	-- /e/
	text = rsub(text, "%~([ʰⁿᵐʔðθbdʒʃɣtkpsɬɮxwɾjmnzhʰʼaeiouɛɪMHL%.]*)i(M?L?H?)%.", "~%1ɪ%2.")
	text = rsub(text, "e", "ɛ")
	text = rsub(text, "ɛ(M?L?H?%~[ʰⁿᵐʔðθbdʒʃɣtkpsɬɮxwɾjmnzhʰʼ]*[iu]M?L?H?)", "e%1")
	text = rsub(text, "([ʒj])ɛ", "%1e")
	text = rsub(text, "eM", "ɛM")
	-- /i/
	text = rsub(text, "ɪM", "iM")
	-- vowel thingies
	text = rsub(text, "%~", ".")
	text = rsub(text, "([aeiouɛɪ])(M?)L?H?%.%1(M?L?H?)", "%1%2%3ː")
	text = rsub(text, "(" .. V .. ")$", "%1(ʔ)")
	-- /o/
	text = rsub(text, "o", "o̞")
	text = rsub(text, "MM", "M")
	text = rsub(text, "M", "̃")
	text = rsub(text, "L", "̀")
	text = rsub(text, "H", "́")
	text = rsub(text, "t([θsʃɬ])", "t͡%1")
	text = rsub(text, "^%.", "")
	text = rsub(text, "ʰʼ", "ʼ")
	text = rsub(text, "^D", "-")
	text = rsub(text, "D", "")
	text = rsub(text, "h$", "(h)")
	return text
end

function export.IPA(frame)
	local words = {}
	
	for _, word in ipairs(frame:getParent().args) do
		table.insert(words, word)
	end
	
	if #words == 0 then
		words = {mw.title.getCurrentTitle().text}
	end
	
	local IPA_results = {}
	
	for _, word in ipairs(words) do
		table.insert(IPA_results, { pron = "[" .. phonetic(word) .. "]" })
	end
	
	return m_IPA.format_IPA_full(lang, IPA_results)
end

return export