local export = {}
local PAGENAME = mw.title.getCurrentTitle().text
local find = mw.ustring.find
local gsub = mw.ustring.gsub
local rhyme

local pre = {
	[1] = {"([ꪀ-ꪯ]ꪫ?)ꪚꪾ", "%1ꪰꪚ"}, -- no tone mark
	[2] = {"([ꪀ-ꪯ]ꪫ?)ꪾꪚ", "%1ꪰꪚ"}, -- no tone mark
	[3] = {"ꫛ", "ꪶꪁꪙ"},
	[4] = {"ꫜ", "ꪙꪳ꪿ꪉ"},
	[5] = {"([ꪵꪶꪹꪻꪼ])([ꪀ-ꪯ]ꪫ?)", "%2%1"}, -- for easier processing
	[6] = {"(.*)([꪿꫁])(.*)", "%1%3%2"}, -- for easier processing
}

local initials = {
	["ꪀ"] = "k", ["ꪁ"] = "k", ["ꪂ"] = "kʰ", ["ꪃ"] = "kʰ",
	["ꪄ"] = "x", ["ꪅ"] = "x", ["ꪆ"] = "ɡ", ["ꪇ"] = "ɡ", ["ꪈ"] = "ŋ", ["ꪉ"] = "ŋ",
	["ꪊ"] = "t͡ɕ", ["ꪋ"] = "t͡ɕ", ["ꪌ"] = "t͡ɕʰ", ["ꪍ"] = "t͡ɕʰ",
	["ꪎ"] = "s", ["ꪏ"] = "s", ["ꪐ"] = "ɲ", ["ꪑ"] = "ɲ",
	["ꪒ"] = "d", ["ꪓ"] = "d", ["ꪔ"] = "t", ["ꪕ"] = "t",
	["ꪖ"] = "tʰ", ["ꪗ"] = "tʰ", ["ꪘ"] = "n", ["ꪙ"] = "n",
	["ꪚ"] = "b", ["ꪛ"] = "b", ["ꪜ"] = "p", ["ꪝ"] = "p",
	["ꪞ"] = "pʰ", ["ꪟ"] = "pʰ", ["ꪠ"] = "f", ["ꪡ"] = "f", ["ꪢ"] = "m", ["ꪣ"] = "m",
	["ꪤ"] = "j", ["ꪥ"] = "j", ["ꪦ"] = "r", ["ꪧ"] = "r", ["ꪨ"] = "l", ["ꪩ"] = "l",
	["ꪪ"] = "v", ["ꪫ"] = "v", ["ꪬ"] = "h", ["ꪭ"] = "h", ["ꪮ"] = "ʔ", ["ꪯ"] = "ʔ",
	["ꪀꪫ"] = "kʷ", ["ꪁꪫ"] = "kʷ", ["ꪂꪫ"] = "kʷʰ", ["ꪃꪫ"] = "kʷʰ",
	["ꪄꪫ"] = "xʷ", ["ꪅꪫ"] = "xʷ", ["ꪆꪫ"] = "ɡʷ", ["ꪇꪫ"] = "ɡʷ", ["ꪈꪫ"] = "ŋʷ", ["ꪉꪫ"] = "ŋʷ",
}

local vowels = {
	["ꪰ"] = "a", ["ꪱ"] = "aː", ["ꪲ"] = "i", ["ꪳ"] = "ɨ", ["ꪴ"] = "u",
	["ꪵ"] = "ɛ", ["ꪶ"] = "o", ["ꪮ"] = "ɔ", ["ꪷ"] = "ɔ",
	["ꪸ"] = "iə̯", ["ꪹ"] = "ɨə̯", ["ꪺ"] = "uə̯",
	["ꪻ"] = "aɰ", ["ꪼ"] = "aj", ["ꪽ"] = "an", ["ꪾ"] = "am",
	["ꪹꪱ"] = "aw", ["ꪹꪷ"] = "ə", ["ꪹꪸ"] = "e", [""] = "#",
}
--/aɰ/ comes from Gedney's description of Tai Dam in Hudak's (2008) book (p. 12) (William J. Gedney’s comparative Tai source book) and also from Burusphat, S. (2017). Changes in Tai Dam vowels. JSEALS, 10(2). 10-22.

