local export = {}

local m_IPA = require("Module:IPA")
local lang = require("Module:languages").getByCode("orv")
local m_a = require("Module:accent qualifier")
local rsub = mw.ustring.gsub
local lsub = require("Module:string/replace last").replace_last
local rlower = mw.ustring.lower

local K = mw.ustring.char(0x0311)
local A = mw.ustring.char(0x0301)
local V = "[аеиоуъꙑьѣюꙗѧѫѩѭѵioɔɛ]"
local I = "[еиѣюꙗѧѭь]"
local F = "[еиьѣюꙗѧѭѵ]"
local P = "[сзцнлр]"
local H = "[бвдзлмнпрстфѯѱѳ]"
local C = "[бвгджзклмнпрстфхцчшщѯѱѳj]"

local phon = {
	["а"]="ɑ",	["б"]="b",	["в"]="ʋ",	["г"]="ɡ",	["д"]="d",
	["е"]="ɛ",	["ж"]="ʑ",	["з"]="z",	["и"]="i",	["к"]="k",
	["л"]="l",	["м"]="m",	["н"]="n",	["о"]="ɔ",	["п"]="p",
	["р"]="r",	["с"]="s",	["т"]="t",	["у"]="u",	["ф"]="f",
	["х"]="x",	["ш"]="ɕ",	["ъ"]="ʊ",	["ꙑ"]="ɯ",	["ь"]="ɪ",
	["ѣ"]="eː", ["ю"]="u",	["ꙗ"]="ɑ",	["ѧ"]="ɛ̃", ["ѫ"]="õ",
	["ѩ"]="ɛ̃",	["ѭ"]="õ",	["ѯ"]="ks",	["ѱ"]="ps",	["ѳ"]="f",
	["ц"]="t͡sʲ", ["ч"]="t͡ɕ", ["щ"]="ɕt͡ɕ", ["ѵ"]="i",
}

local function format_accent(qual)
	return m_a.format_qualifiers(lang, {qual})
end

local function convert(text, temp)
	local T = temp
	text = rlower(text)
	-- long o
	text = rsub(text, "о" .. K, "o" .. A .. "ː")
	-- жд
	text = rsub(text, "жд%f" .. F, "ʑd͡ʑ")
	-- iotation
	text = rsub(text, "^(" .. I .. ")", "j%1")
	text = rsub(text, "(" .. V .. A .. "?)(" .. I .. ")", "%1j%2")
	-- palatals
	text = rsub(text, "(" .. P .. ")" .. K, "%1ʲ")
	if T >= 1 then
		-- palatalisation
		text = rsub(text, "(" .. H .. ")%f" .. F, "%1ʲ")
	end
	-- stress
	if T >= 2 then
		text = rsub(text, "[ь]" .. A, "ˈW")
		text = rsub(text, "[ъ]" .. A, "ˈY")
	end
	text = rsub(text, "(" .. V .. ")" .. A, "ˈ%1")
	local i = 1
	while i <= 6 do
		text = rsub(text, "(" .. C .. "ʲ?)ˈ", "ˈ%1")
		i = i + 1
	end
	if T >= 2 then
		-- yer-loss
		local index = 1
		while index <= 10 do -- random number that should cover it
			text = lsub(text, "[ьъ]", "Q", 1)
			text = rsub(text, "jь", "ji")
			text = rsub(text, "[ь](" .. C .. "?" .. C .. "?" .. C .. "?" .. C .. "?ʲ?)Q", "W%1Q")
			text = rsub(text, "[ъ](" .. C .. "?" .. C .. "?" .. C .. "?" .. C .. "?ʲ?)Q", "Y%1Q")
			text = rsub(text, "о(" .. A .. "?)(" .. C .. C .. "?ʲ?Q)", "ɔ%1ː%2")
			text = rsub(text, "е(" .. A .. "?)(" .. C .. C .. "?ʲ?Q)", "ɛ%1ː%2")
			index = index + 1
		end
		text = rsub(text, "W", "ɛ")
		text = rsub(text, "Y", "ɔ")
		text = rsub(text, "Q", "")
		-- cluster resolution
		text = rsub(text, "^([пбтдкг])([пбтдкг])", "%1ɔ%2")
		text = rsub(text, "^([пбтдкг]ʲ)([пбтдкг])", "%1ɛ%2")
		text = rsub(text, "^([пбтдкг])([пбтдкг])$", "%1ɔ%2")
		text = rsub(text, "^([пбтдкг]ʲ)([пбтдкг])$", "%1ɛ%2")
		text = rsub(text, "^(" .. C .. C .. "?ʲ?" .. C .. "?ʲ?" .. "ʲ?)ˈ", "ˈ%1")
	end
	-- general phonology
	text = rsub(text, ".", phon)
	if T >= 1 then
		-- de-nasalisation
		text = rsub(text, "ɛ̃", "a")
		text = rsub(text, "õ", "u")
		text = rsub(text, "ɑ", "a")
	end
	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 E = {}
	local M = {}
	local L = {}
	
	for _, word in ipairs(words) do
		if mw.ustring.match(word, "и" .. A .. "?" .. I) then
			table.insert(E, { pron = "/" .. convert(rsub(word, "и(" .. A .. "?" .. I .. ")", "ь%1"), 0) .. "/" })
			table.insert(M, { pron = "/" .. convert(rsub(word, "и(" .. A .. "?" .. I .. ")", "ь%1"), 1) .. "/" })
			table.insert(L, { pron = "/" .. convert(rsub(word, "и(" .. A .. "?" .. I .. ")", "ь%1"), 2) .. "/" })
		end
		if mw.ustring.match(word, "ꙑ" .. A .. I) then
			table.insert(E, { pron = "/" .. convert(rsub(word, "ꙑ(" .. A .. I .. ")", "ъ%1"), 0) .. "/" })
			table.insert(M, { pron = "/" .. convert(rsub(word, "ꙑ(" .. A .. I .. ")", "ъ%1"), 1) .. "/" })
			table.insert(L, { pron = "/" .. convert(rsub(word, "ꙑ(" .. A .. I .. ")", "ъ%1"), 2) .. "/" })
		end
		if mw.ustring.match(word, "о" .. K .. C .. C .. "?" .. K .. "?[ьъ]") then
			table.insert(L, { pron = "/" .. convert(rsub(word, "о" .. K .. "(" .. C .. C .. "?" .. K .. "?[ьъ])", "о" .. A .. "%1"), 2) .. "/" })
		end
		if mw.ustring.match(word, "ѣ" .. A .. "?" .. C .. C .. "?" .. K .. "?[ьъ]") then
			table.insert(L, { pron = "/" .. convert(rsub(word, "ѣ(" .. A .. "?" .. C .. C .. "?" .. K .. "?[ьъ])", "е%1"), 2) .. "/" })
		end
		table.insert(E, { pron = "/" .. convert(word, 0) .. "/" })
		table.insert(M, { pron = "/" .. convert(word, 1) .. "/" })
		table.insert(L, { pron = "/" .. convert(word, 2) .. "/"})
	end
	
	local show =
		"*" .. format_accent("約9世紀") .. " " .. m_IPA.format_IPA_full { lang = lang, items = E } .. "\n*" ..
		format_accent("約11世紀") .. " " .. m_IPA.format_IPA_full { lang = lang, items = M } .. "\n*" ..
		format_accent("約13世紀") .. " " .. m_IPA.format_IPA_full { lang = lang, items = L }
	
	return show
end

return export