local export = {}

local m_IPA = require("Module:IPA")

local lang = require("Module:languages").getByCode("niv")
local sc = require("Module:scripts").getByCode("Cyrl")
local m_para = require("Module:parameters")
local m_a = require("Module:accent qualifier")
local utilities = require("Module:niv-utilities")

local gsub = mw.ustring.gsub
local rlower = mw.ustring.lower

local correspondences = {
    ["а"]='a', ["в"]='v', ["е"]='je',
    ["ё"]='jo', ["г"]='g', ["д"]='d',
    ["и"]='i', ["й"]='j',
    ["к"]='k', ["л"]='l',
    ["м"]='m', ["н"]='n',
    ["о"]='o', ["п"]='p', ["р"]='r',
    ["с"]='s', ["т"]='t',
    ["у"]='u', ["ф"]='f', ["х"]='x',
    ["ы"]='ɤ', ["э"]='e', ["ю"]='ju',
    ["я"]='ja'
}

local decode = {
	["G"] = "ɣ", ["R"] = "ʁ", ["Q"] = "ɢ",
	["N"] = "ŋ", ["X"] = "χ", ["c"] = "cʰ",
	["q"] = "qʰ", ["r"] = "r̥", ["h"] = "h",
	["k"] = "q", ["w"] = "w"
}

local voiceless = "ptkкCrsf"
local vowel = "[aeiouɤ]"

local function pron(text)
	text = rlower(text)
    text = utilities.encipher(text)
    text = text:gsub("P", "pʰ")
    	:gsub("T", "tʰ")
    	:gsub("K", "kʰ")
    	:gsub("q", "qʰ")
    text = gsub(text, ".", decode)
    text = gsub(text, "ни", "ɲi")
    text = gsub(text, "ди", "ɟi")
    text = gsub(text, "ти", "ci")
    text = gsub(text, "р̌", "r̥")
    text = gsub(text, ".", correspondences)
    text = gsub(text, "nj", "ɲ")
    text = gsub(text, "dj", "ɟ")
    text = gsub(text, "tj", "c")
    text = text:gsub("(%a)j", "%1")
    return text
end

local function toIPA(text)
	text = gsub(text, "\'", "")
	text = gsub(text, "-", "")
	text = gsub(text, ",", "‿")
	text = gsub(text, "ⁿ", "")
	return text
end

local function phon(text)
	-- universal phonetic pronunciation
	local mutation_u = {
		["p"] = 'v', ["t"] = 'r', ["c"] = 'z',
		["k"] = 'ɣ', ["q"] = 'ʁ'
	}
	local mutation_a = {
		["pʰ"] = 'f', ["tʰ"] = 'r̥', ["cʰ"] = 's',
		["qʰ"] = 'χ'
	}
	local voicing = {
		["p"] = 'b', ["t"] = 'd', ["c"] = 'ɟ', 
		["k"] = 'ɡ', ["q"] = 'ɢ'
	}
	local spirantisation = {
		["k"] = 'x', ["q"] = 'χ'
	}
	local boundary = ","
	text = pron(text)
	text = utilities.encipher(text)
	text = gsub(text, ".", correspondences)
	text = gsub(text, "(" .. vowel .. ")f(" .. vowel .. ")", "%1v%2")
	text = gsub(text, "(" .. vowel .. ")s(" .. vowel .. ")", "%1z%2")
	text = gsub(text, "\'", "ˈ")
	text = gsub(text, "-", "\203\144")
	for u,v in pairs(voicing) do
		text = gsub(text, "([mnɲŋⁿ])" .. boundary .. u, "%1" .. boundary .. v)
	end
    for u,v in pairs(spirantisation) do
    	text = gsub(text, u .. "([xχʁ])", v .. "%1")
    end
	for u,v in pairs(mutation_a) do
		text = gsub(text, boundary .. u, boundary .. v)
	end
	for u,v in pairs(mutation_u) do
		text = gsub(text, boundary .. u, boundary .. v)
	end
	text = gsub(text, ",", "\226\128\191\203\136")
	return text
end

local function yphon(text)
	-- phonetic pronunciation among younger speakers
	local C = "[mnŋptkqPTKQfsxχhvzɣʁljwRrl]"
	text = text:gsub("pʰ", "P")
		:gsub("tʰ", "T")
		:gsub("kʰ", "K")
		:gsub("qʰ", "Q")
		:gsub("r\204\165", "R")
	text = phon(text)
	text = gsub(text, "^(" .. C .. ")([ie])(%a)$", "%1\202\178%2%3")
	text = gsub(text, "^(" .. C .. ")([ie])(%a)(%a)$", "%1\202\178%2%3%4")
	text = gsub(text, "^(" .. C .. ")([ie])(%a)(%a)(%a)$", "%1\202\178%2%3%4%5")
	text = text:gsub("P", "pʰ")
		:gsub("T", "tʰ")
		:gsub("K", "kʰ")
		:gsub("Q", "qʰ")
		:gsub("R", "r\204\165")
	return text
