local export = {}

local m_IPA = require("Module:IPA")
local m_table = require("Module:table")
local lang = require("Module:languages").getByCode("gmh")

local U = mw.ustring.char
local rsub = mw.ustring.gsub
local rlower = mw.ustring.lower
local rfind = mw.ustring.find

local raised = U(0x031D)
local consonants = "bpmwfdtznɲrlkɡhvxjsʋʃs̄s̠ŋ"
local C = "[" .. consonants .. "]"
local vowels = "aeɛiouæøyə"
local V = "[" .. vowels .. "]"

local function track(page)
	require("Module:debug").track("gmh-IPA/" .. page)
	return true
end

local function rsub_repeatedly(term, foo, bar)
	while true do
		local new_term = rsub(term, foo, bar)
		if new_term == term then
			return term
		end
		term = new_term
	end
end
	
local digraphs = {
	["iu"] = "yː",
	["ei"] = "ɛi̯",
	["ie"] = "iə̯",
	["ou"] = "ou̯",
	["uo"] = "uə̯",
	["öu"] = "øy̯",
	["eu"] = "ɛu̯",
	["üe"] = "yə̯",
	["pf"] = "p͡f",
	["ph"] = "p͡f",
	["ng"] = "ŋg",
	["ch"] = "x",
	["qu"] = "kw",
	["ht"] = "xt",
	["hs"] = "xs",
}

local function flatmap(items, fun)
	local new = {}
	for _, item in ipairs(items) do
		local results = fun(item)
		mw.logObject(results, "results")
		for _, result in ipairs(results) do
			table.insert(new, result)
		end
		mw.logObject(new, "new")
	end
	return new
end

local phon = {
	["a"] = "a",
	["ā"] = "aː",
	["â"] = "aː",
	["ä"] = "æ",
	["æ"] = "æː",
	["e"] = "e",
	["ë"] = "ɛ",
	["ē"] = "ɛː",
	["ê"] = "ɛː",
	["i"] = "i",
	["ī"] = "iː",
	["î"] = "iː",
	["o"] = "o",
	["ō"] = "oː",
	["ô"] = "oː",
	["u"] = "u",
	["ū"] = "uː",
	["û"] = "uː",
	["ö"] = "ø",
	["œ"] = "øː",
	["ü"] = "y",
	["b"] = "b",
	["p"] = "p",
	["m"] = "m",
	["w"] = "w",
	["v"] = "v",
	["d"] = "d",
	["t"] = "t",
	["z"] = "t͡s̄",
	["s"] = "s̠",
	["ȥ"] = "s̄",
	["n"] = "n",
	["r"] = "r",
	["l"] = "l",
	["c"] = "k",
	["k"] = "k",
	["g"] = "ɡ",
	["h"] = "h",
	[":"] = "ː",
	["?"] = "ʔ"
}

function export.phonemic(text, post)
	text = rlower(text)

	text = rsub(text, " | ", "# | #")
	text = "##" .. rsub(text, " ", "# #") .. "##"

	-- basic phonology
	for digraph, replacement in pairs(digraphs) do
		text = rsub(text, digraph, replacement)
	end
	text = rsub(text, ".", phon)
	
	text = rsub(text, "^ˈ%-", "-")
	if not rfind(text, "'") then
	text = rsub(text, text, "ˈ" .. text)
	end
	text = rsub(text, "'", "ˈ")
	text = rsub(text, "-", "");
	text = rsub(text, "(e)(" .. C .. ")", "ə%2")
	text = rsub(text, "(e)ˈ", "əˈ")
	text = rsub(text, "(e)#", "ə#")
	text = rsub(text, "(ˈ" .. C .. "*)(ə)", "%1e")
	text = rsub(text, "(ˈ##" .. C .. "*)(ə)", "%1e")
	-- suffixes

	local variants = {text}

	local function flatmap_and_sub_pre(from, to1, to2)
		variants =
			flatmap(
			variants,
			function(item)
				if rfind(item, from) then
					local retval = {rsub_repeatedly(item, from, to1)}
					if to2 then
						m_table.insertIfNot(retval, rsub_repeatedly(item, from, to2))
					end
					return retval
				else
					return {item}
				end
			end
		)
	end
	
	flatmap_and_sub_pre("v", "v", "f")
	flatmap_and_sub_pre("#", "")

	return variants
end

function export.IPA(frame)
	local terms = {}

	args = frame:getParent().args
	local currentTitle = mw.title.getCurrentTitle().text

	for _, term in ipairs(args) do
		if term == currentTitle then track("redundant") end
		table.insert(terms, term)
	end

	if #terms == 0 then
		terms = {currentTitle}
	end

	local results = {}

	for _, term in ipairs(terms) do
		local phonemic = export.phonemic(term)
		for _, variant in ipairs(phonemic) do
			table.insert(results, {pron = "/" .. variant .. "/"})
		end
	end

	results[1].qualifiers = {"before 13<sup>th</sup> CE"}
	return "*" .. m_IPA.format_IPA_full { lang = lang, items = results }

end

return export