local export = {}

local lang = require("Module:languages").getByCode("pl")

local function collect_numbered(args, prefix, target)
	local was_empty = false
	if args[prefix] then
		table.insert(target, args[prefix])
		local i = 2
		while args[prefix .. i] do
			table.insert(target, args[prefix .. i])
			i = i + 1
		end
	end
	
	return target
end

local function add_if_nonempty(infls, item)
	if #item > 0 then
		table.insert(infls, item)	
	end
end

local function track(page)
	require("Module:debug").track("pl-headword/" .. page)
	return true
end

-- verbs

-- Aspect normalization table.
-- Note that determinate, indeterminate and frequentative aspects mainly make
-- sense for verbs of motion. Most verbs will have either perfective or
-- imperfective aspect.
local norm_aspect = {
	-- perfective aspect: zrobić, dojechać, pójść
	["p"]            = "pf",
	["pf"]           = "pf",
	["perf"]         = "pf",
	["perfective"]   = "pf",
	-- imperfective aspect: robić, karać
	["i"]            = "impf",
	["impf"]         = "impf",
	["imperf"]       = "impf",
	["imperfective"] = "impf",
	-- determinate aspect: jechać, iść
	-- implies imperfective
	["impf-det"]     = "impf-det",
	["det"]          = "impf-det",
	-- indeterminate aspect: jeździć, chodzić
	-- implies imperfective
	["impf-indet"]   = "impf-indet",
	["indet"]        = "impf-indet",
	-- frequentative aspect: chadzać
	-- implies imperfective and indeterminate
	["freq"]         = "impf-freq",
	["if"]           = "impf-freq",
	["impf-freq"]    = "impf-freq",
}

function export.show_verb(frame)
	local args = frame:getParent().args
	
	local data = {lang = lang, pos_category = "Động từ", categories = {}, sort_key = args.sort, heads = {args.head}, genders = {}, inflections = {}}
	local tracking_categories = {}
	local aspect_args = args.aspect or args.asp or args.a or args[1] or false
	
	if args.aspect or args.asp or args.a then
		track("aspect")
	end
	
	local perf = { label = "h.thành" }
	local imperf = { label = "chưa h.thành" }
	local det = { label = "imperfective determinate" }
	local indet = { label = "indeterminate" }
	local freq = { label = "frequentative" }
		
	for aspect_arg in mw.ustring.gmatch(aspect_args or "", "[^,]+") do
		local aspect = norm_aspect[aspect_arg] or error(aspect_arg .. " is not a valid aspect. Please specify a valid one.")

		if aspect == "pf" then
			table.insert(data.genders, "pf")
			table.insert(data.categories, "Động từ hoàn thành tiếng Ba Lan")
		elseif (aspect == "impf") or (aspect == "impf-det") or (aspect == "impf-indet") or (aspect == "impf-freq") then
			table.insert(data.genders, "impf")
			table.insert(data.categories, "Động từ chưa hoàn thành tiếng Ba Lan")
		else
			table.insert(data.genders, "?")
			table.insert(data.categories, "Polish verbs missing aspect")
		end

		if aspect == "impf-det" then
			table.insert(data.inflections, {label = "determinate"})
			table.insert(data.categories, "Polish determinate verbs")
		elseif aspect == "impf-indet" then
			table.insert(data.inflections, {label = "indeterminate"})
			table.insert(data.categories, "Polish indeterminate verbs")
		elseif aspect == "impf-freq" then
			table.insert(data.inflections, {label = "indeterminate"})
			table.insert(data.inflections, {label = "frequentative"})
			table.insert(data.categories, "Polish indeterminate verbs")
			table.insert(data.categories, "Polish frequentative verbs")
		end
	end

	collect_numbered(args, "perf", perf)
	collect_numbered(args, "pf", perf)
	collect_numbered(args, "imperf", imperf)
	collect_numbered(args, "impf", imperf)
	collect_numbered(args, "det", det)
	collect_numbered(args, "indet", indet)
	collect_numbered(args, "freq", freq)
	
	-- "pref" would never come before "det" (pf; det-inedt, impf; det-indet, indet; det-pf, etc.)
	add_if_nonempty(data.inflections, det)
	add_if_nonempty(data.inflections, perf)
	add_if_nonempty(data.inflections, imperf)
	add_if_nonempty(data.inflections, indet)
	add_if_nonempty(data.inflections, freq)

	require("Module:headword/util").parse_overrides_in_inflections(data.inflections)
	return require("Module:headword").full_headword(data) ..
		require("Module:utilities").format_categories(tracking_categories, lang, args.sort)
end

