Module:Sandbox/LVL

Revision as of 18:46, 24 February 2026 by LVL (talk | contribs) (Trying to add links to pages)

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

local p = {}
local lang = require "Module:Lang"
local util_module = require "Module:Utilities"
local data = mw.loadJsonData("Data:Monster_Domosed/ItemData.json")
local stat_links = mw.loadJsonData("Data:Sandbox.json")

-- Get raw value, CSS class, and LocTokenOverride for a property
local function get_raw_value(item_name, prop)
    local item = data[item_name] or nil
    if not item then
        for k,v in pairs(data) do
            if v.Name == item_name then item = v; break end
        end
    end
    if not item then return nil, nil, nil end

    local raw_prop = item[prop]
    if type(raw_prop) == "table" then
        local loc_override = raw_prop.LocTokenOverride or prop
        return raw_prop.Value, raw_prop.CSSClass, loc_override
    else
        return raw_prop, nil, prop
    end
end

-- Check if property exists in language data
local function is_property_in_lang_data(prop)
    local suffixes = {"_label", "_Label", "_prefix", "_Prefix", "_postfix", "_Postfix", "_postvalue_label", "_Postvalue_Label"}
    for _, suffix in ipairs(suffixes) do
        if lang.get_string(prop .. suffix) then
            return true
        end
    end
    return false
end

-- Get item by name, ignoring disabled if possible
local function get_json_item(name)
    for _, v in pairs(data) do
        if v.Name == name and (v.IsDisabled == false or v.IsDisabled == nil) then
            return v
        end
    end
    for _, v in pairs(data) do
        if v.Name == name then return v end
    end
    return nil
end

-- Helper function: get first non-nil string from variants
local function first_non_nil(...)
    for i = 1, select("#", ...) do
        local v = select(i, ...)
        if v and v ~= "" then return v end
    end
    return ""
end

-- Helper: get lang string with case fallback
local function get_lang_with_fallback(base_key, suffix1, suffix2)
    return first_non_nil(
        lang.get_string(base_key .. suffix1),
        lang.get_string(base_key .. suffix2)
    )
end

