local export = {}

local m_IPA = require("Module:IPA")
local lang = require("Module:languages").getByCode("amf")
local m_para = require("Module:parameters")
local utilities = require("Module:amf-utilities")

local rsub = mw.ustring.gsub
local rlower = mw.ustring.lower
local gmatch = mw.ustring.gmatch

local correspondences = {
	c = "tʃ",
	g = "ɡ",
	j = "dʒ",
	y = "j",
	B = "ɓ",
	C = "tʃʼ",
	D = "ɗ",
	E = "ɛ",
	G = "ɠ",
	N = "ɲ",
	O = "ɔ",
	S = "ʃ",
	T = "tʼ",
	Q = "ʔ",
	L = "ː",		-- long
	X = "\204\164",	-- breathy
	H = "\204\165",	-- voiceless
}

local V = "aeiouEO"

-- custom version of utilities.combine
local function combine(syllables)
	local a,f = syllables.accent, syllables.falling
	local diacritic = f and "\204\130" or "\204\129"
	local word = "" -- do not use table.concat to avoid modifying input
	for i,syl in ipairs(syllables) do
		if i == a then
			syl = syl:gsub("[aeiouEO]","%0"..diacritic)
		end
		word = word .. syl
	end
	word = word:gsub("[cgjyBCDEGNOSTQLXH]",correspondences)
	return mw.ustring.toNFC(word)
end

local function pron(text)
	text = utilities.syllabify(text)
	if text.accent == 0 then
		text.accent = 1
	end
	for i, syl in ipairs(text) do
		text[i] = syl:gsub("([" .. V .. "])%1", "%1L")
	end
	text[1] = text[1]:gsub("^([" .. V .. "])", "ʔ%1")
	return text
end

local function phon(text,last)
	local C = "rCDB"
	for i=1,#text do
		if i>1 and text[i-1]:sub(-1,-1):match("[" .. V .. "L]") then
			text[i] = text[i]:gsub("^[" .. C .. "]", {r="ɾ", C="c", D="d", B="β"})
				:gsub("^ba", "βa")
		end
		if text[i]:sub(-1,-1) == "n" and i<#text and text[i+1]:sub(1,1):match("[kg]") then
			text[i] = text[i]:gsub("n$", "ŋ")
		end
		text[i] = text[i]:gsub("^T([ai])", "tsʼ%1")
		text[i] = text[i]:gsub("n([kg])$", "ŋ%1")
	end
	text[1] = text[1]:gsub("^Du", "ʔu")
		:gsub("^ha", "a̤")
		:gsub("^q[aeiouEO]", {qa="qʼa",qe="qʰe",qi="qʰi",qE="qʰE",qO="qʰO",qo="ʔo",qu="ʔu"})
		:gsub("^([ptk])", "%1ʰ")
		:gsub("tʰsʼ", "tsʼ")
	text[#text] = text[#text]:gsub("[pbBmnrl]$", {p="p̚",b="b̚",B="ɓ̚",m="m̥",n="n̥",r="r̥",l="l̥"})
	if text.accent ~= #text then
		text[#text] = text[#text]:gsub("^q([aeiouEO])$", "qʰ%1")
	end
	if last then -- make the utterance-final vowel (short/long/diphthong) breathy if stressed, voiceless if unstressed
		text[#text] = text[#text]:gsub("[aeiouEO]+R?$", function(vowels)
			vowels = vowels:gsub("[aeiouEO]","%1"..(text.accent == #text and "X" or "H"))
			return vowels
		end)
	end
	return text
end

function export.make(frame)
	local args = m_para.process(frame:getParent().args,{
		[1] = { list = true, default = mw.loadData("Module:headword/data").pagename }
	})
	local results = {}
	for _, term in ipairs(args[1]) do
		local phonemic, phonetic = {}, {}
		local words = mw.text.split(term, " ", true)
		for i,word in ipairs(words) do
			local syllabified = pron(word)
			phonemic[i] = combine(syllabified)
			phonetic[i] = combine(phon(syllabified,i==#words))
		end
		phonemic = table.concat(phonemic, " ")
		phonetic = table.concat(phonetic, " ")
		table.insert(results, { pron = "/" .. phonemic .. "/" })
		if phonemic ~= phonetic then
			table.insert(results, { pron = "[" .. phonetic .. "]" })
		end
	end
	return m_IPA.format_IPA_full { lang = lang, items = results }
end

return export