local export = {}
local m_string_utils = require("Module:string utilities")

local find = m_string_utils.find
local match = m_string_utils.match

local initialConv = {
	["b"] = "p", ["p"] = "pʰ", ["bb"] = "b̥", ["m"] = "m", ["f"] = "f", ["v"] = "v̥",
	["d"] = "t", ["t"] = "tʰ", ["dd"] = "d̥", ["n"] = "n", ["l"] = "l", ["ny"] = "n̠ʲ",
	["z"] = "t͡s", ["c"] = "t͡sʰ", ["zz"] = "d̥͡z̥", ["s"] = "s", ["ss"] = "z̥",
	["j"] = "t͡ɕ", ["q"] = "t͡ɕʰ", ["jj"] = "d̥͡ʑ̊", ["x"] = "ɕ", ["xx"] = "ʑ̊",
	["g"] = "k", ["k"] = "kʰ", ["gg"] = "ɡ̊", ["ng"] = "ŋ",
	["h"] = "x", ["gh"] = "ɣ̊", [""] = ""
}

local finalConv = {
	["r"] = "z̩",
	["i"] = "i", ["ui"] = "u̯i",
	["u"] = "u", ["iu"] = "i̯u",
	["y"] = "y", 
	["a"] = "ä", ["ia"] = "i̯ä", ["ua"] = "u̯ä", ["ya"] = "y̯ä",
	["e"] = "e", ["ie"] = "i̯e̞", ["ue"] = "u̯e̞", ["ye"] = "y̯e̞",
	["o"] = "o", ["io"] = "i̯o",
	["er"] = "ə",
	["ei"] = "ei̯",
	["ai"] = "ai̯", ["uai"] = "u̯ai̯",
	["au"] = "ɑʊ̯", ["iau"] = "i̯ɑʊ̯",
	["ou"] = "ɘu̯",
	["in"] = "in", ["yn"] = "yn",
	["ern"] = "ən", ["un"] = "u̯ən",
	["en"] = "e̞n", ["ien"] = "i̯ɛn", ["uen"] = "u̯ɛn", ["yen"] = "y̯ɛn",
	["an"] = "an", ["ian"] = "i̯an", ["uan"] = "u̯an",
	["eng"] = "ɤŋ",
	["m"] = "m̩", ["ng"] = "ŋ̍"
}

local toneConv = {
	["1"] = "⁴⁴⁵", ["2"] = "¹¹", ["3"] = "³³", ["4"] = "³²⁴", ["5"] = "²¹³", ["6"] = "²²",
	["4*"] = "³²⁴⁻³¹", ["5*"] = "²¹³⁻³¹",
	["0"] = "³",
}

function export.ipa(text)
	if type(text) == "table" then
		text = text.args[1]
	end
	local result = {}
	for word in mw.text.gsplit(text, "/") do
		local syllables = mw.text.split(word, " ")
		local ipa = {}
		for index, syllable in ipairs(syllables) do
			local initial, final, tone
			initial = syllable:match("^([bpmfdtlnzcsjqxgkh]?(g?))")
			final = syllable:match("^" .. initial .. "([^1-6%*]*)")
			if final == "" then
				final = initial
				initial = ""
			end
			if (find(initial, "^[zcs]$") and find(final, "^i")) or (find(initial, "^[jqx]$") and find(final, "^[aeou]")) or (find(final, "^r$") and not find(initial, "^[zcs]$")) then
				error("Invalid input \"" .. syllable .. "\": initial " .. initial .. " cannot go with final " .. final .. ".")
			end	
			tone = syllable:match("[1-6%*]+$") or "0"
			if (match(tone, "^[25]%*?") and match(initial, "^[bfdzsjxgh]$")) then
				initial = initial:gsub("^[bfdzsqxkh]$", {
					["b"] = "bb", ["f"] = "v", ["d"] = "dd", ["z"] = "zz", ["s"] = "ss",
					["j"] = "jj", ["x"] = "xx", ["g"] = "gg", ["h"] = "gh",
				})
			end
			if initial == "n" and (match(final, "^i")) then
				initial = "ny"
			end
			initial = initialConv[initial] or error(("Unrecognised initial: \"%s\""):format(initial))
			final = finalConv[final] or error(("Unrecognised final: \"%s\""):format(final))
			tone = toneConv[tone] or error(("Unrecognised tone: \"%s\""):format(tone))
			ipa[index] = initial .. final .. tone
		end
		local wordipa = table.concat(ipa, " ")
		if not (find(word, "%d")) then
			wordipa = wordipa:gsub("³", "")
		end
		table.insert(result, wordipa)
	end
	return table.concat(result, "/, /")
end

function export.rom(text)
	return (text
		:gsub("/", " / ")
		:gsub("([%d-]+%*?)", "<sup>%1</sup>"))
end

return export