local export = {}

local lang = require("Module:languages").getByCode("mai")
local sc = require("Module:scripts").getByCode("Deva")
local m_IPA = require("Module:IPA")

local gsub = mw.ustring.gsub
local gmatch = mw.ustring.gmatch
local find = mw.ustring.find

local correspondences = {
	["ṅ"] = "ŋ", ["g"] = "ɡ", 
    ["c"] = "ʦ", ["j"] = "ʣ", ["ñ"] = "ɲ",
	["ṭ"] = "ʈ", ["ḍ"] = "ɖ", ["ṇ"] = "ɳ",
	["t"] = "t", ["d"] = "d",
	["y"] = "j", ["v"] = "ʋ", ["l"] = "l",
	["ś"] = "ɕ", ["ṣ"] = "ʂ", ["h"] = "ɦ",
	["ṛ"] = "ɽ", ["n"] = "n", ["ž"] = "ʒ",
    ["z"] = "z", ["ṟ"] = "ɹ", ["r̥"] = "rɪ",
    ["r̥̄"] = "riː", ["ṃ"] = "̃", ["ॽ"] = "",
    [","] = ",", ["r"] = "ɾ", ["ỹ"] = "j̃",

	["a"] = "ə", ["â"] = "ᵊ", ["ä"] = "a",
    ["ā"] = "aː", ["i"] = "ɪ", ["ī"] = "iː",
    ["o"] = "o", ["e"] = "e", ["ē"] = "eː",
    ["u"] = "ʊ", ["ū"] = "uː", ["ō"] = "oː",

    ["ǒ"] = "əŏ", ["ě"] = "əĕ", ["’"] = "ː",
    ["ê"] = "æ", ["ô"] = "ɔ",  

    ["ã"] = "ə̃",
    ["ā̃"] = "ãː",   
    ["ĩ"] = "ɪ̃",
    ["ũ"] = "ʊ̃",
    ["ẽ"] = "ẽ",
    ["ē̃"] = "ẽː",
    ["õ"] = "õ",
    ["ō̃"] = "õː",
    ["ãi"] = "ə̃ʊ̯̃",
    ["ãu"] = "ə̃ʊ̯̃",
    ["ï"] = "ĭ",
    ["ü"] = "ŭ",
    ["ë"] = "ĕ",
    ["ö"] = "ŏ",
    ["²"] = "ː",
    ["ḥ"] = "ʰ",  [" "] = "‿", ["ɨ"] = "ɪ", ["ʉ"] = "ʊ",["ᵊ̆"] = "ᵊ",
}

local vowels = "aāā̃ẽõiīuūᵊɔɪʊi̯u̯âäæeī̃ū̃ĩôoʌʌ̃ãũŏĕ̤"
local weak_v = "ᵊiu ̆"
local weak_h_c = "gʣjdḍd̪ṇɽbnmlṛvrɾṟwy"
local weak_h = "([" .. weak_h_c .. "])h"
local aspirate = "([kctṭt̪pʦ])"
local syllabify_pattern = "([" .. vowels .. "]+)([^" .. vowels .. "%.]+)([" .. vowels .."]+)"

local function find_consonants(text)
	local current = ""
	local cons = {}
	for cc in mw.ustring.gcodepoint(text .. " ") do
		local ch = mw.ustring.char(cc)
		if find(current .. ch, "^[ʦʣɨʉkgṅcjñṭḍṇɽtdnpbmɽ̃yrlɳwvɾjwśṣshqxġzžḻṛṟfθðṉ]$") or find(current .. ch, "^[ʦʣkgcjṭḍṇtɽdɽ̃pbṛṟ]h$") then
			current = current .. ch
		else
			table.insert(cons, current)
			current = ch
		end
	end
	return cons
end

