local export = {}
local pos_functions = {}

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

function export.show(frame)

	local tracking_categories = {}

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

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

	local data = {lang = lang, sc = script, pos_category = poscat, categories = {}, heads = head, translits = {"-"}, inflections = {}}
	
	--Baybayin
	local baybayin = {label = "Baybayin 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 #baybayin > 0 then table.insert(data.inflections, baybayin) end
	
    --Categorize words in Baybayin
    if script:getCode() == "Tglg" then
        table.insert(data.categories, "Pangasinan terms in Baybayin script")
    end

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

	local content = mw.title.new(PAGENAME):getContent()

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

end

local conjugation_types = {
	["actor I"] = {"1st actor trigger", "Pangasinan 1st actor focus verbs"}, -- on-/-on-
	["actor II"] = { "2nd actor trigger", "Pangasinan 2nd actor trigger verbs" }, --man-
	["actor potentive I"] = { "actor trigger potential mood", "Pangasinan 1st actor potential mood verbs" }, -- maka-
	["actor potentive II"] = { "actor trigger potential mood", "Pangasinan 2nd actor potential mood verbs" }, -- makapag-
	["object"] = { "object trigger", "Pangasinan object trigger verbs" }, -- -en
	["object potential"] = { "object trigger potential mood", "Pangasinan object trigger potential mood verbs" }, -- ma-
	["instrument"] = { "instrument trigger", "Pangasinan instrument trigger verbs" }, -- pag-
	["instrument potential"] = { "instrument trigger potential mood", "Pangasinan instrument trigger potential mood verbs" }, -- mapag-
	["instrument causative"] = { "1st instrument trigger causative mood", "Pangasinan 1st instrument trigger causative mood verbs" }, -- pagpa- -an
	["instrument causative II"] = { "2nd instrument trigger causative mood", "Pangasinan 2nd instrument trigger causative mood verbs" }, -- panagpa- 

}

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

    params = {
		[1] = {alias_of = 'head'},
		[2] = {alias_of = 'perf'},
		[3] = {alias_of = 'imperf'},
		[4] = {alias_of = 'past_imperf'},
		[5] = {alias_of = 'fut'},
		head = {list = true},
		head2= {},
		head3= {},
		perf = {list = true},
		imperf = {list = true},
		past_imperf = {list = true},
		fut = {list = true},
		rootword = {},
		type = {},
        type2 = {},
        type3 = {},
		b= {},
		b2= {},
		b3= {}
	}

	local args = require("Module:parameters").process(args,params)
	data.heads = args.head
	data.id = args.id
	local pattern = args.pattern
	
	args.perf.label = "perfective"
	args.imperf.label = "imperfective"
	args.past_imperf.label = "past imperfective"
	args.fut.label = "future"

	args.perf.accel = {form = "perf"}
	args.imperf.accel = {form = "imperf"}
	args.past_imperf.accel = {form = "past|imperf"}
	args.fut.accel = {form = "fut"}


	if #args.perf > 0 then table.insert(data.inflections, args.perf) end
	if #args.imperf > 0 then table.insert(data.inflections, args.imperf) end
	if #args.past_imperf > 0 then table.insert(data.inflections, args.past_imperf) end
	if #args.fut > 0 then table.insert(data.inflections, args.fut) end

    --Tagging verb trigger
    local conjtype = args["type"] or nil
    if conjtype and conjugation_types[conjtype] then
		table.insert(data.inflections, {label = conjugation_types[conjtype][1]})
		table.insert(data.categories, conjugation_types[conjtype][2])
    end
    
        local conjtype = args["type2"] or nil
    if conjtype and conjugation_types[conjtype] then
		table.insert(data.inflections, {label = conjugation_types[conjtype][1]})
		table.insert(data.categories, conjugation_types[conjtype][2])
	end	
		
		    local conjtype = args["type3"] or nil
    if conjtype and conjugation_types[conjtype] then
		table.insert(data.inflections, {label = conjugation_types[conjtype][1]})
		table.insert(data.categories, conjugation_types[conjtype][2])
    end

end

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

	--Mark adjective egrees
	    params = {
		[1] = {alias_of = 'head'},
		[2] = {alias_of = 'comp'},
		[3] = {alias_of = 'sup'},	
        head = {list = true},
		head2= {},
		head3= {},
		comp = {list = true},
	    sup = {list = true},
		int = {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.label = "comparative"
	args.sup.label = "superlative"

	args.comp.accel = {form = "comparative"}
	args.sup.accel = {form = "superlative"}

	if #args.comp > 0 then table.insert(data.inflections, args.comp) end
	if #args.sup > 0 then table.insert(data.inflections, args.sup) end
	
end

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

	--Tagging plurals
	    params = {
		[1] = {alias_of = 'head'},
		head = {list = true},
		head2= {},
		head3= {},
		plural = {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.plural.label = "plural"

	args.plural.accel = {form = "plural"}

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

return export