end

local function ophon(text)
	-- phonetic pronunciation among older speakers
	-- empty for now
	text = phon(text)
	return text
end

local function fphon(text)
	-- phonetic pronunciation in fast speech
	text = phon(text)
	local boundary = "\226\128\191\203\136"
	local voicing = {
		["f"] = "v",
		["s"] = "z"
	}
	text = gsub(text, "(" .. vowel .. ")" .. "([ɣrʁ])" .. "([rl])", "%1\203\144" .. "" .. "%3")
	for u,v in pairs(voicing) do
		text = gsub(text, u .. boundary .. "([mnŋvzɣʁljwrl])", v .. boundary .. "%1")
	end
	return text
end

function export.make(frame)
	local args = m_para.process(frame:getParent().args, {
	    [1] = { list = true, default = mw.loadData("Module:headword/data").pagename },
	})
    local results = {}
    local results_y = {}
    local results_o = {}
    local results_f = {}
    for i, term in ipairs(args[1]) do
        local phonemic = {}
        local phonetic = {}
        local yphonetic = {}
        local ophonetic = {}
        local fphonetic = {}
        local words = mw.text.split(term, ".", true)
        phonemic[i] = pron(words[i])
        phonetic[i] = phon(words[i])
        yphonetic[i] = yphon(words[i])
        ophonetic[i] = ophon(words[i])
        fphonetic[i] = fphon(words[i])
        phonemic = table.concat(phonemic, " ")
        phonetic = table.concat(phonetic, " ")
        yphonetic = table.concat(yphonetic, " ")
        ophonetic = table.concat(ophonetic, " ")
        fphonetic = table.concat(fphonetic, " ")
        phonemic = toIPA(phonemic)
        phonetic = toIPA(phonetic)
        yphonetic = toIPA(yphonetic)
        ophonetic = toIPA(ophonetic)
        fphonetic = toIPA(fphonetic)
        table.insert(results, { pron = "/" .. phonemic .. "/" })
        if phonetic ~= phonemic and gsub(phonetic, "ˈ", "") ~= phonemic then
        	if not phonetic:match("ˈ") then
        		phonetic = gsub(phonetic, "^", "ˈ")
        	end
        	if not yphonetic:match("ˈ") then
        		yphonetic = gsub(yphonetic, "^", "ˈ")
	        end
	        if not ophonetic:match("ˈ") then
	        	ophonetic = gsub(ophonetic, "^", "ˈ")
	        end
	        if not fphonetic:match("ˈ") then
	        	fphonetic = gsub(fphonetic, "^", "ˈ")
	        end
        	table.insert(results, { pron = "[" .. phonetic .. "]" })
        end
        if yphonetic ~= phonetic and gsub(yphonetic, "ˈ", "") ~= phonemic then
        	table.insert(results_y, { pron = "[" .. yphonetic .. "]" })
        end
        if ophonetic ~= phonetic and gsub(ophonetic, "ˈ", "") ~= phonemic then
        	table.insert(results_o, { pron = "[" .. ophonetic .. "]" })
        end
        if fphonetic ~= phonetic and gsub(fphonetic, "ˈ", "") ~= phonemic then
        	table.insert(results_f, { pron = "[" .. fphonetic .. "]" })
        end
    end
    
    local show = ""
    
    if #results_y ~= 0 or #results_o ~= 0 or #results_f ~= 0 then
		show = show .. '\n<div class="vsSwitcher" data-toggle-category="pronunciations"><span class="vsToggleElement"></span>'
    end
	show = show .. "\n*" .. m_IPA.format_IPA_full { lang = lang, items = results }
	if #results_y ~= 0 then
		show = show .. '<div class="vsHide">\n**' .. m_a.format_qualifiers(lang, {"younger speakers"}) .. 
				" " .. m_IPA.format_IPA_full { lang = lang, items = results_y } ..	'</div></div>'
	end
	if #results_o ~= 0 then
		show = show .. '<div class="vsHide">\n**' .. m_a.format_qualifiers(lang, {"older speakers"}) .. 
				" " .. m_IPA.format_IPA_full { lang = lang, items = results_o } ..	'</div></div>'
	end
	if #results_f ~= 0 then
		show = show .. '<div class="vsHide">\n**' .. m_a.format_qualifiers(lang, {"in faster speech"}) .. 
				" " .. m_IPA.format_IPA_full { lang = lang, items = results_f } ..	'</div></div>'
	end
        
    return show
end

return export