local export = {}

export.phonetic = function(frame)
	
	local params = { [1] = {} }
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local word = ""
	if args[1] == nil or args[1] == "" then
		word = mw.title.getCurrentTitle().subpageText
	else
		word = args[1]
	end
	
	if word == "kl-IPA" then
		word = "avinngaq"
	end
	
	word = word:lower()
	
	-- Trigraphs and retracted /a/
	local map3L = { ["nng"] = "ŋ.ŋ", ["aar"] = "ɑːr", ["aaq"] = "ɑːq" }
	
	-- Digraphs and retracted /a/
	local map2L = { ["ng"] = "ŋ", ["aa"] = "aː", ["ee"] = "ɜː",
					["ii"] = "iː", ["oo"] = "ɔː", ["uu"] = "uː",
					["ar"] = "ɑr", ["aq"] = "ɑq" }
				
	-- Geminates and /t/-affrication
	local mapGL = { ["ll"] = "ɬ.ɬ", ["gg"] = "x.x", ["rr"] = "χ.χ",
					["rl"] = "ɬ.ɬ", ["rf"] = "f.f", ["rs"] = "s.s",
					["ff"] = "f.f", ["pp"] = "p.p", ["nn"] = "n.n",
					["mm"] = "m.m", ["tt"] = "t.t", ["qq"] = "q.q",
					["ss"] = "s.s", ["kk"] = "k.k", ["ts"] = "t.t͡s",
					["ti"] = "t͡si", ["rn"] = "n.n", ["rm"] = "m.m",
					["rp"] = "p.p", ["rt"] = "t.t", ["rk"] = "k.k" }
	-- Monographs
	local map1L = { ["g"] = "ɣ", ["e"] = "ɜ", ["o"] = "ɔ", ["r"] = "ʁ", ["'"] = "ˈ" }
	
	local vow = {"a", "e", "i", "o", "u"}
	
	word = mw.ustring.gsub(word, "([aeiou])ng([aeiou])", "%1.ng%2")
	word = mw.ustring.gsub(word, "([aeiou])ng([aeiou])", "%1.ng%2")
	word = mw.ustring.gsub(word, "([aeiou])([mnptkqvsgrlj])([aeiou])", "%1.%2%3")
	word = mw.ustring.gsub(word, "([aeiou])([mnptkqvsgrlj])([aeiou])", "%1.%2%3")
	
	for I = 1, #word do
		local let0 = word:sub(I, I)
		local let1 = word:sub(I + 1, I + 1)
		local let2 = word:sub(I + 2, I + 2)
		if contains(let0, vow) and contains(let1, vow) and let0 ~= let1 then
			word = word:sub(1, I) .. "." .. word:sub(I + 1)
		end
	end
	
	-- Vowel uvularization
	word = mw.ustring.gsub(word, "ːr([mnptkqvsgrljf])", "ʶːr%1")
	
	for let, res in pairs(map3L) do
		word = mw.ustring.gsub(word, let, res)
	end
	
	for let, res in pairs(map2L) do
		word = mw.ustring.gsub(word, let, res)
	end
	
	for let, res in pairs(mapGL) do
		word = mw.ustring.gsub(word, let, res)
	end
	
	for let, res in pairs(map1L) do
		word = mw.ustring.gsub(word, let, res)
	end
	
	word = mw.ustring.gsub(word, "ː%.", "ː")
	word = mw.ustring.gsub(word, "([ntsl])u([ntsl])", "%1ʉ%2")
	word = mw.ustring.gsub(word, "([ntsl])u([ntsl])", "%1ʉ%2")
	word = mw.ustring.gsub(word, "([ntsl])u%.([ntsl])", "%1ʉ.%2")
	word = mw.ustring.gsub(word, "([ntsl])u%.([ntsl])", "%1ʉ.%2")
	
	word = mw.ustring.gsub(word, "ix.x", "iç.ç")
	word = mw.ustring.gsub(word, "ax.x", "ax̟.x̟")
	
	word = mw.ustring.gsub(word, "u.v", "u.ʷ")
	
	word = mw.ustring.gsub(word, "aː([ʁq])", "ɑː%1")
	word = mw.ustring.gsub(word, "a%.([ʁq])", "ɑ.%1")
	
	word = mw.ustring.gsub(word, "ɑː([mnptksljf])", "ɑʶː%1")
	word = mw.ustring.gsub(word, "ɔː([mnptksljf])", "ɔʶː%1")
	word = mw.ustring.gsub(word, "ɜː([mnptksljf])", "ɜʶː%1")
	
	word = mw.ustring.gsub(word, "i([mp])", "i̹%1")
	word = mw.ustring.gsub(word, "i%.([mp])", "i̹.%1")
	
	return "[" .. word .. "]"
	
end

function contains(key, set)
    for k0, val in pairs(set) do
      if val == key then
        return true
      end
    end
    return false
end

return export