local export = {}

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

local function phonetic(text)
	text = rlower(text)
	-- simplexes
	text = rsub(text, "([ptckq])h", "%1ʰ")
	text = rsub(text, "dzh", "dʐ")
	text = rsub(text, "tsh", "tsʰ")
	text = rsub(text, "zh", "ʐ")
	text = rsub(text, "sh", "ʂ")
	text = rsub(text, "khy", "kyʰ")
	text = rsub(text, "gh", "ɢ")
	text = rsub(text, "lh", "lU")
	text = rsub(text, "([bd])b", "%1ʙ")
	text = rsub(text, "([pt])(ʰ?)b", "%1ʙU%2")
	text = rsub(text, "c", "tʂ")
	text = rsub(text, "gy", "dʑ")
	text = rsub(text, "ky", "tɕ")
	text = rsub(text, "ny", "ɲ")
	text = rsub(text, "y", "j")
	text = rsub(text, "ng", "ŋ")
	text = rsub(text, "hr", "ʁ")
	text = rsub(text, "xr", "χ")
	-- complexes
	text = rsub(text, "pʰs", "psʰ")
	text = rsub(text, "([pt])ʰʂ", "%1ʂʰ")
	text = rsub(text, "nk", "ŋk")
	text = rsub(text, "n([ɢqχ])", "ɴ%1")
	-- vowels
	text = rsub(text, "ä", "æ")
	text = rsub(text, "ü", "y")
	text = rsub(text, "ih([aeiouäür])", "iX%1")
	text = rsub(text, "îh([aeiouäür])", "îX%1")
	text = rsub(text, "ìh([aeiouäür])", "ìX%1")
	text = rsub(text, "ĭh([aeiouäür])", "ĭX%1")
	text = rsub(text, "īh([aeiouäür])", "īX%1")
	text = rsub(text, "íh([aeiouäür])", "íX%1")
	text = rsub(text, "ih", "ɨ")
	text = rsub(text, "jy", "ɥy")
	-- tones
	text = rsub(text, "îh", "ɨ1")
	text = rsub(text, "â", "a1")
	text = rsub(text, "ê", "e1")
	text = rsub(text, "î", "i1")
	text = rsub(text, "ô", "o1")
	text = rsub(text, "û", "u1")
	text = rsub(text, "̂", "1")
	text = rsub(text, "ìh", "ɨ2")
	text = rsub(text, "ǜ", "y2")
	text = rsub(text, "à", "a2")
	text = rsub(text, "è", "e2")
	text = rsub(text, "ì", "i2")
	text = rsub(text, "ò", "o2")
	text = rsub(text, "ù", "u2")
	text = rsub(text, "̀", "2")
	text = rsub(text, "ĭh", "ɨ3")
	text = rsub(text, "ă", "a3")
	text = rsub(text, "ĕ", "e3")
	text = rsub(text, "ĭ", "i3")
	text = rsub(text, "ŏ", "o3")
	text = rsub(text, "ŭ", "u3")
	text = rsub(text, "̆", "3")
	text = rsub(text, "īh", "ɨ4")
	text = rsub(text, "ā", "a4")
	text = rsub(text, "ē", "e4")
	text = rsub(text, "ī", "i4")
	text = rsub(text, "ō", "o4")
	text = rsub(text, "ū", "u4")
	text = rsub(text, "ǟ", "æ4")
	text = rsub(text, "ǖ", "y4")
	text = rsub(text, "̄", "4")
	text = rsub(text, "íh", "ɨ5")
	text = rsub(text, "á", "a5")
	text = rsub(text, "é", "e5")
	text = rsub(text, "í", "i5")
	text = rsub(text, "ó", "o5")
	text = rsub(text, "ú", "u5")
	text = rsub(text, "ǘ", "y5")
	text = rsub(text, "́", "5")
	text = rsub(text, "X", "h")
	text = rsub(text, "([aeiouæyɨ])", "%16")
	text = rsub(text, "6([12345])", "%1")
	text = rsub(text, "i6([aeo])", "Y%1")
	text = rsub(text, "u6([ao])", "W%1")
	-- phonetics
	text = rsub(text, "xu", "xnu")
	text = rsub(text, "h([iueo])", "hn%1")
	text = rsub(text, "([aeiouæyɨ][12345])e6r", "%1ɹ")
	text = rsub(text, "([aeiouæyɨ])([12345])([aeiouæyɨʉɤ])6", "%1%3%2D")
	text = rsub(text, "([nmɲŋ][aeiouæyɨ])", "%1N")
	text = rsub(text, "([aeiouæyɨ])(D?[123456]r?[xh]?[nmɲŋ])", "%1N%2")
	text = rsub(text, "([aeiouæyɨ])([aeiouæyɨʉɤ])DN", "%1N%2DN")
	text = rsub(text, "^([aeiouæyɨ])", "ʔ%1")
	text = rsub(text, "Y", "j")
	text = rsub(text, "W", "w")
	text = rsub(text, "([kgxŋqɢχʁɴ])a", "%1ɑ")
	text = rsub(text, "([qɢχʁɴ])o", "%1ɔ")
	text = rsub(text, "ʙ(U?ʰ?)u", "ʙɯ")
	text = rsub(text, "([td][sɕʂzʑʐ]ʰ?)u", "%1ɯ")
	text = rsub(text, "e([123456])r", "ɚ%1")
	text = rsub(text, "e", "ɛ")
	text = rsub(text, "y", "Ü")
	text = rsub(text, "([jɕʑɲ]ʰ?)Ü", "%1y")
	text = rsub(text, "Ü", "ʉ")
	text = rsub(text, "ʙ(U?ʰ?)ɨ", "ʙ%1ʙS")
	text = rsub(text, "([ʂʐ]ʰ?)ɨ", "%1ɯ")
	text = rsub(text, "([ɕʑr]ʰ?)ɨ", "%1ɘ")
	text = rsub(text, "vɨ", "vS")
	text = rsub(text, "mu", "mmS")
	text = rsub(text, "mSN", "mS")
	text = rsub(text, "([123456])ŋ$", "ŋ%1")
	text = rsub(text, "([ptbd])ʙ", "%1Bʙ")
	text = rsub(text, "([td])([sɕʂzʑʐ]ʰ?)", "%1B%2")
	-- diacritics
	text = rsub(text, "hn", "ɦN")
	text = rsub(text, "xn", "xN")
	text = rsub(text, "h", "ɦ")
	text = rsub(text, "g", "ɡ")
	text = rsub(text, "B", "͡")
	text = rsub(text, "U", "̥")
	text = rsub(text, "S", "̩")
	text = rsub(text, "N", "̃")
	text = rsub(text, "D", "̯")
	text = rsub(text, "1", "˥")
	text = rsub(text, "2", "˥˧")
	text = rsub(text, "3", "˩˧")
	text = rsub(text, "4", "˧˩")
	text = rsub(text, "5", "˧˥")
	text = rsub(text, "6", "˧")
	text = rsub(text, "ɲ", "n̠ʲ")

	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