local export = {}

local lang = require("Module:languages").getByCode("si")
local sc = require("Module:scripts").getByCode("Sinh")
local m_IPA = require("Module:IPA")
local gsub = mw.ustring.gsub

local consonants = {
	['ක']='k', ['ඛ']='k', ['ග']='ɡ', ['ඝ']='ɡ', ['ඞ']='ŋ', ['ඟ']='ᵑɡ', 
	['ච']='t͡ʃ', ['ඡ']='t͡ʃ', ['ජ']='d͡ʒ', ['ඣ']='d͡ʒ', ['ඤ']='ɲ', ['ඥ']='ɡn', ['ඦ']='ᶮd͡ʒ', 
	['ට']='ʈ', ['ඨ']='ʈ', ['ඩ']='ɖ', ['ඪ']='ɖ', ['ණ']='ɳ', ['ඬ']='ᶯɖ',
	['ත']='t̪', ['ථ']='t̪', ['ද']='d̪', ['ධ']='d̪', ['න']='n̪', ['ඳ']='ⁿ̪d̪', 
	['ප']='p', ['ෆ']='f', ['ඵ']='p', ['බ']='b', ['භ']='b', ['ම']='m', ['ඹ']='ᵐb', 
	['ය']='j', ['ර']='r', ['ල']='l', ['ව']='w', 
	['ශ']='ʃ', ['ෂ']='ʃ', ['ස']='s', ['හ']='h', ['ළ']='ɭ',
}

local vowel_diacritics  = {
	['ා'] = 'aː', ['ැ'] = 'æ', ['ෑ'] = 'æː',	['ි'] = 'i',	['ී'] = 'iː', ['ු'] = 'u', ['ූ'] = 'uː',
	['ෙ'] = 'e', ['ේ'] = 'eː', ['ෛ'] = 'ai', ['ො'] = 'o', 	['ෝ'] = 'oː', ['ෞ'] = 'au',
	['ෘ'] = 'ru', ['ෟ'] = 'lu', ['ෲ'] = 'ruː', ['ෳ'] = 'luː',
	[''] = 'ə',
}

local other = {
	-- vowels
	['අ']='ə', ['ආ']='aː', ['ඇ']='æ', ['ඈ']='æː', ['ඉ']='i',  ['ඊ']='iː',  ['උ']='u', ['ඌ']='uː', 
	['එ']='e', ['ඒ']='eː', ['ඓ']='ai', ['ඔ']='o', ['ඕ']='oː', ['ඖ']='au', 
	['ඍ']='ru', ['ඎ']='ruː', ['ඏ']='lu', ['ඐ']='luː' ,     
	-- other symbols
	['ං']='m̃', -- anusvara
	['ඃ']='h',  -- visarga
	['්']= '',  --hal kirīma, suppresses the inherent vowel "a"
	-- punctuation
	['෴']='' ,  -- kunddaliya (obsolete)    
}

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

local adjust2 = {
	['d͡ʒɲ']='ɟɲ',
	['t͡ʃt͡ʃ']='t̚t͡ʃ', ['d͡ʒd͡ʒ']='d̚d͡ʒ', ['ɲd͡ʒ']='n̠ʲd͡ʒ',
--	['hp']='pp', ['hk']='kk', ['hs']='ss', ['hʃ']='ʃʃ', ['hʂ']='ʂʂ',
}

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)
			return consonants[c] .. vowel_diacritics[d]
		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,
			{ { pron = '/' .. transcriptions[1] .. '/' } })
	else
		IPA_text = m_IPA.format_IPA_full(
			lang,
			{ { pron = '/' .. transcriptions[1] .. '/' }, { pron = '[' .. transcriptions[2] .. ']' } })
	end

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

end

return export