local export = {}

local m_IPA = require("Module:IPA")
local lang = require("Module:languages").getByCode("ltg")
local rsub = mw.ustring.gsub
local rlower = mw.ustring.lower

local c = mw.ustring.char(0x0302)
local g = mw.ustring.char(0x0300)
local s = mw.ustring.char(0x032F)

local V = "[aeėiouy]"
local R = "[lļr]"
local F = "[iìîɛæ]u?" .. s .. "?ː?"
local C = "[bdfɡxjklmnprvʃʒz]"
local X = "ˈ?[bcdfghjklmnprstvzčģķļņšž$]"

local di = {
	["aî"]="aqi̯",	["ai"]="awi̯",
	["eî"]="æqi̯",	["ei"]="æwi̯",
	["ėî"]="ɛqi̯",	["ėi"]="ɛwi̯",
	["oî"]="ɔqi̯",	["oi"]="ɔwi̯",
	["uî"]="uqi̯",	["ui"]="uwi̯",
	["aû"]="aqu̯",	["au"]="awu̯",
	["iû"]="iqu̯",	["iu"]="iwu̯",
	["yû"]="ɨqu̯",	["yu"]="ɨwu̯",
	["oû"]="ɔqu̯",	["ou"]="ɔwu̯",
	["uô"]="uqɔ̯",	["uo"]="uwɔ̯",
	["iê"]="iqɛ̯",	["ie"]="iwɛ̯",
}

local phon = {
	-- non-tonal
	["a"]="a",	["b"]="b",	["c"]="ts",	["d"]="d",
	["e"]="æ",	["ė"]="ɛ",	["f"]="f",	["g"]="ɡ",
	["h"]="x",	["i"]="i",	["j"]="j",	["k"]="k",
	["l"]="l",	["m"]="m",	["n"]="n",	["o"]="ɔ",
	["p"]="p",	["r"]="r",	["s"]="s",	["t"]="t",
	["u"]="u",	["v"]="v",	["y"]="ɨ",	["č"]="tʃ",	
	["ģ"]="ɡʲ",	["ķ"]="kʲ",	["ļ"]="lʲ",	["ņ"]="nʲ",	
	["š"]="ʃ",	["ž"]="ʒ",	["'"]="ʲ",
	-- tonal
	["â"]="âː",	["ê"]="æ̂ː", ["î"]="îː", ["û"]="ûː",
	["ā"]="àː",	["ē"]="æ̀ː", ["ī"]="ìː", ["ū"]="ùː",
}

local function phonetic(text)
	text = rlower(text)
	-- basic phonology
	text = rsub(text, "..", di)
	text = rsub("1" .. text, "..", di)
	text = rsub(text, "1", "")
	text = rsub(text, "(" .. V .. ")(" .. R .. ")" .. c, "%1q%2")
	text = rsub(text, "(" .. V .. ")(" .. R .. X .. ")", "%1w%2")
	text = rsub(text, ".", phon)
	text = rsub(text, "q", c)
	text = rsub(text, "w", g)
	-- palatalisation
	text = rsub(text, "([pbvtdszkɡmnl])(" .. F .. ")", "%1ʲ%2")
	local i = 0
	text = rsub(text, "(" .. F .. C .. "?" .. C .. "?[ts]?[ts]?)([ts])$", "%1%2ʲ")
	while i <= 3 do
		text = rsub(text, "([ts])ʲ([ts])", "%1ʲ%2ʲ")
		i = i + 1
	end
	i = 0
	while i <= 5 do
		text = rsub(text, "([pbvtdszkɡmnl])(.)ʲ", "%1ʲ%2ʲ")
		i = i + 1
	end
	i = 0
	-- devoicing
	text = rsub(text, "b$", "p")
	text = rsub(text, "d$", "t")
	text = rsub(text, "ɡ(ʲ?)$", "k%1")
	text = rsub(text, "z$", "s")
	text = rsub(text, "v$", "f")
	text = rsub(text, "ʒ$", "ʃ")
	while i <= 5 do
		text = rsub(text, "b(ʲ?)([pstkxfʃ])", "p%1%2")
		text = rsub(text, "d(ʲ?)([pstkxfʃ])", "t%1%2")
		text = rsub(text, "ɡ(ʲ?)([pstkxfʃ])", "k%1%2")
		text = rsub(text, "z(ʲ?)([pstkxfʃ])", "s%1%2")
		text = rsub(text, "v(ʲ?)([pstkxfʃ])", "f%1%2")
		text = rsub(text, "ʒ(ʲ?)([pstkxfʃ])", "ʃ%1%2")
		text = rsub(text, "p(ʲ?)([bzdɡʒ])", "b%1%2")
		text = rsub(text, "t(ʲ?)([bzdɡʒ])", "d%1%2")
		text = rsub(text, "k(ʲ?)([bzdɡʒ])", "ɡ%1%2")
		text = rsub(text, "s(ʲ?)([bzdɡʒ])", "z%1%2")
		text = rsub(text, "ʃ(ʲ?)([bzdɡʒ])", "ʒ%1%2")
		i = i + 1
	end
	i = 0
	-- affrication
	text = rsub(text, "n(ʲ?)([sʃzʒ])$", "n%1t%2%1")
	text = rsub(text, "tʲ?([sʃ])", "t͡%1")
	text = rsub(text, "dʲ?([zʒ])", "d͡%1")
	text = rsub(text, "([ʃʒ])ʲ", "%1")
	-- stress
	if mw.ustring.find(text, "ˈ") == nil then
		text = "ˈ" .. text
	end
	text = rsub(text, "^ˈ%f[-]", "")
	text = rsub(text, "%.", "")
	text = rsub(text, "ʲʲ+", "ʲ")
	
	return text
end

function export.IPA(frame)
	local words = {}
	
	for _, word in ipairs(frame:getParent().args) do
		table.insert(words, word)
	end
	
	if #words == 0 then
		words = {mw.title.getCurrentTitle().text}
	end
	
	local IPA_results = {}
	
	for _, word in ipairs(words) do
		table.insert(IPA_results, { pron = "[" .. phonetic(word) .. "]" })
	end
	
	return m_IPA.format_IPA_full(lang, IPA_results)
end

return export