Documentation for this module may be created at Module:Sandbox/LVL/doc

-- This module generates a sortable wikitable of unreleased (disabled) items.
-- It filters data from Data:ItemData.json for items that have a Name and are marked IsDisabled: true.

local p = {}
local items_data = mw.loadJsonData("Data:ItemData.json")
local lang_module = require('Module:Lang')

function p.generate_table(frame)
    local itemType = frame.args[1]
    if not itemType or itemType == '' then
        return '<strong class="error">Error: Item type (Weapon, Armor, Tech) must be specified.</strong>'
    end

    local unreleased_items = {}

    -- 1. Collect all unreleased items of the specified type that have a name
    for item_key, item_data in pairs(items_data) do
        if item_data["Name"] ~= nil and item_data["IsDisabled"] == true and item_data["Slot"] == itemType then
            table.insert(unreleased_items, {
                name = item_data["Name"],
                key = item_key,
                cost = tonumber(item_data["Cost"]) or 0,
                slot = item_data["Slot"] or "Unknown"
            })
        end
    end

    -- If no items found, return a simple message
    if #unreleased_items == 0 then
        return "''No unreleased " .. itemType .. " items found in the data files.''"
    end

    -- 2. Sort them alphabetically by name for a consistent default order
    table.sort(unreleased_items, function(a, b) return a.name < b.name end)

    -- 3. Build the wikitext for the table
    local wikitext = '{| class="wikitable sortable" style="width:100%;"\n'
    wikitext = wikitext .. '! style="width:64px;" | Icon\n'
    wikitext = wikitext .. '! Name\n'
    -- The ItemType column is redundant if we are already using section headers, but can be kept for sorting.
    -- wikitext = wikitext .. '! style="width:100px;" | Type\n' 
    wikitext = wikitext .. '! style="width:120px;" | Cost\n'
    
    for _, item in ipairs(unreleased_items) do
        wikitext = wikitext .. '|-\n'
        -- Icon cell (using the display name for the image file)
        wikitext = wikitext .. '| [[File:' .. item.name .. '.png|64x64px|link=' .. item.name .. ']]\n'
        -- Name cell (linking to the image page, as a placeholder)
        wikitext = wikitext .. '| [[:File:'.. item.name ..'.png|' .. item.name .. ']]\n'
        -- Type cell (optional)
        -- wikitext = wikitext .. '| {{ItemType|' .. item.slot .. '|color=true}}\n'
        -- Cost cell (with data-sort-value for correct numerical sorting)
        wikitext = wikitext .. '| data-sort-value="' .. item.cost .. '" | {{Souls|' .. item.cost .. '}}\n'
    end

    wikitext = wikitext .. '|}'
    
    return frame:preprocess(wikitext)
end

return p