local export = {}

local lang = require("Module:languages").getByCode("or")
local sc = require("Module:scripts").getByCode("Orya")
local m_IPA = require("Module:IPA")
local u = mw.ustring.char
local gsub = mw.ustring.gsub

local consonants = {
	--common
	["କ"]="k", ["ଖ"]="kʰ", ["ଗ"]="ɡ", ["ଘ"]="ɡʱ", ["ଙ"]="ŋ",
	["ଚ"]="t͡ʃ", ["ଛ"]="t͡ʃʰ", ["ଜ"]="d͡ʒ", ["ଝ"]="d͡ʒʱ", ["ଞ"]="n̪",
	["ଟ"]="ʈ", ["ଠ"]="ʈʰ", ["ଡ"]="ɖ", ["ଢ"]="ɖʱ", ["ଣ"]="ɳ",
	["ତ"]="t̪", ["ଥ"]="t̪ʰ", ["ଦ"]="d̪", ["ଧ"]="d̪ʱ", ["ନ"]="n̪",
	["ପ"]="p", ["ଫ"]="pʰ", ["ବ"]="b", ["ଭ"]="bʱ", ["ମ"]="m",
	["ଯ"]="d͡ʒ", ["ୟ"]="j", ["ର"]="ɾ", ["ଲ"]="l", ["ଳ"]="ɭ",
	["ଵ"]="ʋ", ["ୱ"]="w", ["କ୍ଷ"]="kʰj", ["ଶ"]="s", ["ଷ"]="s", ["ସ"]="s", ["ହ"]="h",
	--nuktas
    ["କ଼"]="k", ["ଖ଼"]="kʰ", ["ଗ଼"]="ɡʱ", ["ଜ଼"]="d͡ʒ", ["ଝ଼"]="d͡ʒʱ", ["ଫ଼"]="pʰ",
    ["ଡ଼"]="ɽ", ["ଢ଼"]="ɽʱ",
}

local vowel_diacritics  = {
	["ା"]="a", ["ି"]="i", ["ୀ"]="i", ["ୁ"]="u", ["ୂ"]="u",
	["େ"]="e", ["ୈ"]="ɔi", ["ୋ"]="o", ["ୌ"]="ɔu",
	["ୃ"]="ɾu", ["ୄ"]="ɾu", ["ୢ"]="lu", ["ୣ"]="lu",
	["୍"]="", [""] = "ɔ",
}

local other = {
	["ଅ"]="ɔ", ["ଆ"]="a", ["ଇ"]="i", ["ଈ"]="i", ["ଉ"]="u", ["ଊ"]="u",
	["ଏ"]="e", ["ଐ"]="ɔi̯", ["ଓ"]="o", ["ଔ"]="ɔu̯", ["ଉ"]="u", ["ଊ"]="u",
	["ଋ"]="ɾu", ["ୠ"]="ɾu", ["ଌ"]="lu", ["ୡ"]="lu",
	-- Other symbols
	['ଂ']='m̃', ['ଃ']='h',
}

local adjust1 = {
	-- Assimilate the anusvara
	['m̃([kɡŋ])']='ŋ%1',
	['m̃([td]͡[ʃʒ])']='n̪%1',
	['m̃([ʈɖɳ])']='ɳ%1',
	['m̃([td]̪)']='n̪%1', ['m̃(n̪)']='n̪%1',
	['m̃([pbm])']='m%1',
	['m̃([%s%p])']='ŋ%1', ['m̃$']='ŋ',
	['([aeiou])୕']='%1ː',
}

local adjust2 = {
	['n̪d͡ʒ']='n̠ʲd͡ʒ', ['n̪t͡ʃ']='n̠ʲt͡ʃ',
}

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

function export.to_IPA(text)

	text = gsub(
		text,
		"([କ-ହୟୱ]଼?)([ା-୍]?)([ଁଂ]?)",
		function(c, d, a)
			return consonants[c] .. vowel_diacritics[d] .. (a ~= "" and u(0x0303) or "")
		end)
	text = gsub(
		text,
		"([ଅ-ଔୠୡ])([ଁଂ]?)",
		function(n, a)
			return other[n] .. (a ~= "" and u(0x0303) or "")
		end)


	for k, v in pairs(adjust1) do
		text = gsub(text, k, v)
	end

	-- If an independent vowel is after another vowel, assume diphthong
	text = gsub(text, "([ɔaeiou]ː?)•", "%1")
	text = gsub(text, '.', other)

	-- Phonetic transcription
	text2 = text
	for k, v in pairs(adjust2) do
		text2 = gsub(text2, k, v)
	end

	return (text == text2 and { text } or { text, text2 })

end

function export.show(frame)
	local args = frame:getParent().args
	local page_title = mw.title.getCurrentTitle().text
	local text = args[1] or page_title
	local qualifier = args["q"] or nil

	local transcriptions = export.to_IPA(text)
	local IPA_text
	if not transcriptions[2] then
		IPA_text = m_IPA.format_IPA_full {
			lang = lang,
			items = {{ pron = "/" .. transcriptions[1] .. "/" }},
		}
	else
		IPA_text = m_IPA.format_IPA_full {
			lang = lang,
			items = {{ pron = "/" .. transcriptions[1] .. "/" }, { pron = "[" .. transcriptions[2] .. "]" }},
		}
	end

	return "* " .. (qualifier and require("Module:qualifier").format_qualifier{qualifier} .. " " or "")
		.. IPA_text
end

return export