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

local pre = {
	["ျ"] = "္ယ", ["ြ"] = "္ရ", ["ွ"] = "္ဝ", ["ှ"] = "္ဟ",
	["ၞ"] = "္န", ["ၟ"] = "္မ", ["ၠ"] = "္လ",
}

local initials = {
	['က'] = 'k', ['ခ'] = 'kʰ', ['ဂ'] = 'ɡ', ['ဃ'] = 'gʰ', ['ၚ'] = 'ŋ',
	['စ'] = 'c', ['ဆ'] = 'cʰ', ['ဇ'] = 'ɲ', ['ၛ'] = 'cʰ', ['ည'] = 'ɲ',
	['ဋ'] = 't', ['ဌ'] = 'tʰ', ['ဍ'] = 'ɗ', ['ဎ'] = 'tʰ', ['ဏ'] = 'n',
	['တ'] = 't', ['ထ'] = 'tʰ', ['ဒ'] = 't', ['ဓ'] = 'tʰ', ['န'] = 'n',
	['ပ'] = 'p', ['ဖ'] = 'pʰ', ['ဗ'] = 'p', ['ဘ'] = 'pʰ', ['မ'] = 'm',
	['ယ'] = 'j', ['ရ'] = 'r', ['လ'] = 'l', ['ဝ'] = 'w', ['သ'] = 's',
	['ဟ'] = 'h', ['ဠ'] = 'l', ['ၜ'] = 'ɓ', ['အ'] = 'ʔ', ['ၝ'] = 'ɓ',
}

local medials = {
	[''] = '', ['ျ'] = 'j', ['ြ'] = 'r', ['ႂ'] = 'm', ['္ၚ'] = 'ŋ', ['္ည'] = 'ɲ',
	['္က'] = 'k', ['္ခ'] = 'kʰ', ['္ဂ'] = 'ɡ', ['္ဃ'] = 'gʰ',  ['ၞ'] = 'n',
	['္ဋ'] = 't', ['္ဌ'] = 'tʰ', ['္ဍ'] = 'ɗ', ['္ဎ'] = 'tʰ', ['္ဏ'] = 'n',
	['္တ'] = 't', ['္ထ'] = 'tʰ', ['္ဒ'] = 't', ['္ဓ'] = 'tʰ', ['္န'] = 'n',
	['္ပ'] = 'p', ['္ဖ'] = 'pʰ', ['္ဗ'] = 'p', ['္ဘ'] = 'pʰ', ['္မ'] = 'm',
	['္ယ'] = 'j', ['္ရ'] = 'r', ['္လ'] = 'l', ['ွ'] = 'w', ['္သ'] = 's',
	['္ဟ'] = 'h', ['ၠ'] = 'l', ['္ၜ'] = 'ɓ', ['္အ'] = 'ʔ', ['ှ'] = 'h', ["်"] = "¤",
}

local vowels = {
	["ါ"] = "ā", ["ာ"] = "ā", ["ိ"] = "i", ["ီ"] = "iṃ", ["ဳ"] = "ī", ["ု"] = "u", ["ူ"] = "ū", ["ဲ"] = "ʸ",
	["ဴ"] = "ao", ["ေ"] = "e", ["ဵ"] = "e", 
	["ံ"] = "ṃ", ["း"] = "ḥ", ["္"] = "¡",
}

local finals = {
	["ဣ"] = "ʼi", ["ဥ"] = "ʼu",
	["ဨ"] = "ʼe", ["ဩ"] = "ʼo",
	["ဣဳ"] = "ʼī", ["ဥု"] = "ʼū",
	["ေါ"] = "o", ["ော"] = "o",
}

function export.show(frame)

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

	for syl in mw.text.gsplit(text, '[ %.%-]') do
		syl = mw.ustring.gsub(syl, 'ဴ', '္ည') -- for easier detection
		local i, m, v, f, t = mw.ustring.match(syl,
			'^([ကခဂဃၚစဆဇၛဉညဋဌဍဎဏတထဒဓနပဖဗဘမယရလဝသဟဠၜအၝဿအာဣဣဳဥဥူဨအဲသြအဴအံအး])([ျြႂ]?%*?)([ဲဴ္ၚဵ္ညးၞၟြ!]*)([ဥဥူဨအဲသြအဴအံအး]*)([းၟဵ္ၚx]?)$')
		
		if not i then
			error("Pattern did not match the syllable " .. tostring(syl) .. ".")
		end
		
		if i == 'သ' and m == 'ျ' then
			i = 'သျ'
			m = ''
		end
		if i == 'က' and m == 'ျ*' then
			i = 'ကျ*'
			m = ''
		end
		if i == 'ခ' and m == 'ျ*' then
			i = 'ချ*'
			m = ''
		end
		if m == 'ႂ' and v == '်' then
			m = ''
			v = '္ည'
		end
		if v == 'ူ' and f ~= '' then
			v = 'ူ*'
		end
		if v == '!' then
			t = 'x'
		end
		if debug then
			table.insert(ipa, '['..(i or '-')..']['..(m or '-')..']['..(v or '-')..']['..(f or '-')..']['..(t or '-')..']')
		else
			local missing
			for _, part in pairs {
				{ 'initial', initials, i },
				{ 'medial', medials, m },
				{ 'vowel', vowels, v },
				{ 'final', finals, f },
				{ 'tone', tones, t },
			} do
				if not part[2][part[3]] then
					missing = missing or require 'Module:array'()
					table.insert(missing, part)
				end
			end
			if missing then
				error('Data for '
					.. missing
						:map(
							function (part)
								return part[1] .. ' (' .. tostring(part[3]) .. ')'
							end)
						:concat(', ') .. (missing[2] and ' are' or ' is')
					.. ' missing.')
			end
			table.insert(ipa, initials[i] .. medials[m] .. vowels[v] .. finals[f] .. tones[t])
		end
	end

	return '* ' .. (qualifier and frame:expandTemplate{title = 'qualifier', args = {qualifier}} .. ' ' or '') .. '[[ရီု:IPA-Mon|IPA]]<sup>([[Wiktionary:ရမျာင်ပညာမန်|key]])</sup>: <span class="IPA">/' .. table.concat(ipa, '.') .. '/</span>[[Category:ဝေါဟာမန်ရမ္သာင်IPAဂမၠိုင်]]' .. (#ipa > 0 and ' ' or '')

end

return export