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

-- Exceptions if the link is different from the item name local linkOverrides = {

   ["Bullet Lifesteal"] = "Bullet Lifesteal (item)",
   ["Spirit Lifesteal"] = "Spirit Lifesteal (item)"

}

-- Cached items grouped by slot to prevent redundant full-table evaluations local cached_slots = nil local function get_items_by_slot(slot)

   if not cached_slots then
       cached_slots = { table = {}, Weapon = {}, Armor = {}, Tech = {} }
       for item_key, item_data in pairs(items_data) do
           local this_slot = item_data["Slot"]
           if this_slot and cached_slots[this_slot] then
               table.insert(cached_slots[this_slot], { key = item_key, data = item_data })
           end
       end
   end
   return cached_slots[slot] or {}

end

-- With debug_mode on, it outputs unprocessed wikitext local function process_debug_mode(wikitext, debug_mode)

   if debug_mode == 'true' then
       return wikitext
   elseif debug_mode == 'false' or debug_mode == nil then
       return mw.getCurrentFrame():preprocess(wikitext)
   else
       return "debug_mode must be 'true' or 'false'"
   end

end

-- Writes list of items of a certain slot within the min and max soul bounds local function write_wrapped_item_list(slot, min_souls, max_souls, template, sep, filter_mode)

   if slot ~= 'Weapon' and slot ~= 'Armor' and slot ~= 'Tech' then
       return 'slot must be Weapon, Armor (Vitality), or Tech (Spirit)'
   end
   
   local min_souls = tonumber(min_souls)
   local max_souls = tonumber(max_souls)
   if min_souls == nil or max_souls == nil then return 'Min/Max souls must be numerical' end
   
   filter_mode = filter_mode or "all"
   
   -- Lifted out of the loop: Language code doesn't change per item
   local lang_code = lang_module.get_lang_code() 
   
   local items = {}
   local slot_items = get_items_by_slot(slot) -- Drastically reduces search pool size
   
   for _, item_entry in ipairs(slot_items) do
       local item_key = item_entry.key
       local item_data = item_entry.data
       
       local this_cost = tonumber(item_data["Cost"])
       local is_street_brawl = item_data["StreetBrawl"] == true
       local should_include = true
       
       -- Filter logic for Street Brawl items
       if filter_mode == "street_brawl_only" and not is_street_brawl then
           should_include = false
       elseif filter_mode == "exclude_street_brawl" and is_street_brawl then
           should_include = false
       end
       
       if should_include and item_data["Name"] ~= nil and item_data["IsDisabled"] == false and this_cost ~= nil then
           if this_cost >= min_souls and this_cost < max_souls then
               local item_name_english = lang_module.get_string(item_key, "en")
               local item_link = linkOverrides[item_name_english] or item_name_english
               local item_name_local = lang_module.get_string(item_key, lang_code)
               
               local item_template
               if template == "ItemIcon" then
                   if lang_code == "en" then
                       item_template = "Icon not found ('" .. item_name_english .. "'). "
                   else
                       item_template = "Icon not found ('" .. item_name_english .. "'). "
                   end
                   
                   local search_data = item_name_english
                   if lang_code ~= "en" and item_name_local then
                       search_data = search_data .. " " .. item_name_local
                   end
                   
                   item_template = '' .. item_template .. ''
               elseif template == "ItemBox" then
                   if lang_code == "en" then
                       if is_street_brawl then
                           item_template = "

" .. item_name_english .. "

Active

"

                       else
                           item_template = "

<div class="itembox itembox-tier-Item Not Found. itembox-icon-v2 itembox-wear-v4 itembox-texture-v1 " style="font-size:10px;">

" .. item_name_english .. "

Active

"

                       end
                   else
                       local link_local = item_link .. "/" .. lang_code
                       if is_street_brawl then
                           item_template = "

<div class="itembox itembox-tier-Item Not Found. itembox-icon-v2 itembox-wear-v4 itembox-texture-v1 " style="font-size:10px;">

" .. item_name_english .. "

Active

"

                       else
                           item_template = "

<div class="itembox itembox-tier-Item Not Found. itembox-icon-v2 itembox-wear-v4 itembox-texture-v1 " style="font-size:10px;">

" .. item_name_english .. "

Active

"

                       end
                   end
               else
                   return "Invalid template type"
               end
               table.insert(items, item_template)
           end
       end
   end
   
   -- Order list alphabetically
   table.sort(items)
   
   -- Append closing tags and bullets
   if template == "ItemIcon" then
       local total_items = #items
       for i = 1, total_items do
           if i == total_items then
               items[i] = items[i] .. ''
           else
               items[i] = items[i] .. ''
           end
       end
   end
   
   return table.concat(items, sep)

end

-- for Template:Item Navbox function p.get_item_nav_bulletpoints(slot, min_souls, max_souls, debug_mode, filter_mode)

   if type(slot) == "table" and slot.args then
       local frame = slot
       slot = frame.args[1]
       min_souls = frame.args[2]
       max_souls = frame.args[3]
       debug_mode = frame.args["debug_mode"]
       filter_mode = frame.args["filter"]
   end
   
   local item_list = write_wrapped_item_list(slot, min_souls, max_souls, "ItemIcon", , filter_mode)
   return process_debug_mode(item_list, debug_mode)

end

