local export = {}

local m_IPA = require("Module:IPA")
local lang = require("Module:languages").getByCode("raw")

local u = mw.ustring.char

local rsubn = mw.ustring.gsub

-- version of rsubn() that discards all but the first return value
local function rsub(term, foo, bar)
	local retval = rsubn(term, foo, bar)
	return retval
end

-- apply rsub() repeatedly until no change
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 ACUTE = u(0x0301) -- acute =  ́
local GRAVE = u(0x0300) -- grave =  ̀
local MACRON = u(0x0304) -- macron =  ̄
local DOT = u(0x0323) -- dot =  ̣
local NASAL = u(0x0303) -- nasal =  ̃

local consonants = {
	["g"] = "kɡ",
	["k"] = "kʰ",
	["q"] = "ʔ",
	["h"] = "h",
	["ng"] = "ŋ",
	["j"] = "tʃ",
	["ch"] = "tʃʰ",
	["sh"] = "ʃ",
	["y"] = "j",
	["r"] = "ɾ",
	["d"] = "td",
	["t"] = "tʰ",
	["s"] = "s",
	["z"] = "z",
	["n"] = "n",
	["b"] = "pb",
	["p"] = "pʰ",
	["l"] = "l",
	["w"] = "w",
	["m"] = "m",
	["f"] = "f",
	["ny"] = "ɲ",
}

local vowels = {
	["i"] = "i",
	["ø"] = "ɯ",
	["u"] = "u",
	["e"] = "ɛ",
	["o"] = "ɔ",
	["v"] = "ə",
	["a"] = "ɑ",
}

local nasals = {
	["á"] = "ɑ",
	["ā"] = "ɑ",
	["à"] = "ɑ",
}

function export.IPA(text, loanword, downstep)
	text = mw.ustring.lower(text)
	text = mw.ustring.gsub(text, "%-", "z")
	text = mw.ustring.toNFD(text)
	text = mw.ustring.gsub(text, ".[" .. DOT .. "]", {
		["a" .. DOT] = "á",
		["a" .. DOT] = "ā",
		["a" .. DOT] = "à",
	})
	text = mw.ustring.gsub(text, "x", "")
	
	-- Adds downstep ꜜ before provided syllable
	if downstep then
		downstep = tonumber(downstep)
		local i = 0
		local result = (text):gsub("[^.]+", function(syllable)
    		i = i + 1
    		if i == downstep then
        		return "ꜜ" .. syllable
    		end -- else leave it alone
		end)
		text = result
	end
		
	return mw.ustring.toNFC(text)
end

function export.show(frame)
	local args = frame:getParent().args
	local p, results = {}, {}

	if args[1] then
		for _, item in ipairs(args) do
			table.insert(p, (item ~= "") and item or nil)
		end
	else
		error("Please provide a tone marked term.")
	end

	for _, text in ipairs(p) do
		if args["loan"] == "1" then
			table.insert(results, {pron = "/" .. export.IPA(text, true, args["downstep"]) .. "/", note = nil})
		else
			table.insert(results, {pron = "/" .. export.IPA(text, false, args["downstep"]) .. "/", note = nil})
		end
	end

	return m_IPA.format_IPA_full(lang, results)
end

return export