local export = {}
local gsub = mw.ustring.gsub
local match = mw.ustring.match

function export.tidyRom(text)
	text = gsub(text, "([fhvw])(.?)", function(a, b) if b == "-" or b == "" or b == " " or b == "," then return "<sup>"..a.."</sup>"..b end end)
	return text
end

local iConv = {
	["b"] = "p", ["p"] = "pʰ", ["bb"] = "b",
	["d"] = "t", ["t"] = "tʰ", ["dd"] = "d",
	["gy"] = "c", ["ky"] = "cʰ", ["ghy"] = "ɟ",
	["g"] = "k", ["k"] = "kʰ", ["gg"] = "g",
	
	["z"] = "t͡s", ["c"] = "t͡sʰ", ["zz"] = "d͡z",
	["zh"] = "ʈ͡ʂ", ["ch"] = "ʈ͡ʂʰ", ["dh"] = "ɖ͡ʐ",
	["j"] = "t͡ɕ", ["q"] = "t͡ɕʰ", ["jj"] = "d͡ʑ",
	
	["m"] = "m", ["n"] = "n", ["ny"] = "ȵ", ["ng"] = "ŋ",
	["mh"] = "m̥", ["ngh"] = "ŋ̊", --limited
	
	["l"] = "l", ["lh"] = "ɬ",
	["f"] = "f", ["s"] = "s", ["h"] = "h",
	["sh"] = "ʂ", ["x"] = "ɕ", ["hy"] = "ç",
	
	["r"] = "ʐ",
	
	["w"] = "w", ["y"] = "j",
	
	[""] = "ʔ",
}

local fConv = {
	["i"] = "i", ["e"] = "e", ["a"] = "a", ["ac"] = "ə", ["o"] = "o", ["u"] = "u",
	
	["ii"] = "iː", ["ee"] = "eː", ["aa"] = "aː", ["acc"] = "əː", ["oo"] = "oː", ["uu"] = "uː",
	["ae"] = "ɛː", ["oe"] = "øː", ["ue"] = "yː", ["ie"] = "ɪː", ["uo"] = "ʊː",
	
	["iu"] = "iu", ["eu"] = "eu", ["acu"] = "əu",
	["ao"] = "ao", ["ua"] = "ua", ["ei"] = "ei", ["io"] = "io",
	
	["im"] = "im", ["em"] = "em", ["am"] = "am", ["acm"] = "əm", ["om"] = "om", ["um"] = "um",
	
	["in"] = "ĩ", ["en"] = "ẽ", ["an"] = "an", ["on"] = "on", ["un"] = "un",
	["aen"] = "ɛ̃", ["oen"] = "ø̃", ["uen"] = "ỹ",

	["ing"] = "iŋ", ["eng"] = "eŋ", ["ang"] = "aŋ", ["acng"] = "əŋ", ["ong"] = "oŋ", ["ung"] = "uŋ",
	
	["ib"] = "ip̚", ["eb"] = "ep̚", ["ab"] = "ap̚", ["acb"] = "əp̚", ["ob"] = "op̚", ["ub"] = "up̚",
	
	["ig"] = "i(k̚)ˀ", ["eg"] = "e(k̚)ˀ", ["acg"] = "ə(k̚)ˀ", ["ag"] = "a(k̚)ˀ", ["og"] = "o(k̚)ˀ", ["ug"] = "u(k̚)ˀ",
	
	["ir"] = "iː(ɹ)", ["er"] = "eː(ɹ)", ["ar"] = "aː(ɹ)", ["or"] = "oː(ɹ)", ["ur"] = "uː(ɹ)",
}

local tConv = {
	["f"] = "˥˥",
	["h"] = "˥˨",
	["v"] = "˩˨",
	["w"] = "˩˧˨",
	[""] = "ˑ",
}

function export.ipa(text)
	text = mw.ustring.lower(text)
	for word in mw.ustring.gmatch(text, "([a-z]+)") do
		local originalWord = word
		word = gsub(word, "^([^aeiou]*)([aeiou][aeioucmnbgr]*)([fhvw]?)$", function(initial, final, tone)
			return (iConv[initial] or initial) .. (fConv[final] or final) .. tConv[tone] end)
		text = gsub(text, originalWord, word, 1)
	end
	text = gsub(text, "-", ".")
	return text
end

function ipaFormat(word, no_intro)
	local part = mw.text.split(word, ",")
	return (no_intro and "" or "[[Wiktionary:國際音標|國際音標]]<sup>([[Appendix:藏語發音|幫助]])</sup>:") .. 
		'<span class=\"IPA\">/' .. table.concat(part, "/, /") .. '/</span>'
end