-- Enhanced stat wrapper - prefix and value get enhanced styling, postfix/label don't
local function format_stat_enhanced(item_name, prop, skip_label)
	skip_label = skip_label or false
	local value, css, loc_key = get_raw_value(item_name, prop)
	if value == nil or value == 0 or value == "" then
		return nil
	end

	local numeric_val = tonumber(tostring(value):match("^-?%d+%.?%d*")) or 0
	if numeric_val == 0 then
		return nil
	end

	local label_key = loc_key:gsub("^#", "")

	if not (
		lang.get_string(label_key.."_label") or
		lang.get_string(label_key.."_Label") or
		lang.get_string(label_key.."_prefix") or
		lang.get_string(label_key.."_Prefix") or
		lang.get_string(label_key.."_postfix") or
		lang.get_string(label_key.."_Postfix") or
		lang.get_string(label_key.."_postvalue_label") or
		lang.get_string(label_key.."_Postvalue_Label")
	) then
		return nil
	end

	if prop == "BuildUpPerShot" then label_key = "BuildupPerShot"
	elseif prop == "DotDuration" then label_key = "DOTDuration"
	end

	local label = skip_label and "" or get_lang_with_fallback(label_key, "_label", "_Label")
	local prefix = get_lang_with_fallback(label_key, "_prefix", "_Prefix")
	local postfix = get_lang_with_fallback(label_key, "_postfix", "_Postfix")

	if prefix == "" and postfix == "" and label == "" then
		return nil
	end

	--------------------------------------------------
	-- VALUE (Enhanced) with PREFIX and POSTFIX/SUFFIX
	--------------------------------------------------
	local val_str
	local suffix = postfix
	if css == "move_speed" or css == "sprint_speed" then
		if not postfix:find("%%") then
			suffix = postfix .. "/s"
		end
	end

	if prefix ~= "" then
		if prefix == "{s:sign}" then
			local num = tonumber(tostring(value):match("^-?%d+%.?%d*"))
			if num and num >= 0 then
				prefix = "+"
			else
				prefix = ""
			end
		end
		if suffix ~= "" and suffix ~= "%" then
			val_str = string.format(
				"{{#invoke:Enhanced|render|%s|%s|prefix=%s|suffix=%s|noicon=true}}",
				item_name,
				prop,
				prefix,
				suffix
			)
		else
			val_str = string.format(
				"{{#invoke:Enhanced|render|%s|%s|prefix=%s|noicon=true}}",
				item_name,
				prop,
				prefix
			)
		end
	else
		if suffix ~= "" and suffix ~= "%" then
			val_str = string.format(
				"{{#invoke:Enhanced|render|%s|%s|suffix=%s|noicon=true}}",
				item_name,
				prop,
				suffix
			)
		else
			val_str = string.format(
				"{{#invoke:Enhanced|render|%s|%s|noicon=true}}",
				item_name,
				prop
			)
		end
	end

	--------------------------------------------------
	-- Postfix (UNSTYLED)
	--------------------------------------------------
	if postfix ~= "" and postfix ~= "%" and not val_str:find(postfix, 1, true) then
		if postfix == "HP/s" then
			val_str = val_str .. " " .. postfix
		else
			val_str = val_str .. postfix
		end
	end

	--------------------------------------------------
	-- Label (UNSTYLED - only if not skipped)
	--------------------------------------------------
	if label ~= "" then
	    local link_page = stat_links[prop]
	    if link_page then
	        if link_page == label then
	            val_str = val_str .. " [[" .. label .. "]]"
	        else
	            val_str = val_str .. " [[" .. link_page .. "|" .. label .. "]]"
	        end
	    else
	        val_str = val_str .. " " .. label
	    end
	end

	--------------------------------------------------
	-- Conditional marker
	--------------------------------------------------
	local item = get_json_item(item_name)
	local raw_prop = item[prop]

	if type(raw_prop) == "table"
		and (raw_prop.UsageFlags == "ConditionallyApplied"
			or raw_prop.UsageFlags == "ConditionallyEnemyApplied")
	then
		local conditional_text = lang.get_string("Citadel_Shop_ConditionalAttribute")
		val_str = val_str .. string.format(
			' <span style="color:#C0C0C0">(%s)</span>',
			conditional_text
		)
	end

	return val_str
end

-- Special formatting for StatusEffect properties (enhanced variant)
local function format_property_for_infobox_enhanced(item_name, prop)
    if prop:match("^StatusEffect") then
        return string.format(
            "[[{{#invoke:Lang|get_string|Citadel_%s}}]] [[{{#invoke:Lang|get_string|Citadel_StatusEffect}}]]",
            prop
        )
    else
        return format_stat_enhanced(item_name, prop)
    end
end

-- Returns items that build from this item, sorted by tier
local function get_component_of_sorted(item_input)
    local target_key = nil
    if data[item_input] then
        target_key = item_input
    else
        for k, v in pairs(data) do
            if v.Name == item_input and (v.IsDisabled == false or v.IsDisabled == nil) then
                target_key = k; break
            end
        end
    end
    if not target_key then return {} end

    local parents = {}
    for key, item in pairs(data) do
        if item.IsDisabled ~= true and item.Components and type(item.Components) == "table" then
            for _, comp in ipairs(item.Components) do
                if comp == target_key then
                    table.insert(parents, key)
                    break
                end
            end
        end
    end

    table.sort(parents, function(a, b)
        local tier_a = tonumber(data[a] and data[a]["Tier"]) or 0
        local tier_b = tonumber(data[b] and data[b]["Tier"]) or 0
        return tier_a < tier_b
    end)

    return parents
end

