Documentation for this module may be created at ಮಾಡ್ಯೂಲ್:RfD/doc

require('Module:No globals')

local p = {}

local getTargetFromText = require('Module:Redirect').getTargetFromText
local messageBox

local mboxText = "%s'''The purpose of this redirect is currently being discussed by the Wikipedia community."
	.. " The outcome of the discussion may result in a change of this page, or possibly its deletion in accordance with Wikipedia's [[Wikipedia:Deletion policy|deletion policy]].''' <br />"
	.. " Please share your thoughts on the matter at '''[[Wikipedia:Redirects for discussion/Log/%s %s %s#%s|this redirect's entry]]''' on the [[Wikipedia:Redirects for discussion|Redirects for discussion]] page.<br />"
	.. "  '''Click on the link below''' to go to the current destination page.<br />"
	.. "<small>Please notify the good-faith creator and any main contributors of the redirect by placing <code>&#123;&#123;[[Wikipedia:Substitution|subst]]:[[Template:RFDNote|RFDNote]]&#124;%s&#125;&#125; &#126;&#126;&#126;&#126;</code> on their talk page.</small>"
local errorMessage = '<span class="error">Error: Unable to determine the redirect\'s target. If this page is a soft redirect, then this error can be ignored. Otherwise, please make sure that [[WP:RFD#HOWTO|the instructions for placing this tag]] were followed correctly, in particular that the redirect\'s old content was passed in the <code>content</code> parameter.</span><br />'
local deleteReason = '[[Wikipedia:Redirects for discussion]] debate closed as delete'

local function makeRfdNotice(args)
	local currentTitle = mw.title.getCurrentTitle()
	if not messageBox then
		messageBox = require('Module:Message box')
	end
	local discussionPage = args[1] and mw.text.trim(args[1])
	if discussionPage == '' then
		discussionPage = nil
	end
	local target = getTargetFromText(args.content)
	local isError = not target or not mw.title.new(target)
	return string.format('%s<span id="delete-reason" style="display:none;">%s</span>%s%s',
		messageBox.main('mbox', {
			type = 'delete',
			image = 'none',
			text = string.format(mboxText, isError and errorMessage or '', args.year, args.month, args.day, discussionPage or currentTitle.prefixedText, mw.text.nowiki(currentTitle.prefixedText))
		}),
		mw.uri.encode(deleteReason),
		args.category or string.format('[[ವರ್ಗ:Redirects for discussion|%s]]', currentTitle.text),
		isError and '[[ವರ್ಗ:RfD errors]]' or ''
	)
end

-- called if the redirect up for discussion is being transcluded
p['1'] = function(frame)
	local target = getTargetFromText(frame.args.content)
	if target then
		target = mw.title.new(target)
	end
	if target and not target.isRedirect and target ~= frame:getParent():getTitle() then
		local pframe = frame:getParent()
		-- We should actually be calling expandTemplate on the grandparent rather than on the parent, but we can't do that yet
		-- Since we don't have grandparent access, though, it means the thing we're calling doesn't either, so it doesn't really matter yet

		-- Without [[gerrit:84985]], we need to convert args to a simple table for it to actually work
		local args = {}
		for k, v in pairs( pframe.args ) do
			args[k] = v
		end
		return pframe:expandTemplate{title = ':' .. target.prefixedText, args = args}
	else
		return p[''](frame)
	end
end

-- called if the redirect up for discussion is being viewed
p[''] = function(frame)
	return makeRfdNotice(frame.args) .. '\n' .. frame.args.content
end

local substText = "{{<includeonly>safesubst:</includeonly>#invoke:RfD|<includeonly>1</includeonly>|%s|%s%s\n"
	.. "<!-- The above content is generated by {{subst:rfd}}. -->\n<!-- End of RFD message. Don't edit anything above here, but feel free to edit below here. -->|content=\n%s\n"
	.. "<!-- Don't add anything after this line. -->\n}}"
local dateText = 'month = %B\n|day = %e\n|year = %Y\n|time = %R\n|timestamp = %Y%m%d%H%M%S'

-- called during subst when the template is initially placed on the page
function p.main(frame)
	local titleText
	local pframe = frame:getParent()
	local pargs = pframe.args
	local retval = string.format(substText, pargs.FULLPAGENAME or pargs[1] or '', os.date(dateText), pframe:getTitle() == mw.title.getCurrentTitle().prefixedText and '|category=' or '', pargs.content or '')
	if mw.isSubsting() then
		return retval
	else
		return frame:expandTemplate{title = 'Template:Error:must be substituted', args = {'rfd'}} .. frame:preprocess(retval)
	end
end

return p