Module:ks-conj-pa


local export = {}
--arrays
local rounded_vow = {
	["َ"] = "ۆ",
	["ا"] = "و", 
	["آ"] = "او", --for word beginnings
	["ِ"] = "یُٛ",
	["ےٚ"] = "یۆ",
	["ے"] = "یٛوٗ",
	["و"] = "وٗ",
	["ۄ"] = "ۆ",
	["ؠ"] = "یٛو" --not really a vowel, but for all practical purposes
}
local centered_vow = {
	["َ"] = "ٔ", 
	["ا"] = "ٲ",
	["آ"] = "ٲ",
	["ے"] = "یٖ",
	["ۄ"] = "ۆ",
}
local fem_cons = {
	["ت"] = "ژ",
	["تھ"] = "ژھ",
	["ٹ"] = "چ",
	["ٹھ"] = "چھ",
	["ڈ"] = "ج",
	["ک"] = "چ",
	["کھ"]  = "چھ",
	["گ"] = "ج",
	["ل"] = "ج",
}
local  vowels = {
	"َ",
	"اَ",
	"ا",
	"آ",
	"ٔ",
	"ٲ",
	"ُ",
	"وٗ",
	"ۆ",
	"و",
	"ِ", 
	"ی", 
	"یٖ",
	"ؠ",
	"ُٛ", --???
}
local non_he_ending = {"د", "ڈ", "ذ", "ر", "ڑ", "ز", "ژ", "ھ", "و"}
function export.endswith(main, subs)
	return string.sub(main, #main-#subs+1, #main) == subs
end
function export.startswith(main, subs)
	return string.sub(main, 1, #subs) == subs
end
--morphological changes
function replace(stem, lst, vow, forms)
	local out = {}
	local length = #stem
	local pat
	for i=1, length do
		local syl = stem[length-i+1]
		for chr, repl in pairs(lst) do
			local cond = true
			if vow then cond = not export.startswith(syl, "و") or #syl > 2 end
			if export.endswith(syl, chr) and cond then
				if vow then pat = chr.."$" else pat = "^" .. chr end
				out[length-i+1] = mw.ustring.gsub(syl, pat, repl)
				break
			else
				out[length-i+1] = syl
			end
		end
	end
	return out
end

local function endswith_vow(str)
	if #str > 2 then
		for _, v in ipairs(vowels) do
			if mw.ustring.match(str, v .. "$") then
				return true
			end
		end
	end
	return false
end

function repl_last_vow(stem, vow_lst)
	return replace(stem, vow_lst, 1)
end
function repl_last_cons(stem, cons_lst)
	return replace(stem, cons_lst, 0)
end

function export.rounded(stem)
	return repl_last_vow(stem, rounded_vow)
end

function export.centered(stem, vow, forms)
	if vow then
		return stem
	end
	return repl_last_vow(stem, centered_vow)
end

function export.fem(stem)
	return repl_last_cons(export.centered(stem), fem_cons)
end

function export.fempl(stem)
	return repl_last_cons(stem, fem_cons)
end

function export.to_text(tab)
	local outstr = ""
	for _, v in ipairs(tab) do
		outstr = outstr .. mw.ustring.gsub(v,
			"[ےےٚ]",{["ے"] = "ی" ,  ["ےٚ"]= "ێ"	})
	end
	return outstr
end

function export.contains_val(lst, w)
	for _, t in pairs(lst) do
		if mw.ustring.match(w, t) then
			return true
		end
	end
	return false
end

local function add(main, s)
	return export.to_text(main) .. s
end

local function add_zer(tab, vow, forms)
	local word = export.to_text(tab)
	local last = mw.ustring.sub(word, mw.ustring.len(word))
	local out = word
	if not export.contains_val(non_he_ending, last) then
		return word .. "ہِ "
	end
	return out .."ِ"
end

local function add_sayi(tab, vow, forms)
	local word = export.to_text(tab)
	local last = mw.ustring.sub(word, mw.ustring.len(word))
	local out = word
	if not export.contains_val(non_he_ending, last) then
		return word .. "ہٕ "
	end
	return out .."ٕ"
end
--inflections
-- ["<form>"] = {"<verb morph>", "<vowel ending>", "<consonant ending>"},
local erg_endings = {
	["impf"] = {"stem", "وان", "ان"},
	["cp"] = {"centered", "تھ", "ِتھ"},
	["cpn"] = {"stem", "نَے", "ٕنَے"},
	["msgptc"] = {"rounded", "مُت",  "مُت"},
	["mplptc"] = {"centered", "مٕتؠ", "ؠ مٕتؠ"},
	["fsgptc"] = {"fem", "مٕژ", "مٕژ"},
	["fplptc"] = {"fempl", "مَژٕ", "zer_مَژٕ"},
	
	["fut1ps"] = {"stem", "مہٕ", "sayi_"},
	["fut2ps"] = {"stem", "کھ", "َکھ"},
	["fut3ps"] = {"stem", "یہِ", "zer_"},
	["fut1pp"] = {"stem", "مَو", "َو"},
	["fut2pp"] = {"centered", "یِو", "ِو"},
	["fut3pp"] = {"stem", "ن", "َن"},
	
	["pstmsg"] = {"rounded", "و", ""},
	["pstmpl"] = {"centered", "یہِ", "ؠ"},
	["pstfsg"] = {"fem", "یہِ", ""},
	["pstfpl"] = {"fempl", "یہِ", "zer_"},
	["pst2psmsg"] = {"rounded", "تھ", "ُتھ"},
	["pst2psmpl"] = {"centered", "یَتھ", "ِتھ"},
	["pst2psfsg"] = {"fem", "یَتھ", "ِتھ"},
	["pst2psfpl"] = {"fempl", "یَتھ", "ؠتھ"},
	["pst2ppmsg"] = {"rounded", "وٕ", "وٕ"},
	["pst2ppmpl"] = {"centered", "یِوٕ",  "ِوٕ"},
	["pst2ppfsg"] = {"fem", "یِوٕ",  "ِوٕ"},
	["pst2ppfpl"] = {"fempl",  "یِوٕ",  "ِوٕ"},
	
	["impsims"] = {}, --don't add
	["impsimp"] = {"centered", "یِو", "ِو"},
	["imppols"] = {"stem", "تہٕ", "تہٕ"},
	["imppolp"] = {"stem", "تَو", "تَو"},
	["impobls"] = {"centered", "ؠ زِ", "ؠ زِ"},
	["impoblp"] = {"centered", "ؠ زؠو", "ؠ زؠو"},
	
	["cond1ps"] = {"stem", "ہٲ", "sayi_ہٲ"},
	["cond2ps"] = {"stem", "ہٲکھ", "sayi_ہٲکھ"},
	["cond3ps"] = {"stem", "ہے", "zer_ہے"},
	["cond1pp"] = {"stem", "ہٲو", "sayi_ہٲو"},
	["cond2pp"] = {"stem", "ہیٖو", "zer_ہیٖو"},
	["cond3pp"] = {"stem", "ہٲن", "sayi_ہٲن"},
}
local nom_endings = {
	["pstm1ps"] = {"rounded", "س", "ُس"},
	["pstm2ps"] = {"rounded", "کھ", "ُکھ"},
	["pstm3ps"] = {"rounded", "و", ""},
	["pstm1pp"] = {"centered", "یہِ", "ؠ"},
	["pstm2pp"] = {"centered", "یِوٕ", "ِوٕ"},
	["pstm3pp"] = {"centered", "یہِ", "ؠ"},
	
	["pstf1ps"] = {"fem", "یَس", "ِس"},
	["pstf2ps"] = {"fem", "یَکھ", "ٕکھ"},
	["pstf3ps"] = {"fem", "یہِ", ""},
	["pstf1pp"] = {"fempl", "یہِ", "zer_"},
	["pstf2pp"] = {"fempl", "یِوٕ", "ِوٕ"},
	["pstf3pp"] = {"fempl", "یہِ", "zer_"},
}
function export.inflection(infl, stem, vow, forms, nom, trans)
	if nom_endings[infl] or erg_endings[infl] then
		local inflection
		if nom and nom_endings[infl] then inflection = nom_endings[infl] else inflection = erg_endings[infl] end
		local form = forms[inflection[1]]
		mw.log(infl)
		if infl == "impsims" then
			return mw.ustring.gsub(export.to_text(stem), "ِ$", "ہِ")
		elseif endswith_vow(form[#form]) then
			return add(form, inflection[2])
		else
			local suff = inflection[3]
			local out = form
			if mw.ustring.match(suff, "zer_") then
				suff = mw.ustring.gsub(suff, "zer_", "")
				out = add_zer(out)
			elseif mw.ustring.match(suff, "sayi_") then
				suff = mw.ustring.gsub(suff, "sayi_", "")
				out = add_sayi(out)
			else
				out = export.to_text(out)
			end
			return out .. suff
		end
	else
		return false
	end
end

return export