local export = {}

local lang = require("Module:languages").getByCode("xsr")
local sc = require("Module:scripts").getByCode("Deva")
local sc = require("Module:scripts").getByCode("Tibt")
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"] = "t͡ʃ", 
        ["j"] = "d͡ʒ", ["ċ"] = "c", ["j̈"] = "ɟ", 
        ["C"] = "t͡s", ["Z"] = "d͡z", 
        ["ñ"] = "ɲ", ["ṭ"] = "ʈ", ["ḍ"] = "ɖ", 
        ["ṇ"] = "ɳ", ["t"] = "t̪", ["d"] = "d̪",
	    ["y"] = "j", ["r"] = "ɾ", ["w"] = "w",
        ["l"] = "l", ["ś"] = "ʃ", ["ṣ"] = "s",
        ["h"] = "h", ["n"] = "n", ["ž"] = "ʒ",
        ["'"] = "ʔ", ["s"] = "s",

        ["z"] = "z", ["ġ"] = "ɣ", ["q"] = "q", 
        ["x"] = "x", ["θ"] = "θ", ["ð"] = "ð", 
        ["f"] = "f", ["ḻ"] = "ɭ", ["ṟ"] = "ɹ", 
        ["r̥"] = "ri", ["ṃ"] = "̃", [","] = ",", 
        ["V"] = "v", ["ṛ"] = "ɽ", 

	["a"] = "a", ["ā"] = "ɑ", ["i"] = "i",
	["ī"] = "i", ["o"] = "o", ["e"] = "e",
	["u"] = "u", ["ū"] = "u", ["ai"] = "ai", 
    ["au"] = "au", ["ŏ"] = "ɒ", ["ĕ"] = "æ", 

        ["āu"] = "ɑu̯",  ["āi"] = "ɑi̯",["ũ"] = "ũ",
        ["õ"] = "õ", ["ã"] = "ã", ["ā̃"] = "ɑ̃",
        ["ẽ"] = "ẽ",

	["ḥ"] = "hʌ",  [" "] = " ",
}

local vowels = "aāā̃ẽõiīuūeī̃ū̃ĩɑɑ̃oʌʌ̃ãũ"
local weak_h_c = "gjzdḍd̪ṇbṛvṟwy"
local weak_h = "([" .. weak_h_c .. "])h"
local aspirate = "([ksctṭ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ñṭḍṇtdnpbmyrlɳwvɾjwśṣshqxġzžḻṛṟfθðṉ]$") or find(current .. ch, "^[kgcjṭḍṇtdpbṛṟ]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, phonetic)
	local translit = transliterate(text)
	if not translit then
		error('The term "' .. Sherpa .. '" could not be transliterated.')
	end

	if phonetic then
	end
	local result = gsub(translit, ".", correspondences)
		
	
        translit = gsub(translit, "͠", "̃")
        translit = gsub(translit, "(...)*$", "%1ʌ")  
	translit = gsub(translit, "%-", " ")
	translit = gsub(translit, "r̥", "ri")
	translit = gsub(translit, "ky", "ċ")
	translit = gsub(translit, "khy", "ċʰ")
	translit = gsub(translit, "gy", "ɟ")
	translit = gsub(translit, "ghy", "ɟʱ")
    translit = gsub(translit, "ts", "C")
	translit = gsub(translit, "tsh", "Cʰ")
	translit = gsub(translit, "dz", "Z")
	translit = gsub(translit, "dzh", "Zʱ")
    translit = gsub(translit, "ng", "ŋ")
    translit = gsub(translit, "ny", "ɲ")
    translit = gsub(translit, "sh", "ʃ")
    translit = gsub(translit, "zh", "ʒ")
	translit = gsub(translit, "rh", "ɾ̥")
    translit = gsub(translit, "hr", "ɾ̥")
	translit = gsub(translit, "lh", "l̥")
	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, "ː̃", "̃ː")
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 _, Sherpa in ipairs(p) do
		table.insert(results, { pron = "/" .. export.toIPA(Sherpa) .. "/" })
		if export.toIPA(Sherpa, true) ~= export.toIPA(Sherpa) then
			table.insert(results, { pron = "[" .. export.toIPA(Sherpa, true) .. "]" })
		end
	end
	
	return m_IPA.format_IPA_full { lang = lang, items = results }
end

return export