local m_ipa = require("Module:IPA")
local m_a = require("Module:accent qualifier")

local export = {}

local lang = require("Module:languages").getByCode("ca")

local function list_true(list)
	for i, val in ipairs(list) do
		list[val] = true
	end
	return list
end

local valid_onsets = list_true({
	"b", "bl", "br",
	"c", "cl", "cr",
	"ç",
	"d", "dj", "dr",
	"f", "fl", "fr",
	"g", "gl", "gr", "gu", "gü",
	"h",
	"i",
	"j",
	"k", "kl", "kr",
	"l", "ll",
	"m",
	"n", "ny", "ñ",
	"p", "pl", "pr",
	"qu", "qü",
	"r", "rr",
	"s", "ss",
	"t", "tg", "tj", "tr", "tx", "tz",
	"u",
	"v", "vl",
	"w",
	"x",
	"z",
})

local function fix_prefixes(word)
	-- Orthographic fixes for unassimilated prefixes
	local prefix = {
		"a[eè]ro", "ànte", "[aà]nti", "[aà]rxi", "[aà]uto", -- a- is ambiguous as prefix
		"bi", "b[ií]li", "bio",
		"c[oò]ntra", -- ambiguous co-
		"dia", "dodeca",
		"[eé]ntre", "equi", "estereo", -- ambiguous e-(radic)
		"f[oó]to",
		"g[aà]stro", "gr[eé]co",
		"hendeca", "hepta", "hexa", "h[oò]mo",
		"[ií]nfra", "[ií]ntra",
		"m[aà]cro", "m[ií]cro", "mono", "morfo", "m[uú]lti",
		"n[eé]o",
		"octo", "orto",
		"penta", "p[oòô]li", "pol[ií]tico", "pr[oòô]to", "ps[eèê]udo", "psico", -- ambiguous pre-(s), pro-
		"qu[aà]si", "qu[ií]mio",
		"r[aà]dio", -- ambiguous re-
		"s[eèê]mi", "s[oó]bre", "s[uú]pra",
		"termo", "tetra", "tri", -- ambiguous tele-(r)
		"[uú]ltra", "[uu]n[ií]",
		"v[ií]ce"
	}
	local prefix_r = {"[eèéê]xtra", "pr[eé]"}
	local prefix_s = {"antropo", "centro", "deca", "d[ií]no", "eco", "[eèéê]xtra",
		"hetero", "p[aà]ra", "post", "pré", "s[oó]ta", "tele"}
	local prefix_i = {"pr[eé]", "pr[ií]mo", "pro", "tele"}
	local no_prefix = {"autoic", "autori", "biret", "biri", "bisa", "bisell", "bisó", "biur", "contrari", "contrau",
		"diari", "equise", "heterosi", "monoi", "parasa", "parasit", "preix", "psicosi", "sobrera", "sobreri"}
	
	-- False prefixes
	for _, pr in ipairs(no_prefix) do
		if mw.ustring.find(word, "^" .. pr) then
			return word
		end
	end
	
	-- Double r in prefix + r + vowel
	for _, pr in ipairs(prefix_r) do
		word = mw.ustring.gsub(word, "^(" .. pr .. ")r([aàeèéiíïoòóuúü])", "%1rr%2")
	end
	word = mw.ustring.gsub(word, "^eradic", "erradic")
	
	-- Double s in prefix + s + vowel
	for _, pr in ipairs(prefix_s) do
		word = mw.ustring.gsub(word, "^(" .. pr .. ")s([aàeèéiíïoòóuúü])", "%1ss%2")
	end
	
	-- Hiatus in prefix + i
	for _, pr in ipairs(prefix_i) do
		word = mw.ustring.gsub(word, "^(" .. pr .. ")i(.)", "%1ï%2")
	end
	
	-- Both prefix + r/s or i/u
	for _, pr in ipairs(prefix) do
		word = mw.ustring.gsub(word, "^(" .. pr .. ")([rs])([aàeèéiíïoòóuúü])", "%1%2%2%3")
		word = mw.ustring.gsub(word, "^(" .. pr .. ")i(.)", "%1ï%2")
		word = mw.ustring.gsub(word, "^(" .. pr .. ")u(.)", "%1ü%2")
	end
	
	-- Voiced s in prefix roots -fons-, -dins-, -trans-
	word = mw.ustring.gsub(word, "^enfons([aàeèéiíoòóuú])", "enfonz%1")
	word = mw.ustring.gsub(word, "^endins([aàeèéiíoòóuú])", "endinz%1")
	word = mw.ustring.gsub(word, "tr([aà])ns([aàeèéiíoòóuúbdghlmv])", "tr%1nz%2")
	
	-- in + ex > ineks/inegz
	word = mw.ustring.gsub(word, "^inex", "inhex")
	
	return word
