local export = {}

local m_IPA = require("Module:IPA")
local lang = require("Module:languages").getByCode("alu")
local m_hyphenate = require("Module:hyphenation")

local phoneme_map = {
		-- Consonants
	'm', 'm',
	'n', 'n',
	'p', 'p',
	't', 't',
	'k', 'k',
	'\'', 'ʔ',	
	'r', 'ɾ',
	's', 's',
	'h', 'h',
	'w', 'w',
		-- Vowels
	'aa', 'aː',
	'ee', 'eː',
	'ii', 'iː',
	'oo', 'oː',
	'uu', 'uː',
}

local syllable_map = {
	-- Consonants
	'm', '.m',
	'n', '.n',
	'p', '.p',
	't', '.t',
	'k', '.k',
	'\'', '.\'',
	'r', '.r',
	's', '.s',
	'h', '.h',
	'w', '.w',
		-- Vowels
	'ae', 'a.e',
	'ua', 'u.a',
	'io', 'i.o',
	'iu', 'i.u',
}

local function phonemic(text)
	for i = 1, #phoneme_map, 2 do
		text = mw.ustring.gsub(text, phoneme_map[i], phoneme_map[i+1])
	end
	return(text)
end

local function syllabic(text)
    local syls = {}
	for i = 1, #syllable_map, 2 do
		text = mw.ustring.gsub(text, syllable_map[i], syllable_map[i+1])
	end
	local i = 0
	for i in string.gmatch(text, "([^"..".".."]+)") do
		table.insert(syls, i)
	end
	local data = {
		lang = lang,
		hyphs = {{hyph = syls}},
	}
	return data
end

function export.show(frame, alt) 
	if pcall(function() local args = frame:getParent().args end) 
		then args = frame:getParent().args  
		else args = frame end
	
	if not args[1] or (args[1] == "") then
		text = mw.title.getCurrentTitle().text
		text = mw.ustring.lower(text)
	else
		text = args[1]
	end
	if (alt) then return {'/'..phonemic(text)..'/', syllabic(text)} end
	local t_ipa = {}
	table.insert(t_ipa, {pron = '/' .. phonemic(text) .. '/'})
	
	local t_syllabic = m_hyphenate.format_hyphenations(syllabic(text))
	local output = ("* " .. m_IPA.format_IPA_full { lang = lang, items = t_ipa } .. "\n* " .. t_syllabic)
	return(output)
end

return export