Jump to content

Module:Ingredient link list

From Tensura Wiki

Documentation for this module may be created at Module:Ingredient link list/doc

local p = {}
local f = mw.getCurrentFrame()

-- 1) Load the alias table
local aliases = require("Module:Inventory slot/Aliases")

-- Helper function to separate item name from quantity
local function separateItemCount(str)
    if not str or type(str) ~= "string" then
        return nil, nil
    end

    local mainName, stack = str:match('^(.-),(%d+)$')
    if mainName and stack then
        return mw.text.trim(mainName), mw.text.trim(stack)
    end
    return mw.text.trim(str), nil
end

function p.genlink(args)
    -- If first parameter is missing altogether, show an error
    if args[1] == nil then
        return { errno = 1 }
    end

    local sep = args.sep or ';'
    local firstarg = tonumber(args.firstarg) or 1
    if firstarg >= 2 and (not args.maxargs or tonumber(args.maxargs) == nil) then
        return { errno = 2 }
    end

    local maxargs = tonumber(args.maxargs) or (#args - 1)
    local output_array = {}

    mw.logObject(args) -- Debug: Log all arguments passed

    for i = firstarg, maxargs do
        local paramVal = args[i]
        mw.log("Processing argument index: ", i, " Value: ", paramVal) -- Debug
        if paramVal and type(paramVal) == "string" and mw.text.trim(paramVal) ~= '' then
            local trimmedParam = mw.text.trim(paramVal)
            local name_pieces = {}

            -- Split each parameter by sep (semicolon by default)
            for current_name in mw.text.gsplit(trimmedParam, sep) do
                local piece = mw.text.trim(current_name)
                if piece ~= '' then
                    name_pieces[piece] = true
                end
            end

            local converted_names = {}
            for rawName, exist in pairs(name_pieces) do
                if exist then
                    local prefix = ''
                    if rawName:find('^Matching ') then
                        prefix = 'Matching '
                        rawName = rawName:gsub('^Matching ', '')
                    elseif rawName:find('^Any ') then
                        prefix = 'Any '
                        rawName = rawName:gsub('^Any ', '')
                    end

                    local itemName, itemCount = separateItemCount(rawName)
                    local aliasEntry = aliases[itemName]
                    local resolvedLink, displayName

                    if aliasEntry and type(aliasEntry) == 'table' then
                        resolvedLink = aliasEntry.link or itemName
                        displayName = aliasEntry.name or itemName
                    else
                        resolvedLink = itemName
                        displayName = itemName
                    end

                    local linkMarkup = prefix .. '[[' .. resolvedLink .. '|' .. displayName .. ']]'
                    if itemCount then
                        linkMarkup = linkMarkup .. ' x' .. itemCount
                    end

                    if linkMarkup and linkMarkup ~= '' then
                        table.insert(converted_names, f:preprocess(linkMarkup))
                    end
                end
            end

            table.insert(output_array, table.concat(converted_names, ' or<br>'))
        end
    end

    return table.concat(output_array, ' +<br>')
end

function p.main()
    local args = require('Module:ProcessArgs').merge(true)
    local result = p.genlink(args)
    if type(result) == 'string' then
        return result
    end

    local error_prompts = {
        'The first anonymous parameter could not be empty!',
        'When the argument "firstarg" is equals to or greater than 2, the argument "maxargs" must be set!'
    }
    return f:expandTemplate{ title = 'Error', args = { error_prompts[result.errno] } }
end

return p