local export = {}

local m_IPA = require("Module:IPA")
local lang = require("Module:languages").getByCode("yux")
local rsub = mw.ustring.gsub
local rlower = mw.ustring.lower
local rlast = require("Module:string/replace last").replace_last
local rfind = mw.ustring.find

local V = "ʲ?[aeiouøəɨɪ]"
local C = "[bvɡʁdʒzjklmnŋprstfqcʃɕɟʎɲyʔ]ʲ?"
local X = "ʲ?[aeiouøəɨɪbvɡʁdʒzjklmnŋprstfqcʃɕɟʎɲyʔ]ʲ?"
local U = "ʲ?[aeiouøɨɪ]"

local phon = {
	["а"]="a",	["б"]="b",	["в"]="v",	["г"]="ɡ",	["ҕ"]="ʁ",
	["д"]="d",	["е"]="ʲe",	["ж"]="ʒ",	["з"]="z",	["и"]="ɪ",	
	["й"]="j",	["к"]="k",	["л"]="l",	["м"]="m",	["н"]="n",	
	["ҥ"]="ŋ",	["о"]="o",	["ө"]="ø",	["п"]="p",	["р"]="r",	
	["с"]="s",	["т"]="t",	["у"]="u",	["ф"]="f",	["х"]="q",
	["ц"]="y",	["ч"]="c",	["ш"]="ʃ",	["щ"]="ɕ",	["ы"]="ɨ",
	["ь"]="ʲ",	["э"]="e",	["ю"]="ʲu",	["я"]="ʲa"
}

local function phonetic(text)
	text = rlower(text)
	text = rsub(text, "сь", "ɕ")
	text = rsub(text, "дь", "ɟ")
	text = rsub(text, "ль", "ʎ")
	text = rsub(text, "нь", "ɲ")
	text = rsub(text, "ɕи", "ɕi")
	text = rsub(text, "ɟи", "ɟi")
	text = rsub(text, "ʎи", "ʎi")
	text = rsub(text, "ɲи", "ɲi")
	text = rsub(text, "чи", "чi")
	-- general phonology
	text = rsub(text, ".", phon)
	-- initial glide (borrowings only)
	text = rsub(text, "^ʲ", "j")
	text = rsub(text, "(" .. V .. ")ʲ", "%1j")
	-- stress (1)
	text = rsub(text, "(" .. V .. ")(" .. X .. ")$", "ˈ%1%2")
	if rfind(text, "ˈ") == nil then
		text = rlast(text, "(" .. V .. ")(" .. X .. ")(" .. C .. ")", "ˈ%1%2%3", 1)
	end
	if rfind(text, "ˈ") == nil then
		text = rlast(text, "(" .. U .. ")", "ˈ%1", 1)
	end
	-- schwa
	text = rsub(text, "(" .. C .. ")([eo])$", "%1ə")
	-- stress (2)
	text = rsub(text, "(" .. V .. ")ˈ(" .. V .. ")", "ˈ%1%2")
	text = rsub(text, "(" .. V .. ")(" .. C .. ")ˈ", "%1ˈ%2")
	text = rsub(text, "(" .. V .. ")(" .. C .. ")(" .. C .. ")ˈ", "%1%2ˈ%3")
	text = rsub(text, "(" .. V .. ")(" .. C .. C .. ")(" .. C .. ")ˈ", "%1%2ˈ%3")
	text = rsub(text, "^(" .. C .. ")ˈ", "ˈ%1")
	text = rsub(text, "^(" .. C .. C .. ")ˈ", "ˈ%1")
	if rfind(text, "^-") ~= nil then
		text = rsub(text, "ˈ", "")
	end
	-- allophones
	text = rsub(text, "ŋʁ", "ɴɢ")
	text = rsub(text, "(" .. V .. "ˈ?)b(" .. V .. ")", "%1w%2")
	text = rsub(text, "(" .. V .. "ˈ?)c(" .. V .. ")", "%1(t)ɕ%2")
	text = rsub(text, "(" .. V .. "ˈ?)c$", "%1(t)ɕ")
	-- diphthongs
	text = rsub(text, "ie", "ie̯")
	text = rsub(text, "ɪe", "ie̯")
	text = rsub(text, "uo", "uo̯")
	text = rsub(text, "uø", "uø̯")
	text = rsub(text, "([aeioø])u", "%1u̯")
	text = rsub(text, "([aeouø])i", "%1i̯")
	text = rsub(text, "(" .. V .. ")j$", "%1i̯")
	text = rsub(text, "(" .. V .. ")j(ˈ?" .. C .. ")", "%1i̯%2")
	text = rsub(text, "(" .. V .. ")%1", "%1ː")
	text = rsub(text, "ɪː", "iː")
	text = rsub(text, "iɪ", "iː")
	-- affricates
	text = rsub(text, "c", "t͡ɕ")
	text = rsub(text, "ɟ", "d͡ʑ")
	text = rsub(text, "y", "t͡s")
	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