local export = {}

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

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

local lowerc = {
	["А"]="а",	["Б"]="б",	["В"]="в",	["Г"]="г",	["Д"]="д",
	["Е"]="е",	["Ж"]="ж",	["З"]="з",	["И"]="и",	["К"]="к",
	["Л"]="л",	["М"]="м",	["Н"]="н",	["О"]="о",	["П"]="п",
	["Р"]="р",	["С"]="с",	["Т"]="т",	["У"]="у",	["Ф"]="ф",
	["Х"]="х",	["Ц"]="ц",	["Ч"]="ч",	["Ш"]="ш",	["Щ"]="щ",
	["Ъ"]="ъ",	["Ꙑ"]="ꙑ",	["Ь"]="ь",	["Ѣ"]="ѣ",	["Ꙗ"]="ꙗ",
	["Ю"]="ю",	["Ѫ"]="ѫ",	["Ѭ"]="ѭ",	["Ѧ"]="ѧ",	["Ѩ"]="ѩ",
	["Ѯ"]="ѯ",	["Ѱ"]="ѱ",	["Ѳ"]="ѳ",	["Ѵ"]="ѵ",
}

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 early(text)
	text = rsub(text, ".", lowerc)
	-- long o
	text = rsub(text, "о" .. K, "o" .. A .. "ː")
	-- жд
	text = rsub(text, "жд", "ʑd͡ʑ")
	-- iotation
	text = rsub(text, "^(" .. I .. ")", "j%1")
	text = rsub(text, "(" .. V .. A .. "?)(" .. I .. ")", "%1j%2")
	-- palatals
	text = rsub(text, "(" .. P .. ")" .. K, "%1ʲ")
	-- stress
	text = rsub(text, "(" .. V .. ")" .. A, "ˈ%1")
	local i = 1
	while i <= 6 do
		text = rsub(text, "(" .. C .. "ʲ?)ˈ", "ˈ%1")
		i = i + 1
	end
	-- general phonology
	text = rsub(text, ".", phon)
	return text
end

local function middle(text)
	text = rsub(text, ".", lowerc)
	-- 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ʲ")
	-- palatalisation
	text = rsub(text, "(" .. H .. ")%f" .. F, "%1ʲ")
	-- half-reduced
	text = rsub(text, "и(" .. A .. "?)j", "ь%1j")
	text = rsub(text, "ꙑ(" .. A .. "?)j", "ъ%1j")
	-- stress
	text = rsub(text, "(" .. V .. ")" .. A, "ˈ%1")
	local i = 1
	while i <= 6 do
		text = rsub(text, "(" .. C .. "ʲ?)ˈ", "ˈ%1")
		i = i + 1
	end
	-- general phonology
	text = rsub(text, ".", phon)
	-- de-nasalisation
	text = rsub(text, "ɛ̃", "a")
	text = rsub(text, "õ", "u")
	text = rsub(text, "ɑ", "a")
	return text
end

local function late(text)
	text = rsub(text, ".", lowerc)
	-- 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ʲ")
	-- palatalisation
	text = rsub(text, "(" .. H .. ")%f" .. F, "%1ʲ")
	-- half-reduced
	text = rsub(text, "и(" .. A .. "?)j", "ь%1j")
	text = rsub(text, "ꙑ(" .. A .. "?)j", "ъ%1j")
	-- yer-loss
	text = rsub(text, "[ь]" .. A, "ˈW")
	text = rsub(text, "[ъ]" .. A, "ˈY")
	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")
		index = index + 1
	end
	text = rsub(text, "W", "ɛ")
	text = rsub(text, "Y", "ɔ")
	text = rsub(text, "Q", "")
	-- stress
	text = rsub(text, "(" .. V .. ")" .. A, "ˈ%1")
	local i = 1
	while i <= 6 do
		text = rsub(text, "(" .. C .. "ʲ?)ˈ", "ˈ%1")
		i = i + 1
	end
	-- general phonology
	text = rsub(text, ".", phon)
	-- de-nasalisation
	text = rsub(text, "ɛ̃", "a")
	text = rsub(text, "õ", "u")
	text = rsub(text, "ɑ", "a")
	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
		table.insert(E, { pron = "/" .. early(word) .. "/" })
		table.insert(M, { pron = "/" .. middle(word) .. "/" })
		table.insert(L, { pron = "/" .. late(word) .. "/"})
	end
	
	local show = "*" .. m_a("約西元9世紀") .. " " .. m_IPA.format_IPA_full(lang, E) .. "\n*" .. m_a("約西元11世紀") .. " " .. m_IPA.format_IPA_full(lang, M) .. "\n*" .. m_a("約西元13世紀") .. " " .. m_IPA.format_IPA_full(lang, L)
	
	return show
end

return export