-- Format stat value for Infobox, handling prefix/postfix, CSS, scaling, and conditional
-- allow_conditional should be true for ImportantProperties, false for regular Properties
local function format_stat(item_name, prop, skip_label, debug, allow_conditional, enhanced)
	if allow_conditional == nil then allow_conditional = true end

	if prop == "AbilityCooldown" and not skip_label then
		return nil
	end

	local value, css, loc_key = get_raw_value(item_name, prop)
	if value == nil or value == 0 or value == "" then
		return nil
	end

	local numeric_val = tonumber(tostring(value):match("^-?%d+%.?%d*")) or 0
	if numeric_val == 0 then
		return nil
	end

	local label_key = loc_key:gsub("^#", "")

	if not (
		lang.get_string(label_key.."_label") or
		lang.get_string(label_key.."_Label") or
		lang.get_string(label_key.."_prefix") or
		lang.get_string(label_key.."_Prefix") or
		lang.get_string(label_key.."_postfix") or
		lang.get_string(label_key.."_Postfix") or
		lang.get_string(label_key.."_postvalue_label") or
		lang.get_string(label_key.."_Postvalue_Label")
	) then
		return nil
	end

	if prop == "BuildUpPerShot" then label_key = "BuildupPerShot"
	elseif prop == "DotDuration" then label_key = "DOTDuration"
	end

	local label = skip_label and "" or get_lang_with_fallback(label_key, "_label", "_Label")
	local prefix = get_lang_with_fallback(label_key, "_prefix", "_Prefix")
	local postfix = get_lang_with_fallback(label_key, "_postfix", "_Postfix")

	if prefix == "" and postfix == "" and label == "" then
		return nil
	end

	--------------------------------------------------
	-- VALUE (normal or enhanced)
	--------------------------------------------------
	local val_str
	if enhanced then
		val_str = string.format(
			"{{#invoke:Enhanced|render|%s|%s}}",
			item_name,
			prop
		)
	else
		val_str = tostring(value)
	end

	--------------------------------------------------
	-- Prefix (UNSTYLED - skipped when enhanced, handled internally by Enhanced module)
	--------------------------------------------------
	if not enhanced then
		if prefix ~= "" then
			if prefix == "{s:sign}" then
				local num = tonumber(val_str:match("^-?%d+%.?%d*"))
				if num and num >= 0 and not val_str:find("^%+") then
					val_str = "+" .. val_str
				end
			else
				val_str = prefix .. val_str
			end
		end
	end

	--------------------------------------------------
	-- Postfix (UNSTYLED - skipped when enhanced, handled internally by Enhanced module)
	--------------------------------------------------
	if not enhanced then
		if postfix ~= "" and not val_str:find(postfix, 1, true) then
			if postfix == "HP/s" then
				val_str = val_str .. " " .. postfix
			else
				val_str = val_str .. postfix
			end
		end
	end

	--------------------------------------------------
	-- Move speed/Sprint speed postfix (skipped when enhanced)
	--------------------------------------------------
	if not enhanced then
		if css == "move_speed" or css == "sprint_speed" then
			if postfix == "" or not postfix:find("%%") then
				val_str = val_str:gsub("/s", "") .. "/s"
			end
			local num = tonumber(val_str:match("^-?%d+%.?%d*"))
			if num and num > 0 and not val_str:find("^%+") then
				val_str = "+" .. val_str
			end
		end
	end

	--------------------------------------------------
	-- Append scaling template {{Boon}} or {{Ss}}
	--------------------------------------------------
	local item = get_json_item(item_name)
	local raw_prop = item[prop]
	if type(raw_prop) == "table" and raw_prop.Scale and raw_prop.Scale.Value ~= 0 then
		local scale_val = util_module.round_to_sig_fig(raw_prop.Scale.Value, 2)
		local template_name = (raw_prop.Scale.Type == "power_increase") and "Boon" or "Ss"
		val_str = val_str .. " " .. mw.getCurrentFrame():expandTemplate{ title = template_name, args = { scale_val } }
	end

	--------------------------------------------------
	-- Label (UNSTYLED)
	--------------------------------------------------
	if label ~= "" then
	    local link_page = stat_links[prop]
	    if link_page then
	        if link_page == label then
	            val_str = val_str .. " [[" .. label .. "]]"
	        else
	            val_str = val_str .. " [[" .. link_page .. "|" .. label .. "]]"
	        end
	    else
	        val_str = val_str .. " " .. label
	    end
	end

	--------------------------------------------------
	-- Conditional marker
	--------------------------------------------------
	if allow_conditional
		and type(raw_prop) == "table"
		and (raw_prop.UsageFlags == "ConditionallyApplied"
			or raw_prop.UsageFlags == "ConditionallyEnemyApplied")
	then
		local conditional_text = lang.get_string("Citadel_Shop_ConditionalAttribute")
		val_str = val_str .. string.format(
			' <span style="color:#C0C0C0">(%s)</span>',
			conditional_text
		)
	end

	return val_str