end

local function restore_diaereses(word)
	-- Some structural forms do not have diaeresis per diacritic savings, let's restore it to identify hiatus
	
	word = mw.ustring.gsub(word, "([iu])um(s?)$", "%1üm%2") -- Latinisms (-ius is ambiguous but rare)
	
	word = mw.ustring.gsub(word, "([aeiou])isme(s?)$", "%1ísme%2") -- suffix -isme
	word = mw.ustring.gsub(word, "([aeiou])ist([ae]s?)$", "%1íst%2") -- suffix -ista
	
	word = mw.ustring.gsub(word, "([aeou])ir$", "%1ír") -- verbs -ir
	word = mw.ustring.gsub(word, "([aeou])int$", "%1ínt") -- present participle
	word = mw.ustring.gsub(word, "([aeo])ir([éà])$", "%1ïr%2") -- future
	word = mw.ustring.gsub(word, "([^gq]u)ir([éà])$", "%1ïr%2")
	word = mw.ustring.gsub(word, "([aeo])iràs$", "%1ïràs")
	word = mw.ustring.gsub(word, "([^gq]u)iràs$", "%1ïràs")
	word = mw.ustring.gsub(word, "([aeo])ir(e[mu])$", "%1ïr%2")
	word = mw.ustring.gsub(word, "([^gq]u)ir(e[mu])$", "%1ïr%2")
	word = mw.ustring.gsub(word, "([aeo])iran$", "%1ïran")
	word = mw.ustring.gsub(word, "([^gq]u)iran$", "%1ïran")
	word = mw.ustring.gsub(word, "([aeo])iria$", "%1ïria") -- conditional
	word = mw.ustring.gsub(word, "([^gq]u)iria$", "%1ïria")
	word = mw.ustring.gsub(word, "([aeo])ir(ie[sn])$", "%1ïr%2")
	word = mw.ustring.gsub(word, "([^gq]u)ir(ie[sn])$", "%1ïr%2")
	
	return word
end

local function fix_y(word)
	-- y > vowel i else consonant /j/, except ny
	
	word = mw.ustring.gsub(word, "ny", "ñ")
	
	word = mw.ustring.gsub(word, "y([^aeiouàèéêíòóôúïü])", "i%1") -- vowel if not next to another vowel
	word = mw.ustring.gsub(word, "([^aeiouàèéêíòóôúïü·%-%.])y", "%1i") -- excluding also syllables separators
	
	return word
end

local function word_fixes(word)
	word = mw.ustring.gsub(word, "%-([rs]?)", "-%1%1")
	word = mw.ustring.gsub(word, "rç$", "rrs") -- silent r only in plurals -rs
	word = fix_prefixes(word) -- internal pause after a prefix
	word = restore_diaereses(word) -- no diaeresis saving
	word = fix_y(word) -- ny > ñ else y > i vowel or consonant
	
	return word
end

local function split_vowels(vowels)
	local syllables = {{onset = "", vowel = mw.ustring.sub(vowels, 1, 1), coda = ""}}
	vowels = mw.ustring.sub(vowels, 2)
	
	while vowels ~= "" do
		local syll = {onset = "", vowel = "", coda = ""}
		syll.onset, syll.vowel, vowels = mw.ustring.match(vowels, "^([iu]?)(.)(.-)$")
		table.insert(syllables, syll)
	end
	
	local count = #syllables
	
	if count >= 2 and (syllables[count].vowel == "i" or syllables[count].vowel == "u") then
		syllables[count - 1].coda = syllables[count].vowel
		syllables[count] = nil
	end
	
	return syllables
end

