local export = {}

export.phonemic = 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

	word = mw.ustring.lower(word)

	-- Phonemic changes:
	word = mw.ustring.gsub(word, "b", "p")
	word = mw.ustring.gsub(word, "d", "t")
	word = mw.ustring.gsub(word, "nng", "ŋŋ")
	word = mw.ustring.gsub(word, "ng", "ŋ")
	word = mw.ustring.gsub(word, "g", "ɡ")
	word = mw.ustring.gsub(word, "e", "i")
	word = mw.ustring.gsub(word, "o", "u")

	return "/" .. word .. "/ "
end

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
	local map3L = { ["nng"] = "ŋ.ŋ" }
	
	-- Digraphs
	local map2L = { ["ng"] = "ŋ", ["aa"] = "aː", ["ee"] = "ɜː",
	                ["ii"] = "iː", ["oo"] = "ɔː", ["uu"] = "uː" }
	
	-- Process trigraphs first
	for k, v in pairs(map3L) do
	    word = mw.ustring.gsub(word, k, v)
	end
	
	-- Then process digraphs
	for k, v in pairs(map2L) do
	    word = mw.ustring.gsub(word, k, v)
	end
	
	local cons = {"m","n","p","t","k","q","v","s","g","r","l","j","b","d","ŋ"}
	
	-- Convert the cons table to a string pattern
	local consPattern = table.concat(cons, "|")
	
	-- Geminates and /t/-affrication
	word = mw.ustring.gsub(word, "(" .. consPattern .. ")(" .. consPattern .. ")", "%2%2")

	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",
					["vv"] = "f.f", ["jj"] = "j.j" }
	-- Monographs
	local map1L = { ["g"] = "ɣ", ["e"] = "ɜ", ["o"] = "ɔ", ["r"] = "ʁ", 
					["d"] = "t", ["b"] = "p", ["'"] = "ˈ" }
	
	local vow = {"a", "e", "i", "o", "u"}
	
	--Syllabification Rules
	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])ŋ([aeiou])", "%1.ŋ%2")
	word = mw.ustring.gsub(word, "([aeiou])ŋ([aeiou])", "%1.ŋ%2")
	word = mw.ustring.gsub(word, "([aeiou])([mnptkqvsgrljbd])([aeiou])", "%1.%2%3")
	word = mw.ustring.gsub(word, "([aeiou])([mnptkqvsgrljbd])([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, "^g", "k")
	word = mw.ustring.gsub(word, "b", "p")
	word = mw.ustring.gsub(word, "d", "t")
	
	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, "ɑː([mnptksljf])", "ɑʶː%1")
	word = mw.ustring.gsub(word, "ɔː([mnptksljf])", "ɔʶː%1")
	word = mw.ustring.gsub(word, "ɜː([mnptksljf])", "ɜʶː%1")
	

	-- u changes:
	word = mw.ustring.gsub(word, "uː%.([ʁq])", "oː.%1")
	word = mw.ustring.gsub(word, "u%.([ʁq])", "o.%1")
	word = mw.ustring.gsub(word, "uː([ʁq])", "oː%1")
	word = mw.ustring.gsub(word, "u([ʁq])", "o%1")
	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, "uː%.([mp])", "uː.%1")
	word = mw.ustring.gsub(word, "u%.([mp])", "u.%1")
	word = mw.ustring.gsub(word, "uː([mp])", "uː%1")
	word = mw.ustring.gsub(word, "u([mp])", "u%1")
	word = mw.ustring.gsub(word, "uː", "ʊː")
	word = mw.ustring.gsub(word, "u", "ʊ")
	word = mw.ustring.gsub(word, "^ʊː%.([^ʁq])", "uː.%1")
	word = mw.ustring.gsub(word, "^ʊ%.([^ʁq])", "u.%1")
	word = mw.ustring.gsub(word, "^ʊː([^ʁq])", "uː%1")
	word = mw.ustring.gsub(word, "^ʊ([^ʁq])", "u%1")
	
	-- a changes:
	word = mw.ustring.gsub(word, "aː%.([ʁq])", "ɑː.%1")
	word = mw.ustring.gsub(word, "a%.([ʁq])", "ɑ.%1")
	word = mw.ustring.gsub(word, "aː([ʁq])", "ɑː%1")
	word = mw.ustring.gsub(word, "a([ʁq])", "ɑ%1")
	word = mw.ustring.gsub(word, "aː$", "aː")
	word = mw.ustring.gsub(word, "a$", "a")
	word = mw.ustring.gsub(word, "aː", "əː")
	word = mw.ustring.gsub(word, "a", "ə")
	word = mw.ustring.gsub(word, "^əː%.([^ʁq])", "aː.%1")
	word = mw.ustring.gsub(word, "^ə%.([^ʁq])", "a.%1")
	word = mw.ustring.gsub(word, "^əː([^ʁq])", "aː%1")
	word = mw.ustring.gsub(word, "^ə([^ʁq])", "a%1")
	
	-- i changes:
	word = mw.ustring.gsub(word, "^iː%.([^ʁq])", "iː.%1")
	word = mw.ustring.gsub(word, "^i%.([^ʁq])", "i.%1")
	word = mw.ustring.gsub(word, "^iː([^ʁq])", "iː%1")
	word = mw.ustring.gsub(word, "^i([^ʁq])", "i%1")
	word = mw.ustring.gsub(word, "iː%.([ʁq])", "ɐː.%1")
	word = mw.ustring.gsub(word, "i%.([ʁq])", "ɐ.%1")
	word = mw.ustring.gsub(word, "iː([ʁq])", "ɐː%1")
	word = mw.ustring.gsub(word, "i([ʁq])", "ɐ%1")
	word = mw.ustring.gsub(word, "iː%.([mp])", "yː.%1")
	word = mw.ustring.gsub(word, "i%.([mp])", "y.%1")
	word = mw.ustring.gsub(word, "iː([mp])", "yː%1")
	word = mw.ustring.gsub(word, "i([mp])", "y%1")
	word = mw.ustring.gsub(word, "iː", "ɪː")
	word = mw.ustring.gsub(word, "i", "ɪ")
	word = mw.ustring.gsub(word, "^ɪː%.([^ʁq])", "iː.%1")
	word = mw.ustring.gsub(word, "^ɪ%.([^ʁq])", "i.%1")
	word = mw.ustring.gsub(word, "^ɪː([^ʁq])", "iː%1")
	word = mw.ustring.gsub(word, "^ɪ([^ʁq])", "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