Module:ks-conj-pa/template


local p = {}
local frame = mw.getCurrentFrame()
local forms = require("Module:ks-conj-pa")
local  vowels = {
	"َ",
	"اَ",
	"ا",
	"آ",
	"ٔ",
	"ٲ",
	"ُ",
	"وٗ",
	"ۆ",
	"و",
	"ِ", 
	"ی", 
	"یٖ",
	"ؠ",
}

function p.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 p.tab_from_str(str)
	local out = {}
	local syl = ""
	local length = mw.ustring.len(str)
	for i = 1, length do
    	local c = mw.ustring.sub(str, i, i)
    	syl = syl .. c
    	if forms.contains_val(vowels, c) then
    		if #syl > 2 or c == "آ" or i == length then
    			local add = mw.ustring.gsub(syl, "[ییٛ]",{["ی"] = "ے" ,  ["ێ"]= "ےٚ"	})
    			table.insert(out, add)
    			syl = ""
    		end
    	elseif length == i then
    		local add = mw.ustring.gsub(syl, "[ییٛ]",{["ی"] = "ے" ,  ["ێ"]= "ےٚ"	})
    		table.insert(out, add)
    	end
	end
	return out
end

function p.getstem(params)
	local inf = frame:getParent().args["word"]
	if not inf then
		inf = params.args.title
	end
	local inf_endings = {
		["یُٛن"] = "ِ",
		["یُٛن"] = "ِ",
		["یٛۆن"] = "ؠ",
		["ُن"] = "",
	}
	for ending, repl in pairs(inf_endings) do
		if mw.ustring.match(inf, ending.."$") then
			return p.tab_from_str(mw.ustring.gsub(inf, ending.."$", repl))
		end
	end
	return inf
end

function p.show(params)
	local stem = p.getstem(params)
	local vow = p.endswith_vow(stem[#stem])
	local nom = forms.contains_val(frame:getParent().args, "nom")
	local trans = forms.contains_val(frame:getParent().args, "trans")
	local return_keys
	if nom then
		return_keys = {"impf", "cp", "cpn",
			"msgptc", "mplptc", "fsgptc", "fplptc", 
			"fut1ps", "fut2ps", "fut3ps", "fut1pp", "fut2pp", "fut3pp",
			"pstm1ps", "pstm2ps", "pstm3ps", "pstm1pp", "pstm2pp", "pstm3pp",
			"pstf1ps", "pstf2ps", "pstf3ps", "pstf1pp", "pstf2pp", "pstf3pp",
			"cond1ps", "cond2ps", "cond3ps", "cond1pp", "cond2pp", "cond3pp",
			"impsims", "impsimp",
			"imppols", "imppolp",
			"impobls", "impoblp"
		}
	else
		return_keys = {"impf", "cp", "cpn",
		"msgptc", "mplptc", "fsgptc", "fplptc", 
		"fut1ps", "fut2ps", "fut3ps", "fut1pp", "fut2pp", "fut3pp",
		"pstmsg", "pstmpl", "pstfsg", "pstfpl", 
		"pst2psmsg", "pst2psmpl", "pst2psfsg", "pst2psfpl",
		"pst2ppmsg", "pst2ppmpl", "pst2ppfsg", "pst2ppfpl",
		"cond1ps", "cond2ps", "cond3ps", "cond1pp", "cond2pp", "cond3pp",
		"impsims", "impsimp",
		"imppols", "imppolp",
		"impobls", "impoblp"
		}
	end
	local f = {["stem"] = stem, ["rounded"] = forms.rounded(stem), ["centered"] = forms.centered(stem), ["fem"] = forms.fem(stem), ["fempl"] = forms.fempl(stem)}
	local return_vals = {}
	if frame:getParent().args["word"] then
		return_vals["inf"] = frame:getParent().args["word"]
	else
		return_vals["inf"] = params.args.title
	end
	for _, key in ipairs(return_keys) do
		if frame:getParent().args[key] then
			return_vals[key] = frame:getParent().args[key]
		else
			local infl = forms.inflection(key, stem, vow, f, nom, trans)
			if infl then return_vals[key] = infl end
		end
	end
	if nom then
		return frame:expandTemplate({title = 'ks-conj-pa/nom', args = return_vals})
	else
		if trans then
			return frame:expandTemplate({title = 'ks-conj-pa/erg', args = return_vals})
		else
			return frame:expandTemplate({title = 'ks-conj-pa/erg-intrans', args = return_vals})
		end
	end
end
return p