local export = {}
local pos_functions = {}

local lang = require("Module:languages").getByCode("ceb")
local PAGENAME = mw.title.getCurrentTitle().text
local script = lang:findBestScript(PAGENAME) -- Latn

function export.show(frame)
	local tracking_categories = {}

	local args = frame:getParent().args
	local poscat = frame.args[1] or require("Module:string utilities").pluralize(args["pos"]) or error("Part of speech has not been specified. Please pass parameter to the module invocation.")

	local head = {} -- supports multiple headword
	local function insert_head(arg)
		if arg == "" then
			-- track("blank-head")
			arg = PAGENAME
		end
		if arg then
			table.insert(head, arg)
		end
	end
	insert_head(args["head"] or args[1])
	insert_head(args["head2"] or args[2])
	insert_head(args["head3"] or args[3])

	local data = {lang = lang, sc = script, pos_category = poscat, categories = {}, heads = head, tr = tr, inflections = {}}
	
	local baybayin = {label = "Badlit spelling"}
	local sc_Tglg = require("Module:scripts").getByCode("Tglg")
	if args["b"] then table.insert(baybayin, { term = args["b"], sc = sc_Tglg }) end
	if args["b2"] then table.insert(baybayin, { term = args["b2"], sc = sc_Tglg }) end
	if args["b3"] then table.insert(baybayin, { term = args["b3"], sc = sc_Tglg }) end
	if script:getCode() == "Latn" then
		if #baybayin > 0 then 
			table.insert(data.inflections, baybayin) 
			table.insert(data.categories, "Cebuano terms with Baybayin script")
		else
			table.insert(data.categories, "Cebuano terms without Baybayin script")
		end
	elseif script:getCode() == "Tglg" then
		--Categorize words with Badlit
        table.insert(data.categories, "Cebuano terms in Baybayin script")
    end

	-- Badlit to Latin
	local tr = args["tr"]
	if not tr then
		tr = require("Module:ceb-translit")
	end
	
	-- feminines and masculines
	local fem = {label = "feminine"}
	if args["f"] then table.insert(fem, {term = args["f"]}) end
	if #fem > 0 then table.insert(data.inflections, fem) end
	
	local masc = {label = "masculine"}	
	if args["m"] then table.insert(masc, {term = args["m"]}) end
	if #masc > 0 then table.insert(data.inflections, masc) end
	
	local collective = {label = "collective" }	
	if args["collective"] then table.insert(collective, {term = args["collective"]}) end
	if #collective > 0 then table.insert(data.inflections, collective) end

	if pos_functions[poscat] then
		pos_functions[poscat](args, data)
	end

	local content = mw.title.new(PAGENAME):getContent()
	local code = content and mw.ustring.match(content, "{{ceb%-IPA[^}]*}}")

    --Categorize words without [[Template:tl-IPA]]
	if script:getCode() == "Latn" and not code then
		table.insert(tracking_categories, "Cebuano terms without ceb-IPA template")
	end

	return require("Module:headword").full_headword(data) .. require("Module:utilities").format_categories(tracking_categories, lang)

end

pos_functions["verbs"] = function(args, data)

    params = {
    	[1] = {alias_of = 'head'},
		[2] = {alias_of = 'inch'}, --Nasugdan. Use with affixed verbs only
		[3] = {alias_of = 'imp'},
		head = {list = true},
		head2= {},
		head3= {},
		inch = {list = true},
		imp = {list = true},
		b = {},
		b2 = {},
		b3 = {}
	}

	local args = require("Module:parameters").process(args,params)
	data.heads = args.head
	data.id = args.id
	local pattern = args.pattern
	
	args.inch.label = "inchoative"
	args.imp.label = "imperative"

	args.inch.accel = {form = "realis"}
	args.imp.accel = {form = "imperative"}

	if #args.inch > 0 then table.insert(data.inflections, args.inch) end
	if #args.imp > 0 then table.insert(data.inflections, args.imp) end

end

pos_functions["adjectives"] = function(args, data)

	    params = {
		[1] = {alias_of = 'head'},
		[2] = {alias_of = 'comp_sup'},
		[3] = {alias_of = 'abs_sup'},
		[4] = {alias_of = 'plural'}, -- See notes.
        f= {list = true},
        m= {list = true},
		head = {list = true},
		head2= {},
		head3= {},
		plural = {list = true}, -- Some adjectives only
	    comp_sup = {list = true},
		abs_sup = {list = true},
		b = {},
		b2 = {},
		b3 = {}
	}
	
	local args = require("Module:parameters").process(args,params)
	data.heads = args.head
	data.id = args.id
	local pattern = args.pattern
	
	args.comp_sup.label = "comp_sup"
	args.abs_sup.label = "abs_sup"	
	args.plural.label = "plural"
	args.f.label = "feminine"
	args.m.label = "masculine"

	args.comp_sup.accel = {form = "comparative|superlative"}
	args.abs_sup.accel = {form = "absolutive|superlative"}
	args.plural.accel = {form = "plural"}
	args.f.accel = {form = "feminine"}
	args.m.accel = {form = "masculine"}

	if #args.comp_sup > 0 then table.insert(data.inflections, args.comp_sup) end
	if #args.abs_sup > 0 then table.insert(data.inflections, args.abs_sup) end
	if #args.plural > 0 then table.insert(data.inflections, args.plural) end

end

pos_functions["Danh từ"] = function(args, data)

	--Tagging root forms of nouns
	local rootword = args["rootword"] or nil 
	if require("Module:yesno")(rootword) then 
		table.insert(data.inflections, {label = "root word"}) 
		table.insert(data.categories, "Cebuano roots")
	end

end

return export