Module:Fiction redirect category handler/Franchise

require("strict")

local p = {}

--[[ 
Local function which adds the names relevant TV series to the args table.
Franchise templates, such as the Arrowverse-specific one, have short parameters that editors can use
instead of the full TV series name.
This functions checks if the user-input parameters match the ones on the list,
and if they do, adds them to the list.

Franchise templates can also use a "franchise" default parameter
so all members will automatically be tagged in the franchise category,
as well as the specific TV series category.
--]]
local function getListOfSeriesTags(templateArgs, franchise, tvSeriesNameList, validArgs)
	local seriesArgs = {}
	for i, v in ipairs(tvSeriesNameList) do
		local tvSeries = tvSeriesNameList[i]
		-- Check if the user-input parameter matches one on the list;
		-- If it matches, add the series name to the list.
		if (templateArgs[tvSeries.argName]) then
			table.insert(seriesArgs, tvSeries.seriesName)
			table.insert(validArgs, tvSeries.argName)
		end
	end
	if (#seriesArgs > 0) then
		table.insert(seriesArgs, franchise)
		-- This parameter is used by the relevant "R to fictional x" templates
		-- to enable multi-series categories.
		seriesArgs["multi"] = "yes"
		templateArgs["multi_series_name_table"] = seriesArgs
	else
		templateArgs["series_name"] = franchise
	end

	return templateArgs, validArgs
end

--[[ 
Public function which handles the main operation.
--]]
function p.main(args, objectType, franchise, tvSeriesNameList)
	local validArgs = {"correct_disambiguation", "multi", "multi_series_name_table"}
	args, validArgs = getListOfSeriesTags(args, franchise, tvSeriesNameList, validArgs)
	
	-- Franchise articles, such as the Arrowverse and the Marvel Cinematic Universe,
	-- use the franchise name as disambiguation for articles, and not a TV series or film name.
	args["correct_disambiguation"] = franchise

	local fictionRedirectCategoryHandler = require("Module:Fiction redirect category handler")
	return fictionRedirectCategoryHandler["_" .. objectType](args, validArgs)
end

--[[
Public function which is used for the /testcases to verify correct series name output.
--]]
function p.testSeriesName(args, franchise, tvSeriesNameList)
	args = getListOfSeriesTags(args, franchise, tvSeriesNameList, {})
	if (args.series_name) then
		return args.series_name
	else
		return args.multi_series_name_table[1]
	end
end

return p