local export = {}

local m_IPA = require("Module:IPA")
local title = mw.title.getCurrentTitle().text
local lang = require("Module:languages").getByCode("aa")
local rsub = mw.ustring.gsub
local rlower = mw.ustring.lower

local V = "[aeiou]"
local C = "[bħdfɡhɟklmnpʕrstwɖjzʃ]"
local Ci = "[bcdfghjklmnpqrstwxz]"

local phon = {
	["a"]="a", ["b"]="b", ["c"]="ħ", ["d"]="d", ["e"]="e", ["f"]="f",
	["g"]="ɡ", ["h"]="h", ["i"]="i", ["j"]="ɟ", ["k"]="k", ["l"]="l",
	["m"]="m", ["n"]="n", ["o"]="o", ["p"]="p", ["q"]="ʕ", ["r"]="r",
	["s"]="s", ["t"]="t", ["u"]="u", ["w"]="w", ["x"]="ɖ", ["y"]="j",
	["z"]="z", ["á"]="aˈ", ["é"]="eˈ", ["í"]="iˈ", ["ó"]="oˈ", ["ú"]="uˈ",
	["à"]="aˌ", ["è"]="eˌ", ["ì"]="iˌ", ["ò"]="oˌ", ["ù"]="uˌ",
}

local function phonemic(text)
	text = rlower(text)
	-- general phonology
	text = rsub(text, ".", phon)
	-- digraphs
	text = rsub(text, "(" .. V .. ")([ˈˌ]?)%1", "%1ː%2")
	text = rsub(text, "sh", "ʃ")
	text = rsub(text, "(" .. C .. "?)(" .. V .. "ː?)([ˈˌ])", "%3%1%2")
	text = rsub(text, "(" .. C .. ")%1", "%1ː")
	text = rsub(text, "ɟ", "d͡ʒ")
	
	return(text)
end

local function phonetic(text)
	text = phonemic(text)
	-- retroflex
	text = rsub(text, "(" .. V .. "[ˈˌ]?)ɖ(" .. V .. ")", "%1ɽ%2")
	text = rsub(text, "n([ˈˌ]?)ɖ", "ɳ%1ɖ")
	text = rsub(text, "s([ˈˌ]?)ɖ", "ʂ%1ɖ")
	-- other consonants
	text = rsub(text, "n([ˈˌ]?)([kɡ])", "ŋ%1%2")
	text = rsub(text, "r", "ɾ")
	text = rsub(text, "ɾː", "rː")
	text = rsub(text, "ɾ([ˈˌ])ɾ", "r%1r")
	text = rsub(text, "([kpt])([ˈˌ])", "%1ʰ%2")
	text = rsub(text, "([kpt])$", "ʰ%1")
	-- vowels
	text = rsub(text, "^([ˈˌ]?)(" .. V .. ")", "%1ʔ%2")
	text = rsub(text, "a", "ʌ")
	text = rsub(text, "e", "ɛ")
	text = rsub(text, "i", "ɪ")
	text = rsub(text, "o", "ɔ")
	text = rsub(text, "u", "ʊ")
	text = rsub(text, "ʕʌ", "ʕa")
	text = rsub(text, "ʕɛ", "ʕe")
	text = rsub(text, "ʕɪ", "ʕi")
	text = rsub(text, "ʕɔ", "ʕo")
	text = rsub(text, "ʕʊ", "ʕu")
	text = rsub(text, "ʌː", "aː")
	text = rsub(text, "ɛː", "eː")
	text = rsub(text, "ɪː", "iː")
	text = rsub(text, "ɔː", "oː")
	text = rsub(text, "ʊː", "uː")
	
	return(text)
end

function export.syllabify(term) --split for hyphenation
	local H, i = {}, 0
 	for a in string.gmatch(rsub(term, "([aeiou]" .. Ci .. "?)(" .. Ci .. ")%f[aeiou]", "%1.%2"), "[^%.-/]+") do
 		i = i+1
 		H[i] = a
 	end
 	return H
end

function export.show(frame)
	local args = frame:getParent().args
	local p, phol, phot = {}, {}, {}
	if not args[1] or (args[1] == "") then
		error("Please put the word as the first positional parameter!")
	else
		for index, item in ipairs(args) do
			table.insert(p, (item ~= "") and item or nil)
		end
	end
	for _, word in ipairs(p) do
		table.insert(phol, {pron = "/" .. phonemic(word) .. "/"})
		table.insert(phot, {pron = "[" .. phonetic(word) .. "]"})
	end

	local H = ""
	if mw.ustring.match(title, "^[^%-].+[^%-]$") then
		H = export.syllabify(title)
		table.insert(H, 1, "aa") -- language code argument, ie. MOD:hyphenation format, next L
 		H = "\n* " .. require("Module:hyphenation").hyphenate(H)
	end

	return "* " .. m_IPA.format_IPA_full { lang = lang, items = phol } .. ", " .. m_IPA.format_IPA_multiple(lang, phot) .. H
end 

return export