local export = {}

local consonants = {
	['ಕ']='k', ['ಖ']='kʰ', ['ಗ'] = 'ɡ', ['ಘ'] ='ɡʱ', ['ಙ']='ŋ',
	['ಚ']='t͡ʃ', ['ಛ']='t͡ʃʰ', ['ಜ']='d͡ʒ', ['ಝ']='d͡ʒʱ', ['ಞ']='ɲ',
	['ಟ']='ʈ', ['ಠ']='ʈʰ', ['ಡ']='ɖ', ['ಢ']='ɖʱ', ['ಣ']='ɳ',
	['ತ']='t̪', ['ಥ']='t̪ʰ', ['ದ']='d̪', ['ಧ']='d̪ʱ', ['ನ']='n̪', 
	['ಪ']='p', ['ಫ']='pʰ', ['ಬ']='b', ['ಭ']='bʱ',  ['ಮ']='m',
	['ಯ']='j', ['ರ']='ɾ', ['ಲ']='l', ['ವ']='ʋ', 
	['ಶ']='ʃ', ['ಷ']='ʂ', ['ಸ']='s', ['ಹ']='h', 
	['ಳ']='ɭ', ['ೞ']='ɻ', ['ಱ']='r', 
}

local vowel_diacritics = {
	['ಾ']= 'aː', ['ಿ']='i', ['ೀ']='iː', ['ು']='u', ['ೂ']='uː', ['ೃ'] = 'ɾi', ['ೄ'] = 'ɾiː', ['ೢ'] = 'li', ['ೣ'] = 'liː',
	['ೆ']='e', ['ೇ']='eː', ['ೈ']='ɐi̯', ['ೊ']='o', ['ೋ']='oː', ['ೌ']='ɐu̯',
	['್']='',	-- Virama - suppresses the inherent vowel "a"
	[''] = 'ɐ'	-- No diacritic; inherent vowel	
}

local other = {
	-- independent vowels
	['ಅ']='ɐ', ['ಆ']='aː', ['ಇ']='i', ['ಈ']='iː', ['ಉ']='u', ['ಊ']='uː', ['ಋ'] = 'ɾi', ['ೠ'] = 'ɾiː', ['ಌ'] = 'li', ['ೡ'] = 'liː',
	['ಎ']='e', ['ಏ']='eː', ['ಐ']='ɐi̯', ['ಒ']='o', ['ಓ']='oː', ['ಔ']='ɐu̯',
	-- Other symbols
	['ಂ']='m̃', ['ಃ']='h',
	['ೝ']='n',
	['ಽ']='',
}

local adjust1 = {
	-- Assimilate the anusvara
	['m̃([kɡŋ])']='ŋ%1',
	['m̃([td]͡[ʃʒ])']='ɲ%1', ['m̃(ɲ)']='ɲ%1',
	['m̃([ʈɖɳ])']='ɳ%1',
	['m̃([td]̪)']='n̪%1', ['m̃([td]͡[sz])']='n̪%1', ['m̃(n̪)']='n̪%1',
	['m̃([pbmjɾlʋʃʂshɭ])']='m%1',
	['m̃([%s%p])']='m%1', ['m̃$']='m'
}

local adjust2 = {
	-- Account for differences in phonemes vs. phones
}

function export.to_IPA(text)

	text = mw.ustring.gsub(
		text,
		'([ಕ-ಹ])(಼?)([ಾ-್]?)',
		function(c, n, d)
			return ((consonants[c..n] or consonants[c]) or c) .. vowel_diacritics[d]
		end)

	text = mw.ustring.gsub(text, '[ಂ-ೡ]', other)

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

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

	-- Phonetic transcription
	text2 = text
	for k, v in pairs(adjust2) do
		text2 = mw.ustring.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 = require("Module:IPA").format_IPA_full {
			lang = require("Module:languages").getByCode("xuj"),
			items = {{ pron = "/" .. transcriptions[1] .. "/" }},
		}
	else
		IPA_text = require("Module:IPA").format_IPA_full {
			lang = require("Module:languages").getByCode("xuj"),
			items = {{ pron = "/" .. transcriptions[1] .. "/" }, { pron = "[" .. transcriptions[2] .. "]" }},
		}
	end

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

return export