ထာမ်ပလိက်တဏအ်ဒးနွံကဵုလိက်စၟတ်သမ္တီမချူလဝ်သာဓက။
သ္ပဂုန်တုဲထာမ်ပလိက်တဏအ်ဒးစၟတ်သမ္တီကဵုလိက်မချူလဝ်နူကဵုဓမံက်ထ္ၜးကဵုဗီုပြၚ်ကိစ္စမရန်တၟအ်လဝ် ကဵု ပရေၚ်မရပ်စပ်လ္တူမုက်လိက်နကဵုလိက်စၟတ်သမ္တီမချူလဝ်သာဓက

local export = {}

export.vowels_back_unrounded = "aâı"
export.vowels_back_rounded = "ouû"
export.vowels_front_unrounded = "eiî"
export.vowels_front_rounded = "öü"

export.vowels_back = export.vowels_back_unrounded .. export.vowels_back_rounded
export.vowels_front = export.vowels_front_unrounded .. export.vowels_front_rounded

export.vowels = export.vowels_back .. export.vowels_front

export.voiceless_consonants = "çfhkptsş"
export.voiced_consonants = "bcdgğjlmnrvyz"

export.consonants = export.voiceless_consonants .. export.voiced_consonants

function is_in(c, str)
	if mw.ustring.len(c) ~= 1 then
		error("argument with bad length: " .. tostring(mw.ustring.len(c)) .. " ~= 1")
	end
	return mw.ustring.find(str, c) ~= nil
end

function export.is_vowel(c)
	return is_in(c, export.vowels)
end

function export.is_voiceless_consonant(c)
	return is_in(c, export.voiceless_consonants)
end

function export.is_voiced_consonant(c)
	return is_in(c, export.voiced_consonants)
end

function export.get_last_vowel(str)
	return mw.ustring.match(str, "([" .. export.vowels .. "])[^" .. export.vowels .. "]*$")
end

function export.starts_with_vowel(str)
	if mw.ustring.match(str, "^[" .. export.vowels .. "]") then
		return true
	else
		return false
	end
end

function export.starts_with_consonant(str)
	return not starts_with_vowel(str)
end
	
function export.ends_with_vowel(str)
	if mw.ustring.match(str, "[" .. export.vowels .. "]$") then
		return true
	else
		return false
	end
end

function export.ends_with_consonant(str)
	return not ends_with_vowel(str)
end
	
function export.ends_with_voiceless_consonant(str)
	if mw.ustring.match(str, "[" .. export.voiceless_consonants .. "]$") then
		return true
	else
		return false
	end
end

function export.soften(str)
	str = mw.ustring.gsub(str, "ç$", "c")
	str = mw.ustring.gsub(str, "p$", "b")
	str = mw.ustring.gsub(str, "t$", "d")
	str = mw.ustring.gsub(str, "k$", "ğ")
	return str
end

function export.get_2_way_harmony(vowel)
	if mw.ustring.match(vowel, "^[" .. export.vowels_back .. "]$") then
		return "a"
	elseif mw.ustring.match(vowel, "^[" .. export.vowels_front .. "]$") then
		return "e"
	else
		error("'" .. export.vowel .. "' is not a vowel")
	end
end

function export.get_4_way_harmony(vowel)
	if mw.ustring.match(vowel, "^[" .. export.vowels_back_unrounded .. "]$") then
		return "ı"
	elseif mw.ustring.match(vowel, "^[" .. export.vowels_back_rounded .. "]$") then
		return "u"
	elseif mw.ustring.match(vowel, "^[" .. export.vowels_front_unrounded .. "]$") then
		return "i"
	elseif mw.ustring.match(vowel, "^[" .. export.vowels_front_rounded .. "]$") then
		return "ü"
	else
		error("'" .. export.vowel .. "' is not a vowel")
	end
end

function export.get_ki_harmony(vowel)
	if mw.ustring.match(str, "^[" .. export.vowels_back_unrounded .. export.vowels_back_rounded .. export.vowels_front_unrounded .. "]$") then
		return "i"
	elseif mw.ustring.match(str, "^[" .. export.vowels_front_rounded .. "]$") then
		return "ü"
	else
		error("'" .. export.vowel .. "' is not a vowel")
	end
end

function export.attach_suffixes(base, suffixes, base_vowel, generate_2_way_aorist, generate_4_way_aorist)
	local result = base
	local previous_char = mw.ustring.char(mw.ustring.codepoint(base, -1))
	local previous_vowel = base_vowel or export.get_last_vowel(base) or "i" -- yemek -> yiyor
	
	local remaining_suffixes = suffixes
	for c in mw.ustring.gcodepoint(suffixes) do
		c = mw.ustring.char(c)
		remaining_suffixes = mw.ustring.sub(remaining_suffixes, 2)
		
		if c == "R" then -- aorist R
			if export.is_vowel(previous_char) then
				c = "r"
			else
				if generate_2_way_aorist and generate_4_way_aorist then
					ar = export.attach_suffixes(result, "Ar" .. remaining_suffixes, nil, generate_2_way_aorist, generate_4_way_aorist)
					ir = export.attach_suffixes(result, "Ir" .. remaining_suffixes, nil, generate_2_way_aorist, generate_4_way_aorist)
					for _, v in ipairs(ir) do
						ar[#ar + 1] = v
					end
					return ar
				elseif generate_2_way_aorist or generate_4_way_aorist then
					previous_char = (generate_2_way_aorist and export.get_2_way_harmony or export.get_4_way_harmony)(previous_vowel)
					previous_vowel = previous_char
					result = result .. previous_char
					c = "r"
				else
					error("suffixes contain an aorist placeholder after a consonant but no harmony is specified")
				end
			end
		elseif c == "Y" then -- intervocalic Y
			c = export.is_vowel(previous_char) and "y" or nil
		elseif c == "N" then -- intervocalic N
			c = export.is_vowel(previous_char) and "n" or nil
		elseif c == "S" then -- intervocalic S
			c = export.is_vowel(previous_char) and "s" or nil
		elseif c == "J" then -- interconsonantal I
			c = export.is_vowel(previous_char) and nil or export.get_4_way_harmony(previous_vowel)
		elseif c == "D" then -- d/t
			c = export.is_voiceless_consonant(previous_char) and "t" or "d"
		elseif c == "A" then -- 2-way
			c = export.get_2_way_harmony(previous_vowel)
		elseif c == "I" then -- 4-way
			c = export.get_4_way_harmony(previous_vowel)
		end
		
		if c ~= nil then
			if c == mw.ustring.upper(c) then
				error("unrecognized placeholder '" .. c .. "'")
			end
			
			result = result .. c
			previous_char = c
			if export.is_vowel(c) then
				previous_vowel = c
			end
		end
	end
	
	return { result }
end
	
return export