function export.show_participle(frame)
	local args = frame:getParent().args
	
	local data = {lang = lang, pos_category = "participles", categories = {}, sort_key = args.sort, heads = {args.head}, genders = {}, inflections = {}}
	local tracking_categories = {}
	local aspect = norm_aspect[args.aspect or args.asp or args.a or false] or "?"
	
	-- type of participle
	local ptype = args[1]
	if not ptype then
		local PAGENAME = mw.title.getCurrentTitle().fullText
		
		if PAGENAME:match("ąc[yae]$") then -- biegnący, 
			ptype = "aadj"			
		elseif PAGENAME:match("[nt][yae]$") then -- otwarty, uwielbiany
			ptype = "padj"
		elseif PAGENAME:match("ąc$") then
			ptype = "cadv"
		elseif PAGENAME:match("szy$") then
			ptype = "aadv"
		end
	end

	if ptype then
		if ptype == "pasv-adj" or ptype == "padj" then
			table.insert(data.categories, "Polish passive adjectival participles")
		elseif ptype == "actv-adj" or ptype == "aadj" then
			table.insert(data.categories, "Polish active adjectival participles")
		elseif ptype == "antr-adv" or ptype == "aadv" then
			table.insert(data.categories, "Polish anterior adverbial participles")
		elseif ptype == "cont-adv" or ptype == "cadv" then
			table.insert(data.categories, "Polish contemporary adverbial participles")
		else
			table.insert(tracking_categories, "pl-participle with unrecognized type")
		end
	else
		table.insert(tracking_categories, "pl-participle without type")
	end

	if (aspect == "pf") or (aspect == "pf-it") or (aspect == "pf-sem") then
		table.insert(data.genders, "pf")
	elseif (aspect == "impf") or (aspect == "impf-it") or (aspect == "impf-dur") then
		table.insert(data.genders, "impf")
	end

	if aspect == "impf-dur" then
		table.insert(data.inflections, {label = "durative"})
	elseif aspect == "impf-it" then
		table.insert(data.inflections, {label = "iterative"})
	elseif aspect == "pf-sem" then
		table.insert(data.inflections, {label = "semelfactive"})
	elseif aspect == "pf-it" then
		table.insert(data.inflections, {label = "iterative"})
	end

	require("Module:headword/util").parse_overrides_in_inflections(data.inflections)
	return require("Module:headword").full_headword(data) ..
		require("Module:utilities").format_categories(tracking_categories, lang, args.sort)
end

-- nouns and proper nouns

local noun_gender_cat = {
	["m"     ] = "Danh từ giống đực tiếng Ba Lan",
	["m-an"  ] = "Danh từ giống đực tiếng Ba Lan",
	["m-in"  ] = "Danh từ giống đực tiếng Ba Lan",
	["m-pr"  ] = "Danh từ giống đực tiếng Ba Lan",
	["f"     ] = "Danh từ giống cái tiếng Ba Lan",
	["n"     ] = "Danh từ giống trung tiếng Ba Lan",
	["vr"	 ] = "Polish virile nouns",
	["nv"	 ] = "Polish nonvirile nouns",
	["m-p"   ] = false,
	["m-pr-p"] = false,
	["m-an-p"] = false,
	["m-in-p"] = false,
	["f-p"   ] = false,
	["n-p"   ] = false,
	["p"     ] = false,
}

function export.show_noun(frame)
	local args = frame:getParent().args
	
	local data = {lang = lang, pos_category = frame.args.proper and "Danh từ riêng" or "Danh từ", categories = {}, sort_key = args.sort, heads = {args.head}, genders = {}, inflections = {}}
	local tracking_categories = {}
	
	if args.indecl and args.indecl ~= '' then
		table.insert(data.inflections, { label = "không biến cách" })
		table.insert(data.categories, "Danh từ tiếng Ba Lan không biến cách")
	end

	add_if_nonempty(data.inflections, collect_numbered(args, "abbr", { label = "viết tắt" }))
	add_if_nonempty(data.inflections, collect_numbered(args, "dim", { label = "giảm nhẹ" }))
	add_if_nonempty(data.inflections, collect_numbered(args, "aug", { label = "tăng to" }))
	add_if_nonempty(data.inflections, collect_numbered(args, "pej", { label = "nghĩa xấu" }))
	add_if_nonempty(data.inflections, collect_numbered(args, "f", { label = "giống cái" }))
	add_if_nonempty(data.inflections, collect_numbered(args, "m", { label = "giống đực" }))
	add_if_nonempty(data.inflections, collect_numbered(args, "n", { label = "giống trung" }))
	add_if_nonempty(data.inflections, collect_numbered(args, "adj", { label = "[[Phụ lục:Từ điển thuật ngữ#tính từ liên hệ|tính từ liên hệ]]" }))

	local g, genders = args.g or args[1]
	if g then
		if mw.ustring.find(g, ",") then
			local gg = g
			data.genders = { }
			for g in mw.ustring.gmatch(gg, "[^,]+") do
				local gcat = noun_gender_cat[g]
				if gcat == nil then
					table.insert(tracking_categories, "Polish nouns with unsupported gender values")
				else
					table.insert(data.genders, g)
					if gcat then
						table.insert(data.categories, gcat)
					end
					if g == "m" then
						table.insert(tracking_categories, "Polish masculine nouns without animacy")
					end
				end
			end
			if #data.genders < 1 then
				data.genders = { "?" }
			end
		elseif noun_gender_cat[g] ~= nil then
			data.genders = { g }
			table.insert(data.categories, noun_gender_cat[g] or nil)
			if g == "m" then
				table.insert(tracking_categories, "Polish masculine nouns without animacy")
			end
		else
			data.genders = { "?" }
		end
	else
		data.genders = { "?" }
	end

	require("Module:headword/util").parse_overrides_in_inflections(data.inflections)
	return require("Module:headword").full_headword(data) ..
		require("Module:utilities").format_categories(tracking_categories, lang, args.sort)
