-- This module produces a "redirect" hatnote. It looks like this:
-- '"X" redirects here. For other uses, see Y.'
-- It implements the template.
--]]

local mHatnote = require('Module:Hatnote')
local mHatList = require('Module:Hatnote list')
local mArguments --lazily initialize
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local checkTypeMulti = libraryUtil.checkTypeMulti

local p = {}

-- Helper functions

local function getTitle(...)
	--Calls mw.title.new and returns either a title object, or nil on error
	local success, titleObj = pcall
	return success and titleObj or nil
end

-- Main functions

function p.redirect(frame)
	mArguments = require('Module:Arguments')
	local args = mArguments.getArgs
	--Get number of redirects
	local numRedirects = tonumber(frame.args[1]) or 1
	-- Create the options table.
	local options = {}
	options.selfref = args.selfref
	return p._redirect
end

function p._redirect
	-- Validate the input. Don't bother checking currentTitle, redirectTitle or
	-- targetTitle, as they are only used in testing.
	checkType
	checkType
	numRedirects = numRedirects or 1
	checkType
	options = options or {}
	currentTitle = currentTitle or mw.title.getCurrentTitle
	-- Get the table of redirects
	local redirect = {}
	for i = 1, numRedirects do
		-- Return an error if a redirect parameter is missing.
		if not args[i] then
			return mHatnote.makeWikitextError
		end
		redirect[i] = args[i]
	end
	-- Generate the text.
	local formattedRedirect = {}
	for k,v in pairs(redirect) do
		formattedRedirect[k] = "**" .. v .. "**"
	end
	local text = {
		mHatList.andList(formattedRedirect) .. '은 여기로 연결됩니다.',
		mHatList._forSee
	text = table.concat
	-- Functionality for adding categories	
	local categoryTable = {}
	local function addCategory(cat)
		if cat and cat ~= '' then
			-- Add by index to avoid duplicates
			categoryTable[string.format] = true
		end
	end
	--Generate tracking categories
	local mhOptions = {}
	local redirTitle
	for k,v in pairs(redirect) do
		-- We don't need a tracking category if the template invocation has been
		-- copied directly from the docs, or if we aren't in main- or category-space.
		if not v:find('^REDIRECT%d*$') and v ~= 'TERM' -- 
			and currentTitle.namespace == 0 or currentTitle.namespace == 14
		then
			redirTitle = redirectTitle or getTitle(v)
			if not redirTitle or not redirTitle.exists then
				addCategory('Missing redirects')
			elseif not redirTitle.isRedirect then
				if string.find then
					addCategory('Articles with redirect hatnotes impacted by RfD')
				else
					addCategory('Articles with redirect hatnotes needing review')
				end
			else
				local target = targetTitle or redirTitle.redirectTarget
				if target and target ~= currentTitle then
					addCategory('Articles with redirect hatnotes needing review')
				end
			end
		end

		-- Generate the options to pass to Module:Hatnote.
		if currentTitle.namespace == 0 and not mhOptions.selfref
			and redirTitle and redirTitle.namespace ~= 0
		then
			-- We are on a mainspace page, and the hatnote starts with something
			-- like "Wikipedia:Foo redirects here", so automatically label it as
			-- a self-reference.
			mhOptions.selfref = true
		else
			mhOptions.selfref = options.selfref
		end
	end
	--concatenate all the categories
	local category = ''
	for k,v in pairs(categoryTable) do
		category = category .. k
	end

	return mHatnote._hatnote .. category
end