"یَمہٕ ماڈیوٗلُک دَستاویز ییٚہِ ماڈیوٗل:head/دَستاویز جاے بَناونہٕ"

local p = {}
local lang_codes = require("Module:lang name")
local pos_sg = {
	["noun"] = "عام ناوُت", ["adjective"] = "باوُت", ["adverb"] = "ہُر کرٛاوُت", ["postposition"] = "مِلوُت",  ["numeral"] = "آنکونہٕ گرَٛنٛد", ["verb"] = "کرٛاوُت", ["verb form"] = "کرٛاوتُک رٟپ",
	["noun form"] = "ناوتُک رٟپ", ["prefix"] = "برٛۆنٛہہ لۆگ", ["suffix"] = "پۆت لۆگ",  ["interjection"] = "ژھَٹُت",  ["letter"] = "اَچھُر",  ["particle"] = "ٹوٹہٕ وَنُت", 
	["proper noun"] = "خاص ناوُت",["ordinal"] = "ؤنؠ گرَٛنٛد", ["root"] = "موٗل",
	["pronoun"] = "اِشارٕ ناوُت", ["pronoun form"] = "اِشارٕ ناوتُک رٟپ",
	["proverb"] = "دٔپِتھ", ["idiom"] = "مَحاورٕ", ["conjunction "] = "بَنٛدُت",
}
function p.trim(s)
   return (s:gsub("^%s*(.-)%s*$", "%1"))
end
local pos_f = { --add feminine parts of speech i.e. those whose names are feminine in kashmiri
	["numeral"]="true",
} 

function p.pos_name(pos)
	for c, v in pairs(pos_sg) do
		if c == pos then
			return v
		end
	end
	error("Invalid or unsupported part of speech : '" .. pos .. "'")
end

local pos_pl = {
	["noun"] = "عام ناوٕتؠ", ["adjective"] = "باوٕتؠ", ["adverb"] = "ہُر کرٛاوٕتؠ", ["postposition"] = "مِلوٕتؠ", ["numeral"] = "آنکونہٕ گرَٛنٛد", ["verb"] = "کرٛاوٕتؠ", ["verb form"] = "کرٛاوتٕکؠ رٟپؠ",
	["noun form"] = "ناوتٕکؠ رٟپؠ", ["prefix"] = "برٛۆنٛہہ لٔگؠ", ["suffix"] = "پۆت لٔگؠ", ["ordinal"] = "ؤنؠ گرَٛنٛد", ["proper noun"] = "خاص ناوٕتؠ", ["interjection"] = "ژھَٹٕتؠ",  ["letter"] = "اَچھَر",  ["particle"] = "ٹوٹہٕ وَنٕتؠ", 
	["root"] = "موٗلؠ", ["pronoun"] = "اِشارٕ ناوٕتؠ", ["pronoun form"] = "اِشارٕ ناوتٕکؠ رٟپؠ", ["proverb"] = "دٔپِتھؠ", ["idiom"] = "مَحاورٕ", ["conjunction"] = "بَنٛدٕتؠ",
}

function p.add_g(g, extra)
	frame = mw.getCurrentFrame() 
	return frame:expandTemplate{ title = "g", args = { g, extra = extra } }
end

function p.plural_pos(pos)
	for c, v in pairs(pos_pl) do
		if c == pos then
			return v
		end
	end
	error("Invalid or unsupported part of speech : '" .. pos .. "'")
end
function p.table_len(tab)
	length = 0
	for _ in pairs(tab) do
		length = length + 1
	end
	return length
end

function p.split(s, delimiter)
    result = {}
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match)
    end
    return result