end

-- Special formatting for StatusEffect properties
local function format_property_for_infobox(item_name, prop, skip_label, debug)
    if prop:match("^StatusEffect") then
        return string.format(
            "[[{{#invoke:Lang|get_string|Citadel_%s}}]] [[{{#invoke:Lang|get_string|Citadel_StatusEffect}}]]",
            prop
        )
    else
        return format_stat(item_name, prop, skip_label, debug, true)
    end
end

-- Build raw wikitext for the default tooltip infobox (no preprocess)
local function build_tooltip(frame)
    local item_name = frame.args[1]
    if not item_name then return "Item name missing", 0 end

    local debug = tonumber(frame.args.debug) or 0
    local item = get_json_item(item_name)
    if not item then return "Item not found", debug end

    local ItemData = require("Module:ItemData")
    local lines = {
        "{{Infobox item",
        "| item_name = " .. item_name
    }

    if item.StreetBrawl == true then
        table.insert(lines, "| street_brawl = true")
    end

    ----------------------------------------------------------------------
    -- Check if AbilityCooldown or AbilityChargeUpTime exists in sections
    ----------------------------------------------------------------------
    local cooldown_found_in_sections = false
    local chargeup_found_in_sections = false
    for _, section in ipairs(item.TooltipSections or {}) do
        for _, entry in ipairs(section.Entries or {}) do
            for _, prop in ipairs(entry.Properties or {}) do
                if prop == "AbilityCooldown" then cooldown_found_in_sections = true end
                if prop == "AbilityChargeUpTime" then chargeup_found_in_sections = true end
            end
            for _, prop in ipairs(entry.ImportantProperties or {}) do
                if prop == "AbilityCooldown" then cooldown_found_in_sections = true end
                if prop == "AbilityChargeUpTime" then chargeup_found_in_sections = true end
            end
        end
    end

    ----------------------------------------------------------------------
    -- Innate stats
    ----------------------------------------------------------------------
    local innate_index = 1
    for _, section in ipairs(item.TooltipSections or {}) do
        if section.Type == "EArea_Innate" then
            for _, entry in ipairs(section.Entries or {}) do
                for _, prop in ipairs(entry.Properties or {}) do
                    if prop ~= "AbilityCooldown" and prop ~= "AbilityChargeUpTime" then
                        local val = format_stat(item_name, prop, false, debug, false)
                        if val then
                            table.insert(lines, string.format("| item_stat%d = %s", innate_index, val))
                            innate_index = innate_index + 1
                        end
                    end
                end
            end
        end
    end

    ----------------------------------------------------------------------
    -- Passive stats
    ----------------------------------------------------------------------
    local passive_counter = 1
    for _, section in ipairs(item.TooltipSections or {}) do
        if section.Type ~= "EArea_Innate" and section.Type ~= "EArea_Active" then
            for _, entry in ipairs(section.Entries or {}) do
                local loc = entry.LocString and entry.LocString:gsub("#","") or nil
                if loc then
                    table.insert(lines, string.format(
                        "| passive%d_description = {{#invoke:Lang|get_string|%s|item_name=%s}}",
                        passive_counter, loc, item_name
                    ))
                end

                local stat_index = 1
                local seen_props = {}

                for _, prop in ipairs(entry.ImportantProperties or {}) do
                    if prop ~= "AbilityCooldown" and prop ~= "AbilityChargeUpTime"
                       and is_property_in_lang_data(prop) and not seen_props[prop] then
                        local val = format_stat(item_name, prop, false, debug, true)
                        if val then
                            table.insert(lines, string.format("| passive%d_stat%d = %s", passive_counter, stat_index, val))
                            stat_index = stat_index + 1
                            seen_props[prop] = true
                        end
                    end
                end

                for _, prop in ipairs(entry.Properties or {}) do
                    if prop ~= "AbilityCooldown" and prop ~= "AbilityChargeUpTime"
                       and is_property_in_lang_data(prop) and not seen_props[prop] then
                        local val = format_stat(item_name, prop, false, debug, false)
                        if val then
                            table.insert(lines, string.format("| passive%d_stat%d = %s", passive_counter, stat_index, val))
                            stat_index = stat_index + 1
                            seen_props[prop] = true
                        end
                    end
                end

                for _, prop_list in ipairs({entry.Properties or {}, entry.ImportantProperties or {}}) do
                    for _, prop in ipairs(prop_list) do
                        if prop == "AbilityCooldown" then
                            local cd = format_stat(item_name, prop, true, debug)
                            if cd then table.insert(lines, string.format("| passive%d_cooldown = %s", passive_counter, cd)) end
                        end
                        if prop == "AbilityChargeUpTime" then
                            local cu = format_stat(item_name, prop, true, debug)
                            if cu then table.insert(lines, string.format("| passive%d_chargeup = %s", passive_counter, cu)) end
                        end
                    end
                end

                passive_counter = passive_counter + 1
            end
        end
    end

    ----------------------------------------------------------------------
    -- Active stats
    ----------------------------------------------------------------------
    local active_index = 1
    for _, section in ipairs(item.TooltipSections or {}) do
        if section.Type == "EArea_Active" then
            for _, entry in ipairs(section.Entries or {}) do
                if entry.LocString then
                    table.insert(lines, string.format(
                        "| active%d_description = {{#invoke:Lang|get_string|%s|item_name=%s}}",
                        active_index, entry.LocString:gsub("#",""), item_name
                    ))
                end

                local stat_index = 1
                local seen_props = {}

                for _, prop in ipairs(entry.ImportantProperties or {}) do
                    if prop ~= "AbilityCooldown" and prop ~= "AbilityChargeUpTime"
                       and is_property_in_lang_data(prop) and not seen_props[prop] then
                        local val = format_stat(item_name, prop, false, debug, true)
                        if val then
                            table.insert(lines, string.format("| active%d_stat%d = %s", active_index, stat_index, val))
                            stat_index = stat_index + 1
                            seen_props[prop] = true
                        end
                    end
                end

                for _, prop in ipairs(entry.Properties or {}) do
                    if prop ~= "AbilityCooldown" and prop ~= "AbilityChargeUpTime"
                       and is_property_in_lang_data(prop) and not seen_props[prop] then
                        local val = format_stat(item_name, prop, false, debug, false)
                        if val then
                            table.insert(lines, string.format("| active%d_stat%d = %s", active_index, stat_index, val))
                            stat_index = stat_index + 1
                            seen_props[prop] = true
                        end
                    end
                end

                for _, prop_list in ipairs({entry.Properties or {}, entry.ImportantProperties or {}}) do
                    for _, prop in ipairs(prop_list) do
                        if prop == "AbilityCooldown" then
                            local cd = format_stat(item_name, prop, true, debug)
                            if cd then table.insert(lines, string.format("| active%d_cooldown = %s", active_index, cd)) end
                        end
                        if prop == "AbilityChargeUpTime" then
                            local cu = format_stat(item_name, prop, true, debug)
                            if cu then table.insert(lines, string.format("| active%d_chargeup = %s", active_index, cu)) end
                        end
                    end
                end

                active_index = active_index + 1
            end
        end
    end

    ----------------------------------------------------------------------
    -- Global fallback for AbilityCooldown and AbilityChargeUpTime
    -- Attaches to the active section if one exists, otherwise innate
    ----------------------------------------------------------------------
    if not cooldown_found_in_sections and not chargeup_found_in_sections and item.AbilityCooldown then
        local cd = format_stat(item_name, "AbilityCooldown", true, debug)
        if cd then
            local has_active = false
            for _, section in ipairs(item.TooltipSections or {}) do
                if section.Type == "EArea_Active" then has_active = true; break end
            end
            if has_active then
                table.insert(lines, "| active1_cooldown = " .. cd)
            else
                table.insert(lines, "| item_stat_cooldown = " .. cd)
            end
        end
    end

    if not chargeup_found_in_sections and item.AbilityChargeUpTime then
        local cu = format_stat(item_name, "AbilityChargeUpTime", true, debug)
        if cu then table.insert(lines, "| item_stat_chargeup = " .. cu) end
    end

    ----------------------------------------------------------------------
    -- Components
    ----------------------------------------------------------------------
    for i = 1, 2 do
        local comp = ItemData.get_component_name { args = { item_name, i } }
        if comp and comp ~= "" then
            table.insert(lines, string.format("| component%d_name = %s", i, comp))
        end
    end

    ----------------------------------------------------------------------
    -- Is Component Of (sorted by tier)
    ----------------------------------------------------------------------
    local parents = get_component_of_sorted(item_name)
    for i, key in ipairs(parents) do
        local parent_name = data[key] and data[key]["Name"] or ""
        if parent_name ~= "" then
            table.insert(lines, string.format("| iscomponentof%d_name = %s", i, parent_name))
        end
    end

    ----------------------------------------------------------------------
    -- Sounds
    ----------------------------------------------------------------------
    for key, value in pairs(frame.args) do
        local index = tostring(key):match("^sound(%d+)$")
        if index and value ~= "" then
            table.insert(lines, string.format("| sound%s = %s", index, value))
        end
    end

    ----------------------------------------------------------------------
    table.insert(lines, "}}")
    return table.concat(lines, "\n"), debug
