local export = {}

local gsub = mw.ustring.gsub
local sub = mw.ustring.sub
local len = mw.ustring.len
local match = mw.ustring.match

local initialConv = {
	["b"] = "p", ["p"] = "pʰ", ["m"] = "m", ["f"] = "ɸ", 
	["d"] = "t", ["t"] = "tʰ", ["l"] = "l", 
	["z"] = "t͡s", ["c"] = "t͡sʰ", ["s"] = "s",
	["zh"] = "ʈ͡ʂ", ["ch"] = "ʈ͡ʂʰ", ["sh"] = "ʂ", ["r"] = "ʐ", 
	["j"] = "t͡ɕ", ["q"] = "t͡ɕʰ", ["x"] = "ɕ", ["ny"] = "n̠ʲ",
	["g"] = "k", ["k"] = "kʰ", ["ng"] = "ŋ",
	["h"] = "x", [""] = ""
}

local finalConv = {
	["r"] = "z̩", ["rr"] = "ʐ̩", 
	["i"] = "i", 
	["u"] = "u", 
	["y"] = "y", 
	["a"] = "a̠", ["ia"] = "ia̠", ["ua"] = "ua̠", ["ya"] = "ya̠", 
	["o"] = "o", ["io"] = "io", 
	["e"] = "ɤ̞", ["ue"] = "uɤ̞", 
	["ai"] = "ai", ["uai"] = "uai", ["yai"] = "yai", 
	["ei"] = "e̞i", ["uei"] = "ue̞i", ["yei"] = "ye̞i", 
	["au"] = "ɒu", ["iau"] = "iɒu", 
	["ou"] = "əu", ["iou"] = "iəu", 
	["ie"] = "ie̞", ["ye"] = "ye̞", 
	["onn"] = "õ", 
	["enn"] = "ə̃",
	["ienn"] = "iẽ", ["yenn"] = "yẽ",
	["en"] = "ən", ["un"] = "uən", 
	["in"] = "in", 
	["yn"] = "yn", 
	["an"] = "an", ["ian"] = "iæn", ["uan"] = "uan", ["yan"] = "yæn", 
	["ong"] = "ʊŋ", ["iong"] = "iʊŋ", 
	["m"] = "m̩",
	["n"] = "n̩",
}

local toneConv = {
	["1"] = "³³", ["2"] = "¹³", ["3"] = "⁴¹", ["4"] = "⁴⁵", ["5"] = "²¹", ["6"] = "²⁴", ["7"] = "³",
	["5-ts"] = "²¹⁻¹¹", ["6-high"] = "²⁴⁻⁴⁴", ["6-low"] = "²⁴⁻²²", ["6*"] = "²⁴", [""] = ""
}

function export.ipa(text, style)
	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, tone = {}, {}
		for index, syllable in ipairs(syllables) do
			local initial, final
			initial = syllable:match("^([bpmfdtlnzcsrjqxgkh]?([hgy]?))")
			if initial:match("^.y") and initial ~= "ny" then
				initial = sub(initial, 1, 1)
			elseif initial == "y" then
				initial = ""
			end
			final = syllable:match("^" .. initial .. "([^1-7\*]*)")
			if final == "" then
				final = initial
				initial = ""
			end
			tone[index] = syllable:match("[1-7\*]+$") or (index ~= 1 and "7" or "")
			if style == "new" then
				initial = initial:gsub("([zcs])h", "%1")
				if final:match("^i") then
					initial = initial:gsub("[zcs]$", { ["z"] = "j", ["c"] = "q", ["s"] = "x" })
				end
				final = final:gsub("(i?ong)", { ["iong"] = "in", ["ong"] = "en" })
			end
			initial = initialConv[initial] or error(("Unrecognised initial: \"%s\""):format(initial))
			if (initial == "" or match(initial, "[ʂʐ]")) and final == "r" then
				final = "rr"
			end
			final = finalConv[final] or error(("Unrecognised final: \"%s\""):format(final))
			if initial == "l" and ((final ~= "ən" and not final:match("^i") and
				match(final, "[ŋ̃n]")) or final:match("^i.*̃")) then
				initial = "l̃"
			end
			if tone[index] == "5" and #syllables > 1 then
				tone[index] = "5-ts"
			elseif tone[index] == "6" then
				if match(tone[index-1] or "", "[126]") then
					tone[index] = "6-high"
				elseif match(tone[index-1] or "", "[345]") then
					tone[index] = "6-low"
				end
			end
			tone[index] = toneConv[tone[index]] or error(("Unrecognised tone: \"%s\""):format(tone[index]))
			ipa[index] = initial .. final .. tone[index]
		end
		table.insert(result, table.concat(ipa, " "))
	end
	return table.concat(result, "/, /")
end

function export.rom(text)
	text = gsub(text, "/", " / ")
	text = gsub(text, '([1-9-]+)', '<sup>%1</sup>')
	return text
end

function export.stylediff(text)
	if match(text, "[zcs]h") or match(text, "[zcs]i") or match(text, "i?ong") then
		return true
	end
	return false
end

return export