local function syllabify(text)
	for count = 1, 2 do
		text = gsub(text, syllabify_pattern, function(a, b, c)
			b_set = find_consonants(b)
			table.insert(b_set, #b_set > 1 and 2 or 1, "")
			return a .. table.concat(b_set) .. c
			end)
end
return text
end


local identical = "knlsfzθð"
for character in gmatch(identical, ".") do
	correspondences[character] = character
end

local function transliterate(text)
	return (lang:transliterate(text))
end

function export.link(term)
	return require("Module:links").full_link{ term = term, lang = lang, sc = sc }
end

function export.toIPA(text, alternate)
	local translit = transliterate(text)
	if not translit then
		error('The term "' .. Maithili .. '" could not be transliterated.')
	end

	if alternate then
translit= gsub(translit, "([kgṅcjñṭḍṇɽtdnpbmɽ̃yrlɳwvɾjʦʣwśṣsqxġzžḻṛṟfθðṉ]?)([aāäeâôoʌ]?)([iuɪʊɨʉ])$", "%3%1%2")
translit= gsub(translit, "([kgṅcjñṭḍtdpbɽ̃yrnlwvɾjwśṣsqʦʣxġzžḻṛṟfθð])([h]?)([aāäeâôoʌ]?)([iuɪʊɨʉ])$", "%4%1%2%3")
translit= gsub(translit, "([kgṅcjñṭḍtdpbɽ̃yrnlwvɾjwśṣsqʦʣxġzžḻṛṟfθð]?)([aāäeâôoʌ]?)([iuɪʊɨʉ]) ", "%3%1%2 ")
translit= gsub(translit, "([kgṅcjñṭḍṇɽtdnpbmɽ̃yrlɳwvɾjwʦʣśṣsqxġzžḻṛṟfθðṉ])([h]?)([aāäeâôoʌ]?)([iuɪʊɨʉ]) ", "%4%1%2%3 ")
translit = gsub(translit, "([aāäʌə])(ː?)([̤]?)(̃?)([y]?)([e̯eë])(̯?)(̃?)", "æ%3%4%8")
translit = gsub(translit, "([aāäʌə])(ː?)([̤]?)(̃?)([vw]?)([o̯oö])(̯?)(̃?)", "ɔ%3%4%8")
translit = gsub(translit, "ě(̃?)", "ɛ%1")
translit = gsub(translit, "ǒ(̃?)", "ɔ%1")
translit = gsub(translit, "([ṣśɕʂ])", "s")
translit = gsub(translit, "v$", "b")
translit = gsub(translit, "v ", "b")
translit = gsub(translit, "([aāäiīuūeâôoʌ])(ː?)ṛ([ʱʰh]?)([aāäiīuūeâôoʌ]?)(̃?)", "%1%2ɾ%3%4%5")
end


local result = gsub(translit, ".", correspondences)
		
	
translit = gsub(translit, "͠", "̃")
translit = gsub(translit, "rr̥", "ri")
translit = gsub(translit, "rr̥̄", "riː")
translit = gsub(translit, "([aāäiīuūeěâôoʌ])(ː?)([̤]?)(̃?)([iīūu])(̃?)", "%1%2%3%4%5%6")
translit = gsub(translit, "([aāäiīuūeěâôoʌ])(ː?)([̤]?)(̃?)([eo])(̃?)", "%1%2%3%4%5̆%6")

translit = gsub(translit, "(#)va([ɪ̯ʊ̯ɪ̃ʊ̃])", "bə%2")
translit = gsub(translit, "(#)v([ieɪēōʊuē̃ō̃ojr])(ː?)", "b%2%3")
translit = gsub(translit, "(#)([v])a([krɾjtcʦʣçʐṅñysśdpɦhn])([tnrṇṣcśkghjɦsāēōçʐʦʣueoʌəayd])", "bə%3%4")
translit = gsub(translit, "([śsnlcçʦʣʐjzkʰʱɦhpɡtdgb])v([aʌäəāiāɪʊɪ̃ʊ̃āēōīuūeoŏĕɔæɛʌ̃ä̃ĩũā̃ē̃ō̃ī̃ū̃ẽõɔ̃e̤])(ː?)([cspdtçʐnɡgkʦʣbɾrjyṇṣśṇɾṅñṃ])",
        "%1w%2%3%4")
translit = gsub(translit, "([śsnlcjzʐçʦʣkhptdgb])([vw])([aāäɪʊəiīāēōuūoeĩ])(ː?)([cspdtngkbrjʦʣyṇṣśṇɾṅñṃ])", "%1w%3%4%5")

translit = gsub(translit, "([ʌäəɪʊāiuāēōeoŏĕ])(ː?)([nl])([td]̪)", "%1%2%3̪%4") -- dental assimilation
translit = gsub(translit, "([ʌəäaɪʊāiāēōueoŏĕ])(ː?)n([ʈɖ])", "%1%2ɳ%3") -- retroflex aassimilation
translit = gsub(translit, "([l])([ʈɖ])", "ɭ%2")
translit = gsub(translit, "([ʌʌ̃äaāiuāūɪʊəãā̃ī̃ĩũū̃ẽõeeāēōā̃ē̃ō̃o̯o ̤])(ː?)ɾ([ʌʌ̃äaāāiīɪuūʊā̃ē̃ō̃əãā̃ī̃ĩũū̃ẽõeeyo̯o])(ː?)", "%1%2ɾ%3%4")
translit = gsub(translit, "ṣp", "ɸp")
translit = gsub(translit, "(#)([spdtzʱʰɦgkbrṇṣśʂʈɖçʐʦʣnṇʌāāäiuīūoɔæɛā̃ē̃ō̃ʌ̃ä̃ĩī̃āēōū̃īūũõɔ̃e̤ːɾṅñ]?)ya", "%2jə")
translit = gsub(translit, "([aʌəäāiīɪuūʊeoŏāēōĕɔæā̃ē̃ō̃ɛāʌ̃ä̃ĩɪ̃ũʊ̃ī̃ū̃ẽõɔ̃e̤])(ː?)kṣ", "%1%2t̚ʦʰ") -- kṣ ligature
translit = gsub(translit, "()kṣ", "ʦʰ") -- kṣ initial
translit = gsub(translit, "%-", " ")
translit = gsub(translit, "r̥", "ri")
translit = gsub(translit, "r̥̄", "riː")
translit = syllabify(translit)
	
	
	-- aspiration rules
	translit = gsub(translit, aspirate .. "h", '%1ʰ')
	translit = gsub(translit, weak_h, '%1ʱ')
	translit = gsub(translit, '([' .. weak_h_c .. '])%.h', '.%1ʱ')
	translit = gsub(translit, aspirate .. '%.h', '.%1ʰ')
        translit = gsub(translit, "%.ː", "ː.")

	local result = gsub(translit, ".", correspondences)
	
	-- formatting
    result = gsub(result, "(...)ə$", "%1ᵊ")
	result = gsub(result, "(...)ə ", "%1ᵊ ")
	result = gsub(result, "(...)ə%.?%-", "%1ᵊ-")
    result = gsub(result, "([ɪʊ])$", "%1̆")
    result= gsub(result, "([aāäeâôoʌiuɪʊe̯eëəᵊ])(ː)([kɦgɕʑṅcjñṭḍṇɽtdnʦʣʰʱpbmɽ̃yrlɳwvɾjwśṣshqxġzžḻṛṟfθðṉ]?)([aāäeâôoʌiuɪʊɨʉe̯eëəᵊ])(ː?)([kɦgṅcjʰʱñṭḍṇɽtdnpbmɽ̃yrlɳwvɾjwśṣshqxġzžḻṛṟfθðṉ]?)([kɦgṅcjñṭḍṇɽtdnpbmɽ̃yrlɳwvɾjwśṣsʰʱhɕʑqxʦʣġzžḻṛṟfθðṉɨʉ]?)([aāäeâôoʌiuɪʊe̯eëəᵊ])(ː?)$", "%1%3%4%5%6%7%8%9")
result= gsub(result, "([aāäeâôoʌiuɪʊe̯eëəᵊ])(ː)([kɦgṅʦʣcɕʑjñṭḍṇɽtdnpbmɽ̃yrʰʱlɳwvɾjwśṣshqxġzžḻṛṟfθðṉ]?)([aāäeâôoʌiuɪʊɨʉe̯eëəᵊ])(ː?)([kɦgṅcjñṭḍɕʑʦʣṇɽtdnpbmʰʱɽ̃yrlɳwvɾjwśṣshqxġzžḻṛṟfθðṉ]?)([aāäeâôoʌiuɪʊe̯eëəᵊ])(ː?)([kɦgṅcjñṭḍṇɽtdnpbmɽ̃yɕʑrlɳʦʣwvɾjwśṣsʰʱhqxġzžḻṛṟfθðṉɨʉ]?)$", "%1%3%4%5%6%7%8%9")
result= gsub(result, "([aāäeâôoʌiuɪʊe̯eëəᵊ])(ː)([kɦgɕʑṅcjñṭḍṇɽtdnʦʣʰʱpbmɽ̃yrlɳwvɾjwśṣshqxġzžḻṛṟfθðṉ]?)([aāäeâôoʌiuɪʊɨʉe̯eëəᵊ])(ː?)([kɦgṅcjʰʱñṭḍṇɽtdnpbmɽ̃yrlɳwvɾjwśṣshqxġzžḻṛṟfθðṉ]?)([kɦgṅcjñṭḍṇɽtdnpbmɽ̃yrlɳwvɾjwśṣsʰʱhɕʑqxʦʣġzžḻṛṟfθðṉɨʉ]?)([aāäeâôoʌiuɪʊe̯eëəᵊ])(ː?) ", "%1%3%4%5%6%7%8%9 ")
result= gsub(result, "([aāäeâôoʌiuɪʊe̯eëəᵊ])(ː)([kɦgṅʦʣcɕʑjñṭḍṇɽtdnpbmɽ̃yrʰʱlɳwvɾjwśṣshqxġzžḻṛṟfθðṉ]?)([aāäeâôoʌiuɪʊɨʉe̯eëəᵊ])(ː?)([kɦgṅcjñṭḍɕʑʦʣṇɽtdnpbmʰʱɽ̃yrlɳwvɾjwśṣshqxġzžḻṛṟfθðṉ]?)([aāäeâôoʌiuɪʊe̯eëəᵊ])(ː?)([kɦgṅcjñṭḍṇɽtdnpbmɽ̃yɕʑrlɳʦʣwvɾjwśṣsʰʱhqxġzžḻṛṟfθðṉɨʉ]?) ", "%1%3%4%5%6%7%8%9 ")
    result = gsub(result, "ː̃", "̃ː")
    result = gsub(result, "ʦ", "t͡ɕ")
    result = gsub(result, "ʣ", "d͡ʑ")
    result = gsub(result, "ː.̃", "̃ː.")
    result = gsub(result, "%. ", " ")
    result = gsub(result, "%.$", " ")
    result = gsub(result, "%.?%-", ".")
    result = gsub(result, "ː%.̃", "̃ː.")

	return result
end

function export.make(frame)
	local args = frame:getParent().args
	local pagetitle = mw.title.getCurrentTitle().text
	
	local p, results = {}, {}
	
	if args[1] then
		for index, item in ipairs(args) do
			table.insert(p, (item ~= "") and item or nil)
		end
	else
		p = { pagetitle }
	end
	
	for _, Maithili in ipairs(p) do
		table.insert(results, { pron = "[" .. export.toIPA(Maithili) .. "]" })
		if export.toIPA(Maithili, true) ~= export.toIPA(Maithili) then
			table.insert(results, { pron = "[" .. export.toIPA(Maithili, true) .. "]" })
		end
	end
	
	return m_IPA.format_IPA_full { lang = lang, items = results }
end

return export