-- Split the word into syllables
local function split_syllables(remainder)
	local syllables = {}
	
	while remainder ~= "" do
		local consonants, vowels
		
		consonants, remainder = mw.ustring.match(remainder, "^([^aeiouàèéêíòóôúïü]*)(.-)$")
		vowels, remainder = mw.ustring.match(remainder, "^([aeiouàèéêíòóôúïü]*)(.-)$")
		
		if vowels == "" then
			syllables[#syllables].coda = syllables[#syllables].coda .. consonants
		else
			local onset = consonants
			local first_vowel = mw.ustring.sub(vowels, 1, 1)
			
			if (mw.ustring.find(onset, "[gq]$") and (first_vowel == "ü" or (first_vowel == "u" and vowels ~= "u")))
			or ((onset == "" or onset == "h") and #syllables == 0 and first_vowel == "i" and vowels ~= "i")
			then
				onset = onset .. mw.ustring.sub(vowels, 1, 1)
				vowels = mw.ustring.sub(vowels, 2)
			end
			
			local vsyllables = split_vowels(vowels)
			vsyllables[1].onset = onset .. vsyllables[1].onset
			
			for _, s in ipairs(vsyllables) do
				table.insert(syllables, s)
			end
		end
	end
	
	-- Shift over consonants from the onset to the preceding coda,
	-- until the syllable onset is valid
	for i = 2, #syllables do
		local current = syllables[i]
		local previous = syllables[i-1]
		
		while not (current.onset == "" or valid_onsets[current.onset]) do
			local letter = mw.ustring.sub(current.onset, 1, 1)
			current.onset = mw.ustring.sub(current.onset, 2)
			if not mw.ustring.find(letter, "[·%-%.]") then -- syllables separators
				previous.coda = previous.coda .. letter
			else
				break
			end
		end
	end
	
	-- Detect stress
	for i, syll in ipairs(syllables) do
		if mw.ustring.find(syll.vowel, "^[àèéêíòóôú]$") then
			syllables.stress = i -- primary stress: the last one stressed
			syll.stressed = true
		end
	end
	
	if not syllables.stress then
		local count = #syllables
		
		if count == 1 then
			syllables.stress = 1
		else
			local final = syllables[count]
			
			if final.coda == "" or final.coda == "s" or (final.coda == "n" and (final.vowel == "e" or final.vowel == "i")) then
				syllables.stress = count - 1
			else
				syllables.stress = count
			end
		end
		syllables[syllables.stress].stressed = true
	end
	
	return syllables
end

local IPA_vowels = {
	["a"] = "a", ["à"] = "a",
	["e"] = "e", ["è"] = "ɛ", ["ê"] = "ɛ", ["é"] = "e",
	["i"] = "i", ["í"] = "i", ["ï"] = "i",
	["o"] = "o", ["ò"] = "ɔ", ["ô"] = "ɔ", ["ó"] = "o",
	["u"] = "u", ["ú"] = "u", ["ü"] = "u",
}

local function replace_context_free(cons)
	cons = mw.ustring.gsub(cons, "ŀ", "l")
	
	cons = mw.ustring.gsub(cons, "r", "ɾ")
	cons = mw.ustring.gsub(cons, "ɾɾ", "r")
	cons = mw.ustring.gsub(cons, "ss", "s")
	cons = mw.ustring.gsub(cons, "ll", "ʎ")
	cons = mw.ustring.gsub(cons, "ñ", "ɲ") -- hint ny > ñ
	
	cons = mw.ustring.gsub(cons, "[dt]j", "d͡ʒ")
	cons = mw.ustring.gsub(cons, "tx", "t͡ʃ")
	cons = mw.ustring.gsub(cons, "[dt]z", "d͡z")
	
	cons = mw.ustring.gsub(cons, "ç", "s")
	cons = mw.ustring.gsub(cons, "[cq]", "k")
	cons = mw.ustring.gsub(cons, "h", "")
	cons = mw.ustring.gsub(cons, "g", "ɡ")
	cons = mw.ustring.gsub(cons, "j", "ʒ")
	cons = mw.ustring.gsub(cons, "x", "ʃ")
	
	cons = mw.ustring.gsub(cons, "i", "j") -- must be after j > ʒ
	cons = mw.ustring.gsub(cons, "y", "j") -- must be after j > ʒ and fix_y
	cons = mw.ustring.gsub(cons, "[uü]", "w")
	
	return cons
end

local function postprocess_general(syllables)
	syllables = mw.clone(syllables)
	
	local voiced = list_true({"b", "d", "ɡ", "m", "n", "ɲ", "l", "ʎ", "r", "ɾ", "v", "z", "ʒ"})
	local voiceless = list_true({"p", "t", "k", "f", "s", "ʃ", ""})
	local voicing = {["k"]="ɡ", ["f"]="v", ["p"]="b", ["t"]="d", ["s"]="z"}
	local devoicing = {["b"]="p", ["d"]="t", ["ɡ"]="k"}
	
	for i = 1, #syllables do
		local current = syllables[i]
		local previous = syllables[i - 1]
		
		-- Coda consonant losses
		if i < #syllables or (i == #syllables and mw.ustring.find(current.coda, "s$")) then
			current.coda = mw.ustring.gsub(current.coda, "m[pb]", "m")
			current.coda = mw.ustring.gsub(current.coda, "([ln])[td]", "%1")
			current.coda = mw.ustring.gsub(current.coda, "n[kɡ]", "ŋ")
		end
		
		-- Consonant assimilations
		if i > 1 then
			-- t + lateral/nasal assimilation
			local cons = mw.ustring.match(current.onset, "^([lʎmn])")
			if cons then
				previous.coda = mw.ustring.gsub(previous.coda, "t$", cons)
			end
			
			-- n + labial > labialized assimilation
			if mw.ustring.find(current.onset, "^[mbp]") then
				previous.coda = mw.ustring.gsub(previous.coda, "n$", "m")
			elseif mw.ustring.find(current.onset, "^[fv]") then
				previous.coda = mw.ustring.gsub(previous.coda, "n$", "m") -- strictly ɱ
			
			-- n + velar > velarized assimilation
			elseif mw.ustring.find(current.onset, "^[ɡk]") then
				previous.coda = mw.ustring.gsub(previous.coda, "n$", "ŋ")
			
			-- l/n + palatal > palatalized assimilation
			elseif mw.ustring.find(current.onset, "^[ʒʎʃɲ]")
			or mw.ustring.find(current.onset, "^t͡ʃ")
			or mw.ustring.find(current.onset, "^d͡ʒ")
			then
				previous.coda = mw.ustring.gsub(previous.coda, "[ln]$", {["l"] = "ʎ", ["n"] = "ɲ"})
			end
			
			-- ɡʒ > d͡ʒ
			if previous.coda == "ɡ" and current.onset == "ʒ" then
				previous.coda = ""
				current.onset = "d͡ʒ"
			end
		end
		
		current.coda = mw.ustring.gsub(current.coda, "n[kɡ]", "ŋk")
		current.coda = mw.ustring.gsub(current.coda, "n([ʃʒ])", "ɲ%1")
		current.coda = mw.ustring.gsub(current.coda, "n(t͡ʃ)", "ɲ%1")
		current.coda = mw.ustring.gsub(current.coda, "n(d͡ʒ)", "ɲ%1")
		
		current.coda = mw.ustring.gsub(current.coda, "l([ʃʒ])", "ʎ%1")
		current.coda = mw.ustring.gsub(current.coda, "l(t͡ʃ)", "ʎ%1")
		current.coda = mw.ustring.gsub(current.coda, "l(d͡ʒ)", "ʎ%1")
		
		current.coda = mw.ustring.gsub(current.coda, "ɲs", "ɲʃ")
		
		-- Voicing or devoicing
		if i > 1 then
			local coda_letter = mw.ustring.sub(previous.coda, -1)
			local onset_letter = mw.ustring.sub(current.onset, 1, 1)
			if voiced[onset_letter] and voicing[coda_letter] then
				previous.coda = mw.ustring.gsub(previous.coda, coda_letter .. "$", voicing[coda_letter])
			elseif voiceless[onset_letter] and devoicing[coda_letter] then
				previous.coda = mw.ustring.gsub(previous.coda, coda_letter .. "$", devoicing[coda_letter])
			else
				previous.coda = mw.ustring.gsub(previous.coda, "[bd]s", {["bs"] = "ps", ["ds"] = "ts"})
			end
		end
		
		-- Allophones of r
		if i == 1 then
			current.onset = mw.ustring.gsub(current.onset, "^ɾ", "r")
		end
		
		if i > 1 then
			if mw.ustring.find(previous.coda, "[lns]$") then
				current.onset = mw.ustring.gsub(current.onset, "^ɾ", "r")
			end
		end
		
		-- Double sound of letter x > ks/gz (on cultisms, ambiguous in onsets)
		current.coda = mw.ustring.gsub(current.coda, "^ʃs?", "ks")
		if i > 1 and previous.coda == "kz" then
			previous.coda = "ɡz" -- voicing the group
		end
		if i > 1 and current.onset == "s" then
			previous.coda = mw.ustring.gsub(previous.coda, "s$", "") -- reduction exs, exc(e/i) and sc(e/i)
		end
		
		if i > 1 and previous.onset == "" and (previous.vowel == "e" or previous.vowel == "ɛ")
		and ((previous.coda == "" and current.onset == "ʃ") or (previous.coda == "ks" and current.onset == ""))
		then
			-- ex + (h) vowel > egz
			previous.coda = "ɡ"
			current.onset = "z"
		end
	end
	
	-- Final devoicing
	local final = syllables[#syllables].coda
	
	final = mw.ustring.gsub(final, "d͡ʒ", "t͡ʃ")
	final = mw.ustring.gsub(final, "d͡z", "t͡s")
	final = mw.ustring.gsub(final, "b", "p")
	final = mw.ustring.gsub(final, "d", "t")
	final = mw.ustring.gsub(final, "ɡ", "k")
	final = mw.ustring.gsub(final, "ʒ", "ʃ")
	final = mw.ustring.gsub(final, "v", "f")
	final = mw.ustring.gsub(final, "z", "s")
	
	-- Final loses
	final = mw.ustring.gsub(final, "j(t͡ʃ)", "%1")
	final = mw.ustring.gsub(final, "([ʃs])s", "%1") -- homophone plurals -xs, -igs, -çs
	
	syllables[#syllables].coda = final
	
	return syllables
end

local function mid_vowel_e(syllables)
	-- most common cases, other ones are supposed ambiguous
	post_consonants = syllables[syllables.stress].coda
	post_vowel = ""
	post_letters = post_consonants
	if syllables.stress == #syllables - 1 then
		post_consonants = post_consonants .. syllables[#syllables].onset
		post_vowel = syllables[#syllables].vowel
		post_letters = post_consonants .. post_vowel .. syllables[#syllables].coda
	end
	
	if syllables[syllables.stress].vowel == "e" then
		if post_vowel == "i" or post_vowel == "u" then
			return "è"
		elseif mw.ustring.find(post_letters, "^ct[ae]?s?$") then
			return "è"
		elseif post_letters == "dre" or post_letters == "dres" then
			return "é"
		elseif mw.ustring.find(post_consonants, "^l") and syllables.stress == #syllables then
			return "è"
		elseif post_consonants == "l" or post_consonants == "ls" or post_consonants == "l·l" then
			return "è"
		elseif (post_letters == "ma" or post_letters == "mes") and #syllables > 2 then
			return "ê"
		elseif post_letters == "ns" or post_letters == "na" or post_letters == "nes" then -- inflection of -è
			return "ê"
		elseif post_letters == "nse" or post_letters == "nses" then
			return "ê"
		elseif post_letters == "nt" or post_letters == "nts" then
			return "é"
		elseif mw.ustring.find(post_letters, "^r[ae]?s?$") then
			return "é"
		elseif mw.ustring.find(post_consonants, "^r[dfjlnrstxyz]") then -- except bilabial and velar
			return "è"
		elseif post_letters == "sos" or post_letters == "sa" or post_letters == "ses" then -- inflection of -ès
			return "ê"
		elseif mw.ustring.find(post_letters, "^t[ae]?s?$") then
			return "ê"
		end
	elseif syllables[syllables.stress].vowel == "è" then
		if post_letters == "s" or post_letters == "" then -- -ès, -è
			return "ê"
		end
	end
	
	return nil
end

local function mid_vowel_o(syllables)
	-- most common cases, other ones are supposed ambiguous
	post_consonants = syllables[syllables.stress].coda
	post_vowel = ""
	post_letters = post_consonants
	if syllables.stress == #syllables - 1 then
		post_consonants = post_consonants .. syllables[#syllables].onset
		post_vowel = syllables[#syllables].vowel
		post_letters = post_consonants .. post_vowel .. syllables[#syllables].coda
	end
	
	if post_vowel == "i" or post_vowel == "u" then
		return "ò"
	elseif mw.ustring.sub(post_letters, 1, 1) == "i" and mw.ustring.sub(post_letters, 1, 2) ~= "ix" then -- diphthong oi
		return "ò"
	elseif mw.ustring.find(post_letters, "^u[^s]") then -- diphthong ou, ambiguous if final
		return "ò"
	elseif #syllables == 1 and (post_letters == "" or post_letters == "s" or post_letters == "ns") then -- monosyllable
		return "ò"
	elseif post_letters == "fa" or post_letters == "fes" then
		return "ò"
	elseif post_consonants == "fr" then
		return "ó"
	elseif post_letters == "ldre" then
		return "ò"
	elseif post_letters == "ma" or post_letters == "mes" then
		return "ó"
	elseif post_letters == "ndre" then
		return "ò"
	elseif mw.ustring.find(post_letters, "^r[ae]?s?$") then
		return "ó"
	elseif mw.ustring.find(post_letters, "^r[ft]s?$") then
		return "ò"
	elseif post_letters == "rme" or post_letters == "rmes" then
		return "ó"
	end
	
	return nil
end

local function to_IPA(syllables, mid_vowel_hint)
	-- Stressed vowel is ambiguous
	if mw.ustring.find(syllables[syllables.stress].vowel, "[eéèoòó]") then
		if mid_vowel_hint then
			syllables[syllables.stress].vowel = mid_vowel_hint
		elseif syllables[syllables.stress].vowel == "e" or syllables[syllables.stress].vowel == "o" then
			error("The stressed vowel \"" .. syllables[syllables.stress].vowel .. "\" is ambiguous. Please mark it with an acute, grave, or circumflex accent.")
		end
	end
	
	local syllables_IPA = {stress = syllables.stress}
	
	for key, val in ipairs(syllables) do
		syllables_IPA[key] = {onset = val.onset, vowel = val.vowel, coda = val.coda, stressed = val.stressed}
	end
	
	-- Replace letters with IPA equivalents
	for i, syll in ipairs(syllables_IPA) do
		-- Voicing of s
		if syll.onset == "s" and i > 1 and (syllables[i-1].coda == "" or syllables[i-1].coda == "i" or syllables[i-1].coda == "u") then
			syll.onset = "z"
		end
		
		if mw.ustring.find(syll.vowel, "^[eèéêií]$") then
			syll.onset = mw.ustring.gsub(syll.onset, "tg$", "d͡ʒ")
			syll.onset = mw.ustring.gsub(syll.onset, "[cg]$", {["c"] = "s", ["g"] = "ʒ"})
			syll.onset = mw.ustring.gsub(syll.onset, "[qg]u$", {["qu"] = "k", ["gu"] = "ɡ"})
		end
		
		syll.coda = mw.ustring.gsub(syll.coda, "igs?$", "id͡ʒ")
		
		syll.onset = replace_context_free(syll.onset)
		syll.coda = replace_context_free(syll.coda)
		
		syll.vowel = mw.ustring.gsub(syll.vowel, ".", IPA_vowels)
	end
	
	syllables_IPA = postprocess_general(syllables_IPA)
	
	return syllables_IPA
end

-- Reduction of unstressed a,e in Central and Balearic (Eastern Catalan)
local function reduction_ae(syllables)
	for i = 1, #syllables do
		local current = syllables[i]
		local previous = syllables[i - 1] or {onset = "", vowel = "", coda = ""}
		local posterior = syllables[i + 1] or {onset = "", vowel = "", coda = ""}
		
		local pre_vowel_pair = previous.vowel .. previous.coda .. current.onset .. current.vowel
		local post_vowel_pair = current.vowel .. current.coda .. posterior.onset .. posterior.vowel
		local reduction = true
		
		if current.stressed then
			reduction = false
		elseif pre_vowel_pair == "əe" then
			reduction = false
		elseif post_vowel_pair == "ea" or post_vowel_pair == "eɔ" then
			reduction = false
		elseif i < syllables.stress -1 and post_vowel_pair == "ee" then
			posterior.vowel = "ə"
		elseif i > syllables.stress and post_vowel_pair == "ee" then
			reduction = false
		elseif pre_vowel_pair == "oe" or pre_vowel_pair == "ɔe" then
			reduction = false
		end
		
		if reduction then
			current.vowel = mw.ustring.gsub(current.vowel, "[ae]", "ə")
		end
	end
	return syllables
end

local accents = {}

accents["Central Catalan"] = function(syllables)
	syllables = mw.clone(syllables)
	
	-- Reduction of unstressed vowels a,e
	syllables = reduction_ae(syllables)
	
	-- Final consonant losses
	local final = syllables[#syllables].coda
	
	final = mw.ustring.gsub(final, "^ɾ(s?)$", "%1") -- no loss with hint -rr
	final = mw.ustring.gsub(final, "m[pb]$", "m")
	final = mw.ustring.gsub(final, "([ln])[td]$", "%1")
	final = mw.ustring.gsub(final, "[nŋ][kɡ]$", "ŋ")
	
	syllables[#syllables].coda = final
	
	for i = 1, #syllables do
		local current = syllables[i]
		local previous = syllables[i-1]
		
		-- Reduction of unstressed o
		if current.vowel == "o" and not (current.stressed or current.coda == "w") then
			current.vowel = mw.ustring.gsub(current.vowel, "o", "u")
		end
		
		-- v > b
		current.onset = mw.ustring.gsub(current.onset, "v", "b")
		current.coda = mw.ustring.gsub(current.coda, "nb", "mb")
		if i > 1 and mw.ustring.find(current.onset, "^b") then
			previous.coda = mw.ustring.gsub(previous.coda, "n$", "m")
		end
		
		-- allophones of r
		current.coda = mw.ustring.gsub(current.coda, "ɾ", "r")
		
		-- Remove j before palatal obstruents
		current.coda = mw.ustring.gsub(current.coda, "j([ʃʒ])", "%1")
		current.coda = mw.ustring.gsub(current.coda, "j(t͡ʃ)", "%1")
		current.coda = mw.ustring.gsub(current.coda, "j(d͡ʒ)", "%1")
		
		if i > 1 then
			if mw.ustring.find(current.onset, "^[ʃʒ]") or mw.ustring.find(current.onset, "^t͡ʃ") or mw.ustring.find(current.onset, "^d͡ʒ") then
				previous.coda = mw.ustring.gsub(previous.coda, "j$", "")
			end
		end
	end
	
	return syllables
end

accents["Balearic"] = function(syllables, mid_vowel_hint)
	syllables = mw.clone(syllables)
	
	-- Reduction of unstressed vowels a,e
	syllables = reduction_ae(syllables)
	
	for i = 1, #syllables do
		local current = syllables[i]
		local previous = syllables[i-1]
		
		-- Reduction of unstressed o per vowel harmony
		if i > 1 and current.stressed and mw.ustring.find(current.vowel, "[iu]") then
			previous.vowel = mw.ustring.gsub(previous.vowel, "o", "u")
		end
		
		-- Stressed schwa
		if i == syllables.stress and mid_vowel_hint == "ê" then
			current.vowel = mw.ustring.gsub(current.vowel, "ɛ", "ə")
		end
		
		-- Remove j before palatal obstruents
		current.coda = mw.ustring.gsub(current.coda, "j([ʃʒ])", "%1")
		current.coda = mw.ustring.gsub(current.coda, "j(t͡ʃ)", "%1")
		current.coda = mw.ustring.gsub(current.coda, "j(d͡ʒ)", "%1")
		
		if i > 1 then
			if mw.ustring.find(current.onset, "^[ʃʒ]") or mw.ustring.find(current.onset, "^t͡ʃ") or mw.ustring.find(current.onset, "^d͡ʒ") then
				previous.coda = mw.ustring.gsub(previous.coda, "j$", "")
			end
		end
		
		-- No palatal gemination ʎʎ > ll or ʎ, in Valencian and Balearic
		if i > 1 and current.onset == "ʎ" and previous.coda == "ʎ" then
			local prev_syll = previous.onset .. previous.vowel .. previous.coda
			if mw.ustring.find(prev_syll, "[bpw]aʎ$")
				or mw.ustring.find(prev_syll, "[mv]eʎ$")
				or mw.ustring.find(prev_syll, "tiʎ$")
				or mw.ustring.find(prev_syll, "m[oɔ]ʎ$")
				or (mw.ustring.find(prev_syll, "uʎ$") and current.vowel == "a")
				then
				previous.coda = "l"
				current.onset = "l"
			else
				previous.coda = ""
			end
		end
		
		-- Final consonant losses
		if #syllables == 1 then
			current.coda = mw.ustring.gsub(current.coda, "ɾ(s?)$", "%1") -- no loss with hint -rr in monosyllables
		elseif i == #syllables then
			current.coda = mw.ustring.gsub(current.coda, "[rɾ](s?)$", "%1") -- including hint -rr
		end
	end
	
	return syllables
end

accents["Valencian"] = function(syllables, mid_vowel_hint)
	syllables = mw.clone(syllables)
	
	for i = 1, #syllables do
		local current = syllables[i]
		local previous = syllables[i-1]
		
		-- Variable mid vowel
		if i == syllables.stress and (mid_vowel_hint == "ê" or mid_vowel_hint == "ô") then
			current.vowel = mw.ustring.gsub(current.vowel, "[ɛɔ]", {["ɛ"] = "e", ["ɔ"] = "o"})
		end
		
		-- Fortition of palatal fricatives
		current.onset = mw.ustring.gsub(current.onset, "ʒ", "d͡ʒ")
		current.onset = mw.ustring.gsub(current.onset, "d͡d", "d")
		
		current.coda = mw.ustring.gsub(current.coda, "ʒ", "d͡ʒ")
		current.coda = mw.ustring.gsub(current.coda, "d͡d", "d")
		
		if i > 1 and previous.vowel == "i" and previous.coda == "" and current.onset == "d͡z" then
			current.onset = "z"
		elseif (i == 1 and current.onset == "ʃ")
			or (i > 1 and current.onset == "ʃ" and previous.coda ~= "" and previous.coda ~= "j")
			then
			current.onset = "t͡ʃ"
		end
		
		-- No palatal gemination ʎʎ > ll or ʎ, in Valencian and Balearic
		if i > 1 and current.onset == "ʎ" and previous.coda == "ʎ" then
			local prev_syll = previous.onset .. previous.vowel .. previous.coda
			if mw.ustring.find(prev_syll, "[bpw]aʎ$")
				or mw.ustring.find(prev_syll, "[mv]eʎ$")
				or mw.ustring.find(prev_syll, "tiʎ$")
				or mw.ustring.find(prev_syll, "m[oɔ]ʎ$")
				or (mw.ustring.find(prev_syll, "uʎ$") and current.vowel == "a")
				then
				previous.coda = "l"
				current.onset = "l"
			else
				previous.coda = ""
			end
		end
		
		-- Hint -rr only for Central
		if i == #syllables then
			current.coda = mw.ustring.gsub(current.coda, "r(s?)$", "ɾ%1")
		end
	end
	
	return syllables
end


local accent_order = {}

for accent, _ in pairs(accents) do
	table.insert(accent_order, accent)
end

table.sort(accent_order)


local function join_syllables(syllables)
	syllables = mw.clone(syllables)
	
	for i, syll in ipairs(syllables) do
		syll = syll.onset .. syll.vowel .. syll.coda
	
		if i == syllables.stress then -- primary stress
			syll = "ˈ" .. syll
		elseif syllables[i].stressed then -- secondary stress
			syll = "ˌ" .. syll
		end
		
		syllables[i] = syll
	end
	
	return "/" .. mw.ustring.gsub(table.concat(syllables, "."), ".([ˈˌ])", "%1") .. "/"
end

local function group_sort_and_format(syllables, mid_vowel_hint)
	local grouped = {}
	
	for _, accent in pairs(accent_order) do
		local ipa = join_syllables(accents[accent](syllables, mid_vowel_hint))
		if grouped[ipa] then
			table.insert(grouped[ipa], accent)
		else
			grouped[ipa] = {accent}
		end
	end
	
	local out = {}
	
	for ipa, accents in pairs(grouped) do
		table.insert(out, m_a.show(accents) .. " " .. m_ipa.format_IPA_full(lang, {{pron = ipa}}))
	end
	
	table.sort(out)
	return out
end

function export.show(frame)
	local params = {
		[1] = {default = mw.title.getCurrentTitle().text},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local word = mw.ustring.lower(args[1])
	local mid_vowel_hint = nil
	
	if word == "é" or word == "è" or word == "ê" or word == "ó" or word == "ò" or word == "ô" then
		mid_vowel_hint = word
		word = mw.ustring.lower(mw.title.getCurrentTitle().text)
	end
	
	word = word_fixes(word)
	
	local syllables = split_syllables(word)
	
	if mid_vowel_hint == nil then
		if mw.ustring.find(syllables[syllables.stress].vowel, "[éêòóô]") then
			mid_vowel_hint = mw.ustring.match(syllables[syllables.stress].vowel, "[éêòóô]")
		elseif mw.ustring.find(syllables[syllables.stress].vowel, "[eè]") then
			mid_vowel_hint = mid_vowel_e(syllables)
		elseif syllables[syllables.stress].vowel == "o" then
			mid_vowel_hint = mid_vowel_o(syllables)
		end
	end
	syllables = to_IPA(syllables, mid_vowel_hint)
	
	return table.concat(group_sort_and_format(syllables, mid_vowel_hint), "\n* ")
end

-- on debug console use: =p.debug("your_word", "your_hint")
function export.debug(word, mid_vowel_hint)
	word = word_fixes(mw.ustring.lower(word))
	
	local syllables = split_syllables(word)
	if mid_vowel_hint == nil then
		if mw.ustring.find(syllables[syllables.stress].vowel, "[éêòóô]") then
			mid_vowel_hint = mw.ustring.match(syllables[syllables.stress].vowel, "[éêòóô]")
		elseif mw.ustring.find(syllables[syllables.stress].vowel, "[eè]") then
			mid_vowel_hint = mid_vowel_e(syllables)
		elseif syllables[syllables.stress].vowel == "o" then
			mid_vowel_hint = mid_vowel_o(syllables)
		end
	end
	syllables = to_IPA(syllables, mid_vowel_hint)
	
	local ret = {}
	
	for _, accent in ipairs(accent_order) do
		local syllables_accented = accents[accent](syllables, mid_vowel_hint)
		table.insert(ret, accent .. " " .. join_syllables(syllables_accented))
	end
	
	return table.concat(ret, "\n")
end

return export