end

-- Build raw wikitext for the enhanced tooltip infobox (no preprocess)
local function build_tooltip_enhanced(frame)
    local item_name = frame.args[1]
    if not item_name then return "Item name missing" end

    local item = get_json_item(item_name)
    if not item then return "Item not found" end

    local ItemData = require("Module:ItemData")
    local lines = {
        "{{Infobox item",
        "| item_name = " .. item_name
    }

    if item.StreetBrawl == true then
        table.insert(lines, "| street_brawl = true")
    end

    ----------------------------------------------------------------------
    -- Check if AbilityCooldown or AbilityChargeUpTime exists in sections
    ----------------------------------------------------------------------
    local cooldown_found_in_sections = false
    local chargeup_found_in_sections = false
    for _, section in ipairs(item.TooltipSections or {}) do
        for _, entry in ipairs(section.Entries or {}) do
            for _, prop in ipairs(entry.Properties or {}) do
                if prop == "AbilityCooldown" then cooldown_found_in_sections = true end
                if prop == "AbilityChargeUpTime" then chargeup_found_in_sections = true end
            end
            for _, prop in ipairs(entry.ImportantProperties or {}) do
                if prop == "AbilityCooldown" then cooldown_found_in_sections = true end
                if prop == "AbilityChargeUpTime" then chargeup_found_in_sections = true end
            end
        end
    end

    ----------------------------------------------------------------------
    -- Innate stats (Enhanced)
    ----------------------------------------------------------------------
    local innate_index = 1
    for _, section in ipairs(item.TooltipSections or {}) do
        if section.Type == "EArea_Innate" then
            for _, entry in ipairs(section.Entries or {}) do
                for _, prop in ipairs(entry.Properties or {}) do
                    if prop ~= "AbilityCooldown" and prop ~= "AbilityChargeUpTime"
                       and is_property_in_lang_data(prop) then
                        local val = format_stat_enhanced(item_name, prop)
                        if val then
                            table.insert(
                                lines,
                                string.format(
                                    "| item_stat%d = %s",
                                    innate_index,
                                    val
                                )
                            )
                            innate_index = innate_index + 1
                        end
                    end
                end
            end
        end
    end

    ----------------------------------------------------------------------
    -- Passive stats (Enhanced)
    ----------------------------------------------------------------------
    local passive_counter = 1
    for _, section in ipairs(item.TooltipSections or {}) do
        if section.Type ~= "EArea_Innate" and section.Type ~= "EArea_Active" then
            for _, entry in ipairs(section.Entries or {}) do
                if entry.LocString then
                    table.insert(lines, string.format(
                        "| passive%d_description = {{#invoke:Lang|get_string|%s|item_name=%s}}",
                        passive_counter,
                        entry.LocString:gsub("#",""),
                        item_name
                    ))
                end

                local stat_index = 1
                local seen = {}

                for _, prop in ipairs(entry.ImportantProperties or {}) do
                    if prop ~= "AbilityCooldown" and prop ~= "AbilityChargeUpTime"
                       and is_property_in_lang_data(prop) and not seen[prop] then
                        local val = format_property_for_infobox_enhanced(item_name, prop)
                        if val then
                            table.insert(
                                lines,
                                string.format(
                                    "| passive%d_stat%d = %s",
                                    passive_counter,
                                    stat_index,
                                    val
                                )
                            )
                            stat_index = stat_index + 1
                            seen[prop] = true
                        end
                    end
                end

                for _, prop in ipairs(entry.Properties or {}) do
                    if prop ~= "AbilityCooldown" and prop ~= "AbilityChargeUpTime"
                       and is_property_in_lang_data(prop) and not seen[prop] then
                        local val = format_stat_enhanced(item_name, prop)
                        if val then
                            table.insert(
                                lines,
                                string.format(
                                    "| passive%d_stat%d = %s",
                                    passive_counter,
                                    stat_index,
                                    val
                                )
                            )
                            stat_index = stat_index + 1
                            seen[prop] = true
                        end
                    end
                end

                for _, prop_list in ipairs({entry.Properties or {}, entry.ImportantProperties or {}}) do
                    for _, prop in ipairs(prop_list) do
                        if prop == "AbilityCooldown" then
                            local cd = format_stat_enhanced(item_name, prop, true)
                            if cd then
                                table.insert(lines, string.format("| passive%d_cooldown = %s", passive_counter, cd))
                            end
                        end
                        if prop == "AbilityChargeUpTime" then
                            local cu = format_stat_enhanced(item_name, prop, true)
                            if cu then
                                table.insert(lines, string.format("| passive%d_chargeup = %s", passive_counter, cu))
                            end
                        end
                    end
                end

                passive_counter = passive_counter + 1
            end
        end
    end

    ----------------------------------------------------------------------
    -- Active stats (Enhanced)
    ----------------------------------------------------------------------
    local active_index = 1
    for _, section in ipairs(item.TooltipSections or {}) do
        if section.Type == "EArea_Active" then
            for _, entry in ipairs(section.Entries or {}) do
                if entry.LocString then
                    table.insert(lines, string.format(
                        "| active%d_description = {{#invoke:Lang|get_string|%s|item_name=%s}}",
                        active_index,
                        entry.LocString:gsub("#",""),
                        item_name
                    ))
                end

                local stat_index = 1
                local seen = {}

                for _, prop in ipairs(entry.ImportantProperties or {}) do
                    if prop ~= "AbilityCooldown" and prop ~= "AbilityChargeUpTime"
                       and is_property_in_lang_data(prop) and not seen[prop] then
                        local val = format_property_for_infobox_enhanced(item_name, prop)
                        if val then
                            table.insert(
                                lines,
                                string.format(
                                    "| active%d_stat%d = %s",
                                    active_index,
                                    stat_index,
                                    val
                                )
                            )
                            stat_index = stat_index + 1
                            seen[prop] = true
                        end
                    end
                end

                for _, prop in ipairs(entry.Properties or {}) do
                    if prop ~= "AbilityCooldown" and prop ~= "AbilityChargeUpTime"
                       and is_property_in_lang_data(prop) and not seen[prop] then
                        local val = format_stat_enhanced(item_name, prop)
                        if val then
                            table.insert(
                                lines,
                                string.format(
                                    "| active%d_stat%d = %s",
                                    active_index,
                                    stat_index,
                                    val
                                )
                            )
                            stat_index = stat_index + 1
                            seen[prop] = true
                        end
                    end
                end

                for _, prop_list in ipairs({entry.Properties or {}, entry.ImportantProperties or {}}) do
                    for _, prop in ipairs(prop_list) do
                        if prop == "AbilityCooldown" then
                            local cd = format_stat_enhanced(item_name, prop, true)
                            if cd then
                                table.insert(lines, string.format("| active%d_cooldown = %s", active_index, cd))
                            end
                        end
                        if prop == "AbilityChargeUpTime" then
                            local cu = format_stat_enhanced(item_name, prop, true)
                            if cu then
                                table.insert(lines, string.format("| active%d_chargeup = %s", active_index, cu))
                            end
                        end
                    end
                end

                active_index = active_index + 1
            end
        end
    end

    ----------------------------------------------------------------------
    -- Global fallback for AbilityCooldown and AbilityChargeUpTime
    -- Attaches to the active section if one exists, otherwise innate
    ----------------------------------------------------------------------
    if not cooldown_found_in_sections and not chargeup_found_in_sections and item.AbilityCooldown then
        local cd = format_stat_enhanced(item_name, "AbilityCooldown", true)
        if cd then
            local has_active = false
            for _, section in ipairs(item.TooltipSections or {}) do
                if section.Type == "EArea_Active" then has_active = true; break end
            end
            if has_active then
                table.insert(lines, "| active1_cooldown = " .. cd)
            else
                table.insert(lines, "| item_stat_cooldown = " .. cd)
            end
        end
    end

    if not chargeup_found_in_sections and item.AbilityChargeUpTime then
        local cu = format_stat_enhanced(item_name, "AbilityChargeUpTime", true)
        if cu then table.insert(lines, "| item_stat_chargeup = " .. cu) end
    end

    ----------------------------------------------------------------------
    -- Components
    ----------------------------------------------------------------------
    for i = 1, 2 do
        local comp = ItemData.get_component_name { args = { item_name, i } }
        if comp and comp ~= "" then
            table.insert(lines, string.format("| component%d_name = %s", i, comp))
        end
    end

    ----------------------------------------------------------------------
    -- Is Component Of (sorted by tier)
    ----------------------------------------------------------------------
    local parents = get_component_of_sorted(item_name)
    for i, key in ipairs(parents) do
        local parent_name = data[key] and data[key]["Name"] or ""
        if parent_name ~= "" then
            table.insert(lines, string.format("| iscomponentof%d_name = %s", i, parent_name))
        end
    end

    ----------------------------------------------------------------------
    -- Sounds
    ----------------------------------------------------------------------
    for key, value in pairs(frame.args) do
        local index = tostring(key):match("^sound(%d+)$")
        if index and value ~= "" then
            table.insert(lines, string.format("| sound%s = %s", index, value))
        end
    end

    ----------------------------------------------------------------------
    table.insert(lines, "}}")
    return table.concat(lines, "\n")
end

function p.fill_tooltip(frame)
    local output, debug = build_tooltip(frame)
    if debug == 1 or debug == 2 then
        return output
    end
    return mw.getCurrentFrame():preprocess(output)
end

function p.fill_tooltip_enhanced(frame)
    return mw.getCurrentFrame():preprocess(build_tooltip_enhanced(frame))
end

function p.render(frame)
    local output_lines = { "<tabber>" }
    table.insert(output_lines, "Default=")
    local tooltip, _ = build_tooltip(frame)
    table.insert(output_lines, tooltip)
    table.insert(output_lines, "|-|")
    table.insert(output_lines, "Enhanced=")
    table.insert(output_lines, build_tooltip_enhanced(frame))
    table.insert(output_lines, "</tabber>")
    return mw.getCurrentFrame():preprocess(table.concat(output_lines, "\n"))
end

return p