local finals = {
	[""] = "", ["ꪀ"] = "ʔ", ["ꪒ"] = "t̚", ["ꪚ"] = "p̚",
	["ꪉ"] = "ŋ", ["ꪙ"] = "n", ["ꪣ"] = "m", ["ꪥ"] = "j", ["ꪫ"] = "w",
	["+ꪀ"] = "k̚", ["+ꪫ"] = "w", --force final -k and -w
}

-- Checked syllables have no tone mark
-- and they already sound like "mai ek"
-- per [http://www.amritas.com/141122.htm#11212354]
-- The tone letters including the glottalization ultimately come from Gedney's description of Tai Dam in Hudak's (2008) book (p. 12) (William J. Gedney’s comparative Tai source book).

local tones = {
	[""] = {"˨", "˥"}, -- L 22, H 55
	["꪿"] = {"˦˥", "˦"}, -- L 45, H 44
	["꫁"] = {"˨˩ˀ", "˧˩ˀ"}, -- L 21, H 31
	["ꫀ"] = {"˦˥", "˦"},
	["ꫂ"] = {"˨˩ˀ", "˧˩ˀ"},
}


function export.show(frame)

	local output = {}

	table.insert(output, toIPA(frame))

	-- reduced hyphenetion
	table.insert(output, "* การแบ่งพยางค์: <span class='Tavt blt-reading' lang='blt'>" .. (frame:getParent().args[1] or PAGENAME) .. "</span>")

	-- global var trick
	table.insert(output, "* " .. frame:expandTemplate{title = "rhymes", args = {"blt", rhyme}})

	return table.concat(output, "\n")

end

function toIPA(frame)

	local args = frame:getParent().args
	local text = args[1] or PAGENAME -- supports only one pronunciation
	local debug = args["d"] or nil
	local ipa = {}

	for syl in mw.text.gsplit(text, "[ %.%-]") do
		for _, v in ipairs(pre) do
			syl = gsub(syl, v[1], v[2])
		end

		local i, v, f, t = mw.ustring.match(syl, "^([ꪀ-ꪯ]ꪫ?)([ꪰ-ꪾꪮ]*)(%+?[ꪀꪒꪚꪉꪙꪣꪥꪫ]?)([꪿ꫀ꫁ꫂ]?)$")

		if find(i, "^([ꪀꪂꪄꪆꪈꪊꪌꪎꪐꪒꪔꪖꪘꪚꪜꪞꪠꪢꪤꪦꪨꪪꪬꪮ]ꪫ?)$") then --low class
			tn = 1
		else --high class
			tn = 2
		end
		if find(f, "^(%+?[ꪀꪒꪚ])$") then --checked final
			t = "ꫀ"
		end
		longer = (f == "" and find(vowels[v], "^([iɨuɛoɔəe])$")) -- if no final, make it longer

		if debug then
			table.insert(ipa, "["..(i or "-").."]["..(v or "-").."]["..(f or "-").."]["..(t or "-").."]")
		else
			table.insert(ipa, initials[i] .. vowels[v] .. (longer and "ː" or "") .. finals[f] .. tones[t][tn])
			rhyme = vowels[v] .. (longer and "ː" or "") .. finals[f] -- It becomes rhyme after the last iteration
		end
	end

	local ipa_final = table.concat(ipa, ".")
	ipa_final = "[" .. ipa_final .. "]"

	return "* " .. frame:expandTemplate{title = "IPA", args = {"blt", ipa_final}} .. (#ipa > 0 and "[[หมวดหมู่:ศัพท์ภาษาไทดำที่มี " .. #ipa .. " พยางค์]]" or "")

end

return export