end

-- adjectives and adverbs

function export.show_adj_adv(frame)
	local args = frame:getParent().args
	
	local data = {lang = lang, categories = {}, sort_key = args.sort, heads = {args.head}, inflections = {}}
	local tracking_categories = {}
	
	local PAGENAME = mw.title.getCurrentTitle().text
	
	if frame.args.adverb then
		data.pos_category = "Phó từ"
	else
		data.pos_category = "Tính từ"
	end
	
	local comparative, superlative = {}, {}
	
	local i = 1
	
	if args[1] == "-" then
		if frame.args.adverb then
			table.insert(data.categories, "Polish uncomparable adverbs")
		else
			table.insert(data.categories, "Polish uncomparable adjectives")
		end

		if not args[2] then
			table.insert(data.inflections, { label = "not comparable" })
		else
			table.insert(data.inflections, { label = "not always comparable" })
			i = i + 1
			while args[i] do
				local comp, super
				if frame.args.adverb and ((args[i] == "j") or (args[i] == "regular")) then
					table.insert(comparative, PAGENAME .. "j")
					table.insert(superlative, "naj" .. PAGENAME .. "j")
				elseif args[i] == "bardziej" then
					table.insert(comparative, "[[bardziej]] " .. PAGENAME)
					table.insert(superlative, "[[najbardziej]] " .. PAGENAME)
				elseif args[i] then
					table.insert(comparative, args[i])
					table.insert(superlative, "naj" .. args[i])
				end
				i = i + 1
			end

			comparative.label, comparative.accel = "so sánh hơn", {form = "so sánh hơn"}
			superlative.label, superlative.accel = "so sánh nhất", {form = "so sánh nhất"}

			table.insert(data.inflections, comparative)
			table.insert(data.inflections, superlative)
		end
	elseif args[1] then
		while args[i] do
			local comp, super
			if frame.args.adverb and ((args[i] == "j") or (args[i] == "regular")) then
				table.insert(comparative, PAGENAME .. "j")
				table.insert(superlative, "naj" .. PAGENAME .. "j")
			elseif args[i] == "bardziej" then
				table.insert(comparative, "[[bardziej]] " .. PAGENAME)
				table.insert(superlative, "[[najbardziej]] " .. PAGENAME)
			elseif args[i] then
				table.insert(comparative, args[i])
				table.insert(superlative, "naj" .. args[i])
			end
			i = i + 1
		end

		comparative.label, comparative.accel = "so sánh hơn", {form = "so sánh hơn"}
		superlative.label, superlative.accel = "so sánh nhất", {form = "so sánh nhất"}
		
		table.insert(data.inflections, comparative)
		table.insert(data.inflections, superlative)
	else
		table.insert(tracking_categories, "pl-adj or pl-adv without comparative form")
	end
	
	if not frame.args.adverb then
		if args.adv then
			if args.adv ~= '-' then
				add_if_nonempty(data.inflections, collect_numbered(args, "adv", { label = "phó từ" }))
			end
		else
			table.insert(tracking_categories, "pl-adj without corresponding adverb")
		end
	end

	add_if_nonempty(data.inflections, collect_numbered(args, "abbr", { label = "viết tắt" }))
		
	add_if_nonempty(data.inflections, collect_numbered(args, "mpcom", { label = "Middle Polish comparative" }))
	add_if_nonempty(data.inflections, collect_numbered(args, "mpsup", { label = "Middle Polish superlative" }))

	require("Module:headword/util").parse_overrides_in_inflections(data.inflections)
	return require("Module:headword").full_headword(data) ..
		require("Module:utilities").format_categories(tracking_categories, lang, args.sort)
end

return export