end
function p.gender(gs)
	out = {}
	for _, g in ipairs(p.split(gs, "-")) do 
		if g == "m" then out[#out +1] = "نَر" end
		if g == "f" then out[#out +1] = "مادٕ" end
        if g == "c" then out[#out +1] = "عام جِنٕس" end
		if g == "n" then out[#out +1] = "جِنسہٕ چھٔنؠ" end
	end
	return out
end

local lang_g = {
	["none"] = {"en", "ta", "bn", "ja", "ojp", "enm", "tr", "ota", "fa", "ml", "pal", },
	["mfn"] = {"de", "sa", "ru", "inc-pra", "ang", "goh", "odt", "grc", "peo", "mr", "nl", "kn", "gu", "la", "gmh", "dum", "el", },
	["mf"] = {"ks", "hi", "ur", "es", "fr", "it", "sd", "roa-opt", "pt", "fro", "osp", "ps", "pa", "ar", "frm", },
}

function p.add(params)
	local l_code = params.args.l_code
	local g = params.args.g
	local pos = params.args.pos
	local title =tostring(mw.title.getCurrentTitle())
	local ipa = params.args.ipa
	local a1 = params.args.a1
	local t1 = params.args.t1
	local a2 = params.args.a2
	local t2 = params.args.t2
	local a3 = params.args.a3
	local t3 = params.args.t3
	local a4 = params.args.a4
	local t4 = params.args.t4
	local a5 = params.args.a5
	local t5 = params.args.t5
	local a6 = params.args.a6
	local t6 = params.args.t6
	local a7 = params.args.a7
	local t7 = params.args.t7
	local a8 = params.args.a8
	local t8 = params.args.t8
	local out = ""
	if string.match(tostring(title), "فرما") then
		return ""
	else
		if l_code ~= "" and pos ~= "" then
			if pos_f[pos] == "true" then
				plural_lang_name = lang_codes.plural_lang(l_code , "f")
			else
				plural_lang_name = lang_codes.plural_lang(l_code, "m")
			end
			if l_code == "ks" then
				local det = require("Module:ks-script-detect")
				local class = l_code .. "-" .. det.detect(title)
				out = out .. "<strong lang = '" .. class .. "' class = '" .. class .. "-headword'>" .. title .. "</strong>"
			else
				out = out .. "<strong lang = '" .. l_code .. "' class = '" .. l_code .. "-headword'>" .. title .. "</strong>"
			end
			
			if g ~= "" then
				out = out .. " " .. p.add_g(g, p.pos_name(pos)) .. " "
			end
			--'a' is argument and 't' is term. head|..|Avada|Kedavra - avada is 'a' and Kedavra is 't'. renders as word (Avada: Kedavra) [ipa]
 			local as_passed = {a1, a2, a3, a4, a5, a6, a7, a8,}
			local ts_passed = {t1, t2, t3, t4, t5, t6, t7, t8,}
			local as = {}
			local ts = {}
			local td = false
			for n, a in ipairs(as_passed) do
				local t = ts_passed[n]
				if a ~= nil and p.trim(a) ~= '' and t ~= "no_include" then
					table.insert(as,a)
					table.insert(ts,t)
				end
			end
			if p.table_len(as) ~= 0 then
				for n, a in ipairs(as) do
					t = ts[n]
					if n == 1 then
						out = out .. " (<span class = 'bracket-text'>'''" .. a .. "'''"
					else
						out = out .. "،" .. " " .. "'''" .. a .. "'''"
					end
					if t ~= "" then
						local t_split = p.split(t, "//")
						for n, v in ipairs(t_split) do
							if string.match(v, "__nolink") then
								if n == 1 then
									out = out .. " : " .. "'''" .. string.gsub(v,"__nolink","") .. "'''"
								else
									out = out .. " یا " .. "'''" .. string.gsub(v,"__nolink","") .. "'''"
								end
							else
								local lang_name = lang_codes.get_lang_name(l_code)
								if n == 1 then
									if v ~= title then
										out = out .. " : " .. "'''[[" .. v .. "#" .. lang_name .. "|" .. v .. "]]'''"
									else
										out = out .. " : " .. "'''[[" .. v .. "]]'''"
									end
								else
									if v ~= title then
										out = out .. " یا " .. "'''[[" .. v .. "#" .. lang_name .. "|" .. v .. "]]'''"
									else
										out = out .. " یا " .. "'''[[" .. v .. "]]'''"
									end
								end
							end
						end
					end
				end
				out = out .. ")</span>"
			end
			if ipa ~= "" and ipa ~= nil then
				if string.match(ipa, "یا") then
					out = out .. " <span class = '" .. l_code .. "-ipa'>"
					local length = p.table_len(p.split(ipa, "یا"))
					for n, alt in ipairs(p.split(ipa, "یا")) do
						local to_a = p.trim(alt)
						if n == length and to_a ~= '' then
							out = out .. " <span dir = 'ltr' class = '" .. l_code .. "-alt-ipa-" .. tostring(n) .. "'>" .. "[" .. to_a .. "]</span>"
						elseif to_a ~= '' then
							out = out .. " <span dir = 'ltr' class = '" .. l_code .. "-alt-ipa-" .. tostring(n) .. "'>" .. "[" .. to_a .. "]</span> یا"
						end
					end
				else
					out = out .. " <span class = '" .. l_code .. "-ipa'>" .. "[" .. ipa .. "]</span>"
				end
			end
			out = out .. "\n[[Category:" .. plural_lang_name .. " " .. p.plural_pos(pos) .. "]]"
			if g ~= "" then
				local genders = p.gender(g)
				for _, gen in ipairs(genders) do
					out = out .. "\n[[Category:" .. plural_lang_name .. " " .. gen .. " " .. p.plural_pos(pos) .. "]]"
				end
			end
			if pos == "ordinal" then
				out = out .. "\n[[Category:" .. plural_lang_name ..  " باوٕتؠ]]"
			end
		else
			error("Please specify language code and part of speech")
		end
		return out
	end
end
return p