local export = {}
local pos_functions = {}
local match = mw.ustring.match
local gsub = mw.ustring.gsub

local lang = require("Module:languages").getByCode("te")
local PAGENAME = mw.title.getCurrentTitle().text
local script = require("Module:scripts").getByCode("Telu")

function export.show(frame)

	local args = frame:getParent().args
	local poscat = frame.args[1] or error("Golongan kata belum ditentukan. Tolong lepasi parameter 1 ke penyeruan modul.")
 
	local data = {lang = lang, sc = script, pos_category = poscat, categories = {}, heads = {args["head"] or PAGENAME}, genders = {}, inflections = {}}

	if pos_functions[poscat] then
		pos_functions[poscat](args, data)
	end
 
	return require("Module:headword").full_headword(data)

end

-- TODO
local function pluralize(sn)

	local pn

	-- do not swap
	if match(sn, "ుడు$") then -- -uDu > -ulu
		pn = gsub(sn, "ుడు$", "ులు")
	elseif match(sn, "డ్డ[ిు]$") then -- -DDi,-DDu > -Dlu
		pn = gsub(sn, "డ్డ[ిు]$", "డ్లు")
	elseif match(sn, "డి$") then -- -Di > -LLu
		pn = gsub(sn, "డి$", "ళ్ళు")
	elseif match(sn, "[డరల]ు$") then -- -Du,-lu,-ru > -LLu
		pn = gsub(sn, "[డరల]ు$", "ళ్ళు")
	elseif match(sn, "ట్ట[ిు]$") then -- -TTi,-TTu > -Tlu
		pn = gsub(sn, "ట్ట[ిు]$", "ట్లు")
	elseif match(sn, "టు$") then -- -Tu > -Tlu
		pn = gsub(sn, "టు$", "ట్లు")
	elseif match(sn, "([అక-హౘ-ౚ])ం$") then -- -aM > -Alu
		pn = gsub(sn, "([అక-హౘ-ౚ])ం$", "%1ాలు")
	elseif match(sn, "ెం$") then -- -eM > -Elu
		pn = gsub(sn, "ెం$", "ేలు")
	elseif match(sn, "ి$") then -- -i > -ulu
		pn = gsub(sn, "ి$", "ులు")
	else
		pn = sn .. "లు"
	end

	return pn

end

pos_functions["kata nama"] = function(args, data)

	local plurals = {label = "jamak"}

	if not args[1] then
		table.insert(plurals, pluralize(PAGENAME))
		table.insert(data.inflections, plurals)
	elseif args[1] == "-" then
		table.insert(data.inflections, {label = "mufrad sahaja"})
		table.insert(data.categories, "Singularia tantum bahasa Telugu")
	elseif args[1] == "+" then
		table.insert(data.inflections, {label = "jamak sahaja"})
		table.insert(data.categories, "Pluralia tantum bahasa Telugu")
	else
		for _,par in ipairs(args) do
			table.insert(plurals, par)
		end
		table.insert(data.inflections, plurals)
	end

	table.insert(data.genders, args["g"] or "?")

end

pos_functions["kata nama khas"] = function(args, data)

	local plurals = {label = "jamak"}

	if not args[1] then
		-- do nothing
	elseif args[1] == "+" then
		table.insert(data.inflections, {label = "jamak sahaja"})
		table.insert(data.categories, "Pluralia tantum bahasa Telugu")
	else
		for _,par in ipairs(args) do
			table.insert(plurals, par)
		end
		table.insert(data.inflections, plurals)
	end

	table.insert(data.genders, args["g"] or "?")

end

return export