模組:Delete2

local mDelete = require('Module:Delete')
local mDelcat = require('Module:Delcat')
local getArgs = require('Module:Arguments').getArgs
local mYesno

local data = require('Module:Delete/data')
local data_sandbox = require('Module:Delete/data/sandbox')

local z = {}

local function yesno(value)
	if not mYesno then
		mYesno = require('Module:Yesno')
	end
	return mYesno(value)
end

local function extractAliases(item)
    allnames = {item['code']}
    for j, alias in ipairs(item['aliases']) do
        table.insert(allnames, alias)
    end
    return allnames
end

local function extractShortDesc(item)
    if item['description'] then
        return item['description']:gsub('{', '{'):gsub('}', '}'):gsub('|', '|')
    else
        return ''
    end
end

local function isSandbox(frame)
	return yesno(frame.args.sandbox)
end

local function addDeprecated(funcName, output)
	mw.addWarning(mw.ustring.format('{{tlm|#invoke|Delete2|%s_sandbox}}已棄用,請改用{{tlm|#invoke|Delete2|%s|sandbox{{=}}true}}。', funcName, funcName))
	return output .. '[[Category:Delete2模組使用了已棄用的sandbox函數]]'
end

local function makeReasonsBox(frame, data)
    local wt = [=[
{|class="wikitable
|-
!速刪編號!!可使用的代碼!!簡介!!詳細說明
    ]=]
    for i, item in ipairs(data) do
        allnames = extractAliases(item)
        wt = wt .. string.format([=[
|-
|%s
|
* %s
|%s
|%s
        ]=]
        , item['code'], mw.text.listToText(allnames, '\n* ', '\n* '), item['criteria'], extractShortDesc(item):gsub('{{subst:', '{{tls|'):gsub('{{', '{{tl|'):gsub('|', '|'):gsub('}}', '}}'))
    end
    wt = wt .. '|}'
    return frame:preprocess(wt)
end

function z.reasonsBox(frame)
	return makeReasonsBox(frame, isSandbox(frame) and data_sandbox or data)
end

-- @deprecated
function z.reasonsBox_sandbox(frame)
	return addDeprecated('reasonsBox', makeReasonsBox(frame, data_sandbox))
end

local function CSD_reason(frame, isSandbox)
    local args = getArgs(frame, {
        parentFirst = true
    })
    local rows = mDelete._input(args, {
        maxArgs = 1,
        data = isSandbox and data_sandbox or data
    })
    local item = rows and rows[1] or nil
    if not item or not item['code'] then
        return '[[WP:CSD|' .. (args[2] or '快速删除方针') .. ']]'
    elseif item['code'] == '' then
        return require('Module:Error').error{'錯誤:無法找到速刪代碼為<code>' .. args[1] .. '</code>的速刪項目。'}
    else
        local text = ''
        if args[2] ~= nil and args[2] ~= '' then
            text = args[2]
        elseif args['notCSD'] ~= nil and args['notCSD'] ~= '' then
        	text = item['code']
        else
            text = 'CSD ' .. item['code']
        end
        if args.nolink then
            return text
        elseif args.reason then
            return '[[WP:CSD#' .. item['code'] .. '|' .. text .. ']]:<span title="' .. extractShortDesc(item) .. '">' .. item['criteria'] .. '</span>'
        elseif args.onlyreason then
            return '<span title="' .. extractShortDesc(item) .. '">' .. item['criteria'] .. '</span>'
        else
            return '[[WP:CSD#' .. item['code'] .. '|' .. text .. ']]'
        end
    end
end

function z.CSD_reason(frame)
    return CSD_reason(frame, isSandbox(frame))
end

-- @deprecated
function z.CSD_reason_sandbox(frame)
	return addDeprecated('CSD_reason', makeReasonsBox(frame, CSD_reason(frame, true)))
end

function z._getInputWithoutError(args)
   local rows = mDelete._input(args, {
        maxArgs = 10,
        checkFunc = false,
        checkFile = true,
        parseCommit = false
    })
    if not rows then
        return nil
    end
    local outputs = {}
    for _, item in ipairs(rows) do
        if item['code'] and item['code'] ~= '' then
            local imgLink = ''
            -- special case for F1/F5
            if item['code'] == 'F1' or item['code'] == 'F5' then
                if item['imgLink'] then
                    imgLink = '(' .. item['imgLink'] .. ')'
                end
            end
         
            table.insert(outputs, '* [[WP:CSD#' .. item['code'] .. '|CSD ' .. item['code'] .. ']]:<b>' .. item['criteria'].. '</b>' .. imgLink )
        elseif item['code'] == '' then
        	local arg = item['criteria']
            -- try to read it as a title
            local success, aTitle = mw.title.new(mw.text.trim(arg))
            if aTitle and aTitle.exists then
                table.insert(outputs, '* <b>' .. '[[:' .. arg .. ']]</b>')
            else
                arg = string.gsub(arg, '^([*:#]*)(.*)', '%1<b>%2</b>')
                table.insert(outputs, '* ' .. arg)
            end
        end

        if item['commit'] then
            table.insert(outputs, '*' .. item['commit'])
        end
    end
    
    return mw.text.trim(table.concat(outputs, '\n'))
end

function z.getInputWithoutError(frame)
    local args = frame:getParent().args
    local out = z._getInputWithoutError(args);
    if not out then
        return '* <span style="color:#080;">(未填寫理由)</span>'
    end
    return out
end

z.reasons_box = z.reasonsBox
-- @deprecated
z.reasonsBox_sb = z.reasonsBox_sandbox
-- @deprecated
z.reasons_box_sandbox = z.reasonsBox_sandbox
-- @deprecated
z.reasons_box_sb = z.reasonsBox_sandbox
-- @deprecated
z.CSD_reason_sb = z.CSD_reason_sandbox

return z