function export.show(frame)
	local args = frame:getParent().args
	local p, ipa, textShow, textHide = {}, {}, "", ""
	
	if args["otb"] ~= "-" then
		local otb_ipa = {}
		local lang = require("Module:languages").getByCode("bo")
		local page_title = lang:transliterate(args["word"] or mw.title.getCurrentTitle().text)
		if page_title then
			local single_letter = {
				["kh"] = "ġ", ["ch"] = "ċ", ["th"] = "ṫ", ["ph"] = "ṗ",
				["ng"] = "ŋ", ["ny"] = "ñ",
				["ts"] = "ʒ", ["dz"] = "ž",
				["zh"] = "ż", ["sh"] = "ṡ"
			}
			
			local voiceless_lateral = {
				["lh"] = "ĺ", ["hr"] = "ŕ"
			}
			
			local function voice(text)
				return match(text, "[kġcċtṫpṗʒǯṡshĺŕ]") and "voiceless" or "voiced"
			end
			
			local base_ipa = {
				["g"] = "ɡ", ["ġ"] = "kʰ",
				["c"] = "t͡ɕ", ["ċ"] = "t͡ɕʰ", ["j"] = "d͡ʑ", ["ñ"] = "ȵ",
				["ṫ"] = "tʰ",
				["ṗ"] = "pʰ",
				["ʒ"] = "t͡s", ["ǯ"] = "t͡sʰ", ["ž"] = "d͡z",
				["ż"] = "ʑ", ["'"] = "ɣ",
				["y"] = "j", 
				["ṡ"] = "ɕ",
				["ĺ"] = "l̥", ["ŕ"] = "r̥"
			}
			
			local syllable_structure = "^(b?)([dkgbmz'srl]?)(%.?)([kġgŋcċjñtṫdnpṗbmʒǯžwżz'yrlṡshĺŕʔ])([yr]?)(w?)([aeiouI])([gdbŋnms'rl]?)([sd]?)$"
			local voicing = { ["s"] = "z" }
			local devoicing = { ["b"] = "p", ["d"] = "t", ["g"] = "k" }
				
			local function initial_process(prefix, superscript, base)
				local prenasal = { ["kġgŋ"] = "ᵑ", ["cċjñtṫdnʒǯžwżz'yrlṡshĺŕʔ"] = "ⁿ", ["pṗbm"] = "ᵐ" }
				if superscript == "'" then
					for ensuing, nasal in pairs(prenasal) do
						superscript = match(base, "[" .. ensuing .. "]") and nasal or superscript
					end
				end
				return gsub(prefix .. superscript, ".", (voice(base) == "voiced" and voicing or devoicing)) .. (base_ipa[base] or base)
			end
			
			local function vowel_process(glide_a, glide_b, vowel, sep)
				local glide_ipa = { ["y"] = "ʲ", ["w"] = "ʷ" }
				local glide_sep_ipa = { ["y"] = "j", ["w"] = "ʷ" }
				local vowel_ipa = { ["I"] = "ᵻ" }
				return gsub(gsub(glide_a .. glide_b, ".", sep ~= "." and glide_ipa or glide_sep_ipa), "rʷ", "ʷr") .. gsub(vowel, ".", vowel_ipa)
			end
			
			local function coda_process(coda, suffix)
				coda = gsub(coda, "'", "(ɣ)")
				return (devoicing[coda] or coda) .. suffix
			end
			
			page_title = gsub(page_title, "tsh", "ǯ")
			for original, replaced in pairs(single_letter) do
				page_title = gsub(page_title, original, replaced)
			end
			page_title = gsub(page_title, "[lh][hr]", voiceless_lateral)
			
			for syllable in mw.text.gsplit(page_title, " ") do
				syllable = gsub(syllable, "^([aeiouI])", "ʔ%1")
				
				if match(syllable, syllable_structure) then
					syllable = gsub(syllable, syllable_structure, function(prefix, superscript, sep, base, glide_a, glide_b, vowel, coda, suffix)
						if match(base, "[yrw]") and glide_a .. glide_b == "" and prefix .. superscript ~= "" then
							glide_a, glide_b = match(base, "[yr]") or "", match(base, "w") or ""
							base = prefix .. superscript
							prefix, superscript = "", ""
						end
						return
							initial_process(prefix, superscript, base) ..
							vowel_process(glide_a, glide_b, vowel, sep) ..
							coda_process(coda, suffix)
						end)
					
					table.insert(otb_ipa, syllable)
				else
					erroneous = true
					break
				end
			end
			textShow = textShow .. "\n* [[w:中古藏語|中古藏語]]: " .. ipaFormat("*" .. table.concat(otb_ipa, "."), true)
			textHide = textHide .. (not erroneous and "* [[w:中古藏語|中古藏語]]:\n** " .. ipaFormat("*" .. table.concat(otb_ipa, ".")) .. " <span style=\"font-size:80%\">(重構)</span>" or "")
		end
	end
	
	if args[1] then
		for index, item in ipairs(args) do
			table.insert(p, (item ~= "") and item or nil)
		end
		for _, transcription in ipairs(p) do
			table.insert(ipa, export.ipa(transcription))
		end
		textShow = textShow .. "\n* [[w:標準藏語|拉薩]]: " .. ipaFormat(table.concat(ipa, ","), true)
		textHide = textHide .. "\n* [[w:衛藏方言|衛藏]]" ..
			"\n** [[w:藏語拼音|藏語拼音]]: <span style=\"font-family:monospace;\" class=\"tr\">" .. export.tidyRom(table.concat(p, ", ")) .. "</span>" ..
			"\n** (''[[w:標準藏語|拉薩]]'') " .. ipaFormat(table.concat(ipa, ","))
	end

	if args["batang"] or args["dege"] then
		textHide = textHide .. "\n* [[w:康方言|康區]]"
		if args["batang"] then
			textShow = textShow .. "\n* [[w:巴塘縣|巴塘]]: " .. ipaFormat(args["batang"], true)
			textHide = textHide .. "\n** (''[[w:巴塘縣|巴塘]]'') " .. ipaFormat(args["batang"])
		end
		if args["dege"] then
			textShow = textShow .. "\n* [[w:德格縣|德格]]: " .. ipaFormat(args["dege"], true)
			textHide = textHide .. "\n** (''[[w:德格縣|德格]]'') " .. ipaFormat(args["dege"])
		end
		if args["gyaitang"] then
			textShow = textShow .. "\n* [[w:建塘鎮|建塘]]: " .. ipaFormat(args["gyaitang"], true)
			textHide = textHide .. "\n** (''[[w:建塘鎮|建塘]]'') " .. ipaFormat(args["gyaitang"])
		end
	end
	
	if args["zeku"] or args["labrang"] or args["arik"] or args["maqu"] or args["mdungnag"] then
		textHide = textHide .. "\n* [[w:安多方言|安多]]"
		if args["zeku"] then
			textShow = textShow .. "\n* [[w:澤庫縣|澤庫]]: " .. ipaFormat(args["zeku"], true)
			textHide = textHide .. "\n** (''[[w:澤庫縣|澤庫]]'') " .. ipaFormat(args["zeku"])
		end
		if args["labrang"] then
			textShow = textShow .. "\n* [[w:夏河縣|拉卜楞]]: " .. ipaFormat(args["labrang"], true)
			textHide = textHide .. "\n** (''[[w:夏河縣|拉卜楞]]'') " .. ipaFormat(args["labrang"])
		end
		if args["arik"] then
			textShow = textShow .. "\n* [[w:祁連縣|Arik]]: " .. ipaFormat(args["arik"], true)
			textHide = textHide .. "\n** (''[[w:祁連縣|Arik]]'') " .. ipaFormat(args["arik"])
		end
		if args["maqu"] then
			textShow = textShow .. "\n* [[w:瑪曲縣|瑪曲]]: " .. ipaFormat(args["maqu"], true)
			textHide = textHide .. "\n** (''[[w:瑪曲縣|瑪曲]]'') " .. ipaFormat(args["maqu"])
		end
		if args["aba"] then
			textShow = textShow .. "\n* [[w:阿壩縣|阿壩]]: " .. ipaFormat(args["aba"], true)
			textHide = textHide .. "\n** (''[[w:阿壩縣|阿壩]]'') " .. ipaFormat(args["aba"])
		end
		if args["mdungnag"] then
			textShow = textShow .. "\n* [[w:肅南裕固族自治縣|Mdung-Nag]]: " .. ipaFormat(args["mdungnag"], true)
			textHide = textHide .. "\n** (''[[w:肅南裕固族自治縣|Mdung-Nag]]'') " .. ipaFormat(args["mdungnag"])
		end
	end

	if args["spiti"] then
		textShow = textShow .. "\n* [[w:拉胡爾-斯皮提語|拉胡爾-斯皮提]]: " .. ipaFormat(args["spiti"], true)
		textHide = textHide .. "\n* [[w:拉胡爾-斯皮提語|拉胡爾-斯皮提]]"
		textHide = textHide .. "\n** (''[[w:拉豪爾和斯皮提縣|斯皮提]]'') " .. ipaFormat(args["spiti"])
	end
	
	
	
	return '<div class="toccolours mw-collapsible mw-collapsed" style="max-width:400px; font-size:100%">' .. textShow ..
	'\n<div class="mw-collapsible-content">\n----\n' .. textHide .. '</div></div>'
end

return export