local export = {}


function export.checked(stem)
	local rest, vowel, cons = mw.ustring.match(stem, "^(.-)([aeiouâêîôûāēōë]+)([^aeiouâêîôûāēōë]+)$")
	
	if not rest then
		return stem
	end
	
	if mw.ustring.len(cons) == 1 and mw.ustring.len(vowel) == 1 then
		if mw.ustring.find(vowel, "[âêāē]") then
			vowel = vowel .. "e"
		elseif mw.ustring.find(vowel, "[iî]") then
			vowel = vowel .. "j"
		elseif mw.ustring.find(vowel, "[ôō]") then
			vowel = vowel .. "o"
		elseif mw.ustring.find(vowel, "[uû]") then
			vowel = vowel .. "u"
		end
	end
	
	cons = mw.ustring.gsub(cons, "(.)%1", "%1")
	cons = mw.ustring.gsub(cons, "c?k$", "c")
	
	return rest .. vowel .. cons
end


function export.devoiced(stem)
	local rest, cons = mw.ustring.match(stem, "^(.-)([^aeiouâêîôûāēōë]+)$")
	
	if not rest then
		return stem
	end
	
	cons = mw.ustring.gsub(cons, "(.)%1", "%1")
	cons = mw.ustring.gsub(cons, "ng$", "nc")
	cons = mw.ustring.gsub(cons, "[bdgv]", {["b"] = "p", ["d"] = "t", ["g"] = "ch", ["v"] = "f"})
	cons = mw.ustring.gsub(cons, "c?k$", "c")
	
	return rest .. cons
end


function export.final(stem)
	return export.devoiced(export.checked(stem))
end


return export