local export = {}
local PAGENAME = mw.title.getCurrentTitle().text

local initials = {
	["hm"] = "m̥", ["hnl"] = "m̥ˡ", ["hn"] = "n̥", ["hny"] = "ɲ̊",
	["m"] = "m", ["nl"] = "mˡ", ["n"] = "n", ["ny"] = "ɲ", ["g"] = "ŋ",
	["p"] = "p", ["pl"] = "pˡ", ["t"] = "t", ["r"] = "ʈ", ["d"] = "d",
	["ph"] = "pʰ", ["plh"] = "pˡʰ", ["th"] = "tʰ", ["rh"] = "ʈʰ", ["dh"] = "dʱ",
	["np"] = "ᵐb", ["npl"] = "ᵐbˡ", ["nt"] = "ⁿd", ["nr"] = "ᶯɖ",
	["nph"] = "ᵐpʰ", ["nplh"] = "ᵐpˡʰ", ["nth"] = "ⁿtʰ", ["nrh"] = "ᶯʈʰ",
	["c"] = "c", ["k"] = "k", ["q"] = "q", [""] = "ʔ",
	["ch"] = "cʰ", ["kh"] = "kʰ", ["qh"] = "qʰ",
	["nc"] = "ᶮɟ", ["nk"] = "ᵑɡ", ["nq"] = "ᶰɢ",
	["nch"] = "ᶮcʰ", ["nkh"] = "ᵑkʰ", ["nqh"] = "ᶰqʰ",
	["tx"] = "t͡s", ["ts"] = "t͡ʂ", ["ntx"] = "ⁿd͡z", ["nts"] = "ᶯd͡ʐ",
	["txh"] = "t͡sʰ", ["tsh"] = "t͡ʂʰ", ["ntxh"] = "ⁿt͡sʰ", ["ntsh"] = "ᶯt͡ʂʰ",
	["f"] = "f", ["x"] = "s", ["hl"] = "ɬ", ["s"] = "ʂ", ["xy"] = "ç", ["h"] = "h",
	["v"] = "v", ["l"] = "l", ["z"] = "ʐ", ["y"] = "ʝ",
	["xy*"] = "ɕ", ["txy"] = "t͡ɕ", ["txhy"] = "t͡ɕʰ", ["ntxy"] = "ⁿd͡ʑ", ["ntxhy"] = "ⁿt͡ɕʰ", -- morphophonological rules
}

local vowels = {
	["a"] = "a", ["e"] = "e", ["i"] = "i", ["o"] = "ɒ", ["u"] = "u", ["w"] = "ɨ",
	["ee"] = "ẽ", ["oo"] = "ɒ̃", ["on"] = "ɒ̃", ["ww"] = "ɨ̃",
	["ai"] = "ai̯", ["au"] = "au̯", ["aw"] = "aɨ̯", ["ia"] = "i̯ə", ["ua"] = "u̯ə",
	[""] = "",
}

local tones = {
	["b"] = "˥", ["j"] = "˥˧", ["v"] = "˧˦", [""] = "˧",
	["s"] = "˩", ["g"] = "˧˩̤", ["m"] = "˩̰ˀ", ["d"] = "˨˩˧",
}

function export.show(frame)

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

	for syl in mw.text.gsplit(text, "[ %.%-]") do
		local i, v, t = syl:match("([cdfghklmnpqrstvxyz]*)([aeiouw]+n?)([bjvsgmd]?)")
		if v == "i" or v == "e" or v == "ia" then
			if i == "x" or i == "xy" then
				i = "xy*"
			elseif i == "tx" or i == "txh" or i == "ntx" or i == "ntxh" then
				i = i .. "y"
			end
		end
		if debug then
			table.insert(ipa, "["..(i or "-").."]["..(v or "-").."]["..(t or "-").."]")
		else
			table.insert(ipa, initials[i] .. vowels[v] .. tones[t])
		end
	end

	return "* " .. (qualifier and frame:expandTemplate{title = "qualifier", args = {qualifier}} .. " " or "") .. "[[วิกิพจนานุกรม:สัทอักษรสากล|สัทอักษรสากล]]<sup>([[ภาคผนวก:การออกเสียงภาษาม้งขาว|คำอธิบาย]])</sup>: <span class=\"IPA\">/" .. table.concat(ipa, ".") .. "/</span>[[หมวดหมู่:ศัพท์ภาษาม้งขาวที่มีการออกเสียงไอพีเอ]]" .. (#ipa > 0 and "[[หมวดหมู่:ศัพท์ภาษาม้งขาวที่มี " .. #ipa .. " พยางค์]]" or "")

end

return export