-- for Template:Infobox ShopItems function p.get_item_nav_cards(slot, min_souls, max_souls, debug_mode, filter_mode)

   if type(slot) == "table" and slot.args then
       local frame = slot
       slot = frame.args[1]
       min_souls = frame.args[2]
       max_souls = frame.args[3]
       debug_mode = frame.args["debug_mode"]
       filter_mode = frame.args["filter"]
   end
   
   local item_list = write_wrapped_item_list(slot, min_souls, max_souls, "ItemBox", ' ', filter_mode)
   return process_debug_mode(item_list, debug_mode)

end

-- for Template:Item Navbox subgroup rows function p.write_item_slot_subgroup(frame)

   local slot = frame.args[1]
   local type_func = frame.args[2]
   local debug_mode = frame.args['debug_mode']
   local street_brawl_label = frame.args['street_brawl_label'] or 'Legendary'
   
   if slot == nil then return "'slot' parameter is required" end
   
   local template_args = {
       ["groupstyle"] = "background-color:" .. util_module.get_slot_color(slot) .. ";width:10%;min-width:70px;border-radius: 8px 0 0 8px",
       ["grouppadding"] = "5px",
       ["listpadding"] = "0 0.25rem",
   }
   
   local soul_style = "font-size: 12px; text-shadow: 1px 1px rgba(0, 0, 0, 0.3);"
   local prices = generic_module.get_item_price_per_tier()
   local group_index = 1
   
   for i, souls in ipairs(prices) do
       local min_souls = souls
       
       if min_souls ~= 0 then 
           local max_souls = prices[i+1] or (min_souls * 10)
           
           local list
           if type_func == 'get_item_nav_cards' then
               list = p.get_item_nav_cards(slot, min_souls, max_souls, debug_mode, "exclude_street_brawl")
           elseif type_func == 'get_item_nav_bulletpoints' then
               list = p.get_item_nav_bulletpoints(slot, min_souls, max_souls, debug_mode, "exclude_street_brawl")
           else
               return "'type' should be get_item_nav_cards or get_item_nav_bulletpoints"
           end
           
           if list and list ~= "" then
               template_args["group" .. group_index] = frame:expandTemplate{title="Souls", args={[1] = min_souls, ["Shadow"] = soul_style}}
               template_args["list" .. group_index] = list
               group_index = group_index + 1
           end
       end
   end
   
   local sb_list
   if type_func == 'get_item_nav_cards' then
       sb_list = p.get_item_nav_cards(slot, 0, 999999, debug_mode, "street_brawl_only")
   elseif type_func == 'get_item_nav_bulletpoints' then
       sb_list = p.get_item_nav_bulletpoints(slot, 0, 999999, debug_mode, "street_brawl_only")
   end
   
   if sb_list and sb_list ~= "" then
       local sb_style = "color:#98ffde; font-family:'Retail Demo', sans-serif; text-wrap: nowrap; text-shadow: 0px 0px 2px #2c312e, 1.3px 1.3px rgba(0, 0, 0, 0.2);padding:5px;"
       local sb_link_text = "" .. street_brawl_label .. ""
       template_args["group" .. group_index] = frame:preprocess(sb_link_text)
       template_args["list" .. group_index] = sb_list
   end
   
   return frame:expandTemplate{title="Navbox subgroup", args=template_args}

end

-- for Template:Active Items function p.generate_active_items_table(frame)

   local active_items = {}
   
   for item_key, item_data in pairs(items_data) do
       local cost = tonumber(item_data["Cost"])
       if item_data["Name"] and 
          item_data["IsDisabled"] == false and 
          cost ~= nil and cost > 0 and 
          item_data["Activation"] and 
          item_data["Activation"] ~= "Passive" then
           
           table.insert(active_items, {
               name = item_data["Name"],
               key = item_key,
               cost = cost,
               slot = item_data["Slot"] or "Weapon",
               has_active_desc = item_data["ActiveDescription"] ~= nil
           })
       end
   end
   
   table.sort(active_items, function(a, b) return a.name < b.name end)
   
   -- Optimized string compilation using a structural buffer table
   local rows = {}
   table.insert(rows, [=[{| class="wikitable mw-collapsible sortable" style="width:100%" 

|+Active Items ! colspan="2" | Name ! Type ! SoulsCost ! Active ! Cooldown]=])

   for _, item in ipairs(active_items) do
       local bg_color, item_type
       if item.slot == "Armor" then
           bg_color = "#86C921"
           item_type = "Armor"
       elseif item.slot == "Tech" then
           bg_color = "#DE9CFF"
           item_type = "Tech"
       else
           bg_color = "#FCAC4D"
           item_type = "Weapon"
       end
       
       local row = string.format([=[

|- ! style="background-color:%s" | File:%s.png ! [[%s|]] | style="text-align:center;font-weight:bold" | | data-sort-value="%d" style="text-align:center;font-size:15px" | Souls%d | | style="text-align:center;font-size:16px" | Item Not Found.s]=],

           bg_color, 
           item.name, 
           item.name, item.key,
           item_type, item_type,
           item.cost, item.cost,
           item.key, item.name, item.key, item.name, item.key, item.name,
           item.name)
           
       table.insert(rows, row)
   end
   
   table.insert(rows, "\n|}")
   return frame:preprocess(table.concat(rows, "\n"))

end

return p