local export = {}
local find = mw.ustring.find
local sub = mw.ustring.sub
local gsub = mw.ustring.gsub
local match = mw.ustring.match
local length = mw.ustring.len
local vowels = "اەېىوۇۆۈ"
local vowelsBack = "اوۇ"
local vowelsFront = "ەۆۈ"
-- local vowelsCentral = "ېى"
local consonBack = "خغق"
local consonFront = "كگ"
local consonJsiz = "پتچخسشفقكھغگبد"

function export.getLast(str)
	local base, v, c = match(str, "(.*[^" .. vowels .. "]+)([" .. vowels .. "]+)([^" .. vowels .. "]*)$")
	return base, v, c
end
function export.checkTongue(str)
  local isChe = find(str, "چە$")
  if isChe then str = gsub(str, "چە$", "") end 
  local tongue = export.checkFrontBack(str)
  if tongue then return tongue end
  if isChe then return checkChe(str) end
  return export.checkCentral(str)
end
function export.checkFrontBack(str)
	local base, v = match(str, "(.+)([" .. vowels .. "]+)[^" .. vowels .. "]*$")
	if v then
		if find(v, "[" .. vowelsBack .. "]$") then return "back" end
		if find(v, "[" .. vowelsFront .. "]$") then return "front" end
		return export.checkFrontBack(base)
	else
		return nil
	end
end
function export.checkCentral(str)
	--exceptions
	for k, data in pairs(require('Module:ug-common/data').tongue) do
		if match(str, k) then return data end
	end
	
	if match(str,"[" .. consonBack .. "]") then return "back" end
	if match(str,"[" .. consonFront .. "]") then return "front" end
	return nil
end
function checkChe(str)
  local ff =  match(str,"(.-)[" .. consonBack .. "]")
  if ff then return "back" else return "front" end
end

return export