Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Join the Playtest on Steam Now: SpiritVale

Module:GameSkills: Difference between revisions

From SpiritVale Wiki
No edit summary
No edit summary
 
(51 intermediate revisions by the same user not shown)
Line 1: Line 1:
--Module: GameSkills
-- Module:GameSkills
--
--
--Upgrades:
-- Phase 6.5+ (Plug-in Slot Architecture)
--  - Adds a per - skill Level Select slider(client - side JS updates fields)
--  - Default level = Max Level
--  - Adds.sv - skill - card + data - max - level / data - level hooks for JS
--  - Replaces large Lv1 / Lv2 /... lists with data - series dynamic spans
--  - Removes "General" + "Type" section bars and merges Level Select + Type into one unified top band
--(Level Select on the left; Type / Element / Target / Cast Type list on the right)
--  - NEW(list - mode): Wraps all skills in ONE wrapper panel and each skill in a stable.sv - skill - item
--so CSS dividers work reliably under Citizen / table wrappers.
--
--
--Requires the JS you installed in MediaWiki: Common.js.
-- Layout:
--Uses Common.css classes:
--  Row 1: Slot 1 + Slot 2 (Icon + SkillType)
--  .sv - topband - cell, .sv - topband - table, .sv - type - table, .sv - level - ui, .sv - level - title, .sv - level - label, .sv - level - slider
--  Row 2: Slot 3 + Slot 4 (Description + Placeholder)
--  .sv - skill - collection, .sv - skill - item
--   Row 3: Slot 5 + Slot 6 (SourceType + QuickStats)
--  Row 4: Slot 7 + Slot 8 (SpecialMechanics + LevelSelector)
--
-- Requires Common.js:
--   - updates .sv-dyn spans via data-series
--   - updates .sv-level-num + data-level on .sv-skill-card
--  - binds to input.sv-level-range inside each card


local GameData = require("Module:GameData")
local GameData = require("Module:GameData")
Line 21: Line 19:


----------------------------------------------------------------------
----------------------------------------------------------------------
--Internal helpers
-- Data cache
----------------------------------------------------------------------
----------------------------------------------------------------------


local skillsCache
local skillsCache
local eventsCache


-- getSkills: lazy-load + cache skill dataset from GameData.
local function getSkills()
local function getSkills()
if not skillsCache then
        if not skillsCache then
skillsCache = GameData.loadSkills()
                skillsCache = GameData.loadSkills()
        end
        return skillsCache
end
end
return skillsCache
 
local function getEvents()
        if eventsCache == nil then
                if type(GameData.loadEvents) == "function" then
                        eventsCache = GameData.loadEvents()
                else
                        eventsCache = false
                end
        end
 
        return eventsCache
end
end


----------------------------------------------------------------------
-- Small utilities
----------------------------------------------------------------------
-- getArgs: read args from parent frame when invoked from a template.
local function getArgs(frame)
local function getArgs(frame)
local parent = frame: getParent()
local parent = frame:getParent()
if parent then
return (parent and parent.args) or frame.args
return parent.args
end
end
return frame.args
 
-- trim: normalize strings (nil if empty).
local function trim(s)
if type(s) ~= "string" then
return nil
end
s = mw.text.trim(s)
return (s ~= "" and s) or nil
end
end


local function listToText(list, sep)
-- toNum: convert common scalar/table forms to a Lua number.
if type(list) ~= "table" or #list == 0 then
local function toNum(v)
return nil
if type(v) == "number" then
return v
end
if type(v) == "string" then
return tonumber(v)
end
if type(v) == "table" and v.Value ~= nil then
return toNum(v.Value)
end
return nil
end
end
return table.concat(list, sep or ", ")
 
-- clamp: clamp a number into [lo, hi].
local function clamp(n, lo, hi)
if type(n) ~= "number" then
return lo
end
if n < lo then return lo end
if n > hi then return hi end
return n
end
end


local function addRow(tbl, label, value)
-- fmtNum: consistent number formatting (trim trailing zeros).
if value == nil or value == "" then
local function fmtNum(n)
return
if type(n) ~= "number" then
return (n ~= nil) and tostring(n) or nil
end
 
if math.abs(n - math.floor(n)) < 1e-9 then
return tostring(math.floor(n))
end
 
local s = string.format("%.4f", n)
s = mw.ustring.gsub(s, "0+$", "")
s = mw.ustring.gsub(s, "%.$", "")
return s
end
end
local row = tbl: tag("tr")
 
row: tag("th"): wikitext(label): done()
-- listToText: join an array into a readable string.
row: tag("td"): wikitext(value): done()
local function listToText(list, sep)
        if type(list) ~= "table" or #list == 0 then
                return nil
        end
        return table.concat(list, sep or ", ")
end
end


local function addSectionHeader(tbl, label)
local function resolveDisplayName(v, kind)
local row = tbl: tag("tr")
        if v == nil then return nil end
local cell = row: tag("th")
 
cell: attr("colspan", 2)
        local function firstString(keys, source)
cell: addClass("spiritvale-infobox-section-header")
                for _, key in ipairs(keys) do
cell: wikitext(label)
                        local candidate = source[key]
                        if type(candidate) == "string" and candidate ~= "" then
                                return candidate
                        end
                end
                return nil
        end
 
        if type(v) == "table" then
                local primaryKeys = { "External Name", "Display Name", "Name" }
                local extendedKeys = { "Skill External Name", "Status External Name" }
                local internalKeys = { "Internal Name", "Internal ID", "ID", "InternalID", "Skill Internal Name", "InternalID" }
 
                return firstString(primaryKeys, v)
                        or firstString(extendedKeys, v)
                        or firstString(internalKeys, v)
        end
 
        if type(v) == "string" then
                if kind == "event" then
                        local events = getEvents()
                        if events and events.byId and events.byId[v] then
                                local mapped = resolveDisplayName(events.byId[v], "event")
                                if mapped then
                                        return mapped
                                end
                        end
                end
 
                return v
        end
 
        return tostring(v)
end
end


local function trim(s)
local function resolveEventName(v)
if type(s) ~= "string" then return nil end
        local resolved = resolveDisplayName(v, "event")
s = mw.text.trim(s)
        if type(resolved) == "string" then
if s == "" then return nil end
                return resolved
return s
        end
        return (resolved ~= nil) and tostring(resolved) or nil
end
end


local function toNum(v)
local function resolveSkillNameFromEvent(ev)
if type(v) == "number" then return v end
        if type(ev) ~= "table" then
if type(v) == "string" then return tonumber(v) end
                return resolveDisplayName(ev, "skill") or "Unknown skill"
if type(v) == "table" and v.Value ~= nil then
        end
return toNum(v.Value)
 
        local displayKeys = {
                "Skill External Name",
                "External Name",
                "Display Name",
                "Name",
                "Skill Name",
        }
 
        for _, key in ipairs(displayKeys) do
                local candidate = resolveDisplayName(ev[key], "skill")
                if candidate then
                        return candidate
                end
        end
 
        local internalKeys = {
                "Skill Internal Name",
                "Skill ID",
                "Internal Name",
                "Internal ID",
                "ID",
        }
 
        for _, key in ipairs(internalKeys) do
                local candidate = ev[key]
                if type(candidate) == "string" and candidate ~= "" then
                        return candidate
                end
        end
 
        return "Unknown skill"
end
end
return nil
 
-- isNoneLike: treat common "none" spellings as empty.
local function isNoneLike(v)
if v == nil then return true end
local s = mw.text.trim(tostring(v))
if s == "" then return true end
s = mw.ustring.lower(s)
return (s == "none" or s == "no" or s == "n/a" or s == "na" or s == "null")
end
end


local function clamp(n, lo, hi)
-- addRow: add a standard <tr><th>Label</th><td>Value</td></tr> row.
if type(n) ~= "number" then return lo end
local function addRow(tbl, label, value, rowClass, dataKey)
if n < lo then return lo end
if value == nil or value == "" then
if n > hi then return hi end
return
return n
end
end
 
local row = tbl:tag("tr")
row:addClass("sv-row")
if rowClass then row:addClass(rowClass) end
if dataKey then row:attr("data-field", dataKey) end


local function fmtNum(n)
row:tag("th"):wikitext(label):done()
if type(n) ~= "number" then
row:tag("td"):wikitext(value):done()
return (n ~= nil) and tostring(n) or nil
end
if math.abs(n - math.floor(n)) < 1e-9 then
return tostring(math.floor(n))
end
local s = string.format("%.4f", n)
s = mw.ustring.gsub(s, "0+$", "")
s = mw.ustring.gsub(s, "%.$", "")
return s
end
end


--Handles either a scalar OR { Value =..., Unit =... }
-- formatUnitValue: format {Value, Unit} blocks (or scalar) for display.
--IMPORTANT: do NOT convert percent_decimal / percent_whole; just format around stored values.
local function formatUnitValue(v)
local function formatUnitValue(v)
if type(v) == "table" and v.Value ~= nil then
if type(v) == "table" and v.Value ~= nil then
local unit = v.Unit
local unit = v.Unit
local val = v.Value
local val = v.Value


if unit == "percent_decimal" or unit == "percent_whole" or unit == "percent" then
if unit == "percent_decimal" or unit == "percent_whole" or unit == "percent" then
return tostring(val).. "%"
return tostring(val) .. "%"
elseif unit == "seconds" then
elseif unit == "seconds" then
return tostring(val).. "s"
return tostring(val) .. "s"
elseif unit == "meters" then
elseif unit == "meters" then
return tostring(val).. "m"
return tostring(val) .. "m"
elseif unit == "tiles" then
elseif unit == "tiles" then
return tostring(val).. " tiles"
return tostring(val) .. " tiles"
elseif unit and unit ~= "" then
elseif unit and unit ~= "" then
return tostring(val).. " "..tostring(unit)
return tostring(val) .. " " .. tostring(unit)
else
else
return tostring(val)
return tostring(val)
end
end
end
end


if v == nil then
return (v ~= nil) and tostring(v) or nil
return nil
end
return tostring(v)
end
end


----------------------------------------------------------------------
----------------------------------------------------------------------
--Dynamic field helpers(JS - driven)
-- Dynamic spans (JS-driven)
----------------------------------------------------------------------
----------------------------------------------------------------------


local function dynSpan(series, level)
-- dynSpan: render a JS-updated span for a level series.
if type(series) ~= "table" or #series == 0 then
local function dynSpan(series, level)
return nil
if type(series) ~= "table" or #series == 0 then
end
return nil
level = clamp(level or #series, 1, #series)
end
 
level = clamp(level or #series, 1, #series)


local span = mw.html.create("span")
local span = mw.html.create("span")
span: addClass("sv-dyn")
span:addClass("sv-dyn")
span: attr("data-series", mw.text.jsonEncode(series))
span:attr("data-series", mw.text.jsonEncode(series))
span: wikitext(mw.text.nowiki(series[level] or ""))
span:wikitext(mw.text.nowiki(series[level] or ""))


return tostring(span)
return tostring(span)
end
end


-- isFlatList: true if all values in list are identical.
local function isFlatList(list)
local function isFlatList(list)
if type(list) ~= "table" or #list == 0 then
if type(list) ~= "table" or #list == 0 then
return false
return false
end
end
local first = tostring(list[1])
local first = tostring(list[1])
for i = 2, #list do
for i = 2, #list do
if tostring(list[i]) ~= first then
if tostring(list[i]) ~= first then
return false
return false
end
end
end
end
return true
return true
end
end


-- isNonZeroScalar: detect if a value is present and not effectively zero.
local function isNonZeroScalar(v)
local function isNonZeroScalar(v)
if v == nil then return false end
if v == nil then return false end
if type(v) == "number" then return v ~= 0 end
if type(v) == "number" then return v ~= 0 end
if type(v) == "string" then
if type(v) == "string" then
local n = tonumber(v)
local n = tonumber(v)
if n == nil then
if n == nil then return v ~= "" end
return v ~= ""
return n ~= 0
end
end
return n ~= 0
if type(v) == "table" and v.Value ~= nil then
end
return isNonZeroScalar(v.Value)
if type(v) == "table" and v.Value ~= nil then
end
return isNonZeroScalar(v.Value)
return true
end
return true
end
end


--Like your old valuePairLines / valuePairText, but:
-- isZeroish: aggressively treat common “zero” text forms as zero.
-- - if Per Level is a list, render a dynamic span instead of "v1 / v2 / ..."
local function isZeroish(v)
-- - scalar Per Level stays as the old "Base" + "Per Level" lines(still short)
if v == nil then return true end
local function valuePairDynamicLines(name, block, maxLevel, level)
if type(v) == "number" then return v == 0 end
if type(block) ~= "table" then
if type(v) == "table" and v.Value ~= nil then
return {}
return isZeroish(v.Value)
end
end


local base = block.Base
local s = mw.text.trim(tostring(v))
local per = block["Per Level"]
if s == "" then return true end
if s == "0" or s == "0.0" or s == "0.00" then return true end
if s == "0s" or s == "0 s" then return true end
if s == "0m" or s == "0 m" then return true end
if s == "0%" or s == "0 %" then return true end


--Per Level list(expanded)
local n = tonumber((mw.ustring.gsub(s, "[^0-9%.%-]", "")))
if type(per) == "table" then
return (n ~= nil and n == 0)
--empty list -> show Base only
if #per == 0 then
local baseText = formatUnitValue(base)
if baseText then
return { string.format("%s: %s", name, mw.text.nowiki(baseText)) }
end
return {}
end
end


--flat list -> show single value
-- valuePairRawText: render Base/Per Level blocks into readable text (fallback).
if isFlatList(per) then
local function valuePairRawText(block)
local baseText = formatUnitValue(base)
if type(block) ~= "table" then
local one = formatUnitValue(per[1]) or tostring(per[1])
return nil
local show = baseText or one
end
if show then
 
return { string.format("%s: %s", name, mw.text.nowiki(show)) }
local base = block.Base
end
local per  = block["Per Level"]
return {}
end


--dynamic series
if type(per) == "table" then
local series = {}
if #per == 0 then
for _, v in ipairs(per) do
return formatUnitValue(base)
table.insert(series, formatUnitValue(v) or tostring(v))
end
if isFlatList(per) then
return formatUnitValue(base) or tostring(per[1])
end
end


local dyn = dynSpan(series, level)
local vals = {}
if dyn then
for _, v in ipairs(per) do
return { string.format("%s: %s", name, dyn) }
table.insert(vals, formatUnitValue(v) or tostring(v))
end
end
return {}
return (#vals > 0) and table.concat(vals, " / ") or nil
end
end


--scalar Per Level(keep old style)
local lines = {}
local baseText = formatUnitValue(base)
local baseText = formatUnitValue(base)
local perText = formatUnitValue(per)
local perText = formatUnitValue(per)
 
if baseText and perText and isNonZeroScalar(per) then
return string.format("%s (Per Level: %s)", baseText, perText)
end


if baseText then
return baseText or perText
table.insert(lines, string.format("%s: %s", name, mw.text.nowiki(baseText)))
end
if perText and isNonZeroScalar(per) then
table.insert(lines, string.format("%s Per Level: %s", name, mw.text.nowiki(perText)))
end
end


return lines
-- valuePairDynamicValueOnly: render Base/Per Level blocks using dyn spans where possible.
end
local function valuePairDynamicValueOnly(block, maxLevel, level)
if type(block) ~= "table" then
return nil
end
 
local base = block.Base
local per  = block["Per Level"]
 
if type(per) == "table" then
if #per == 0 then
local baseText = formatUnitValue(base)
return baseText and mw.text.nowiki(baseText) or nil
end
 
if isFlatList(per) then
local one  = formatUnitValue(per[1]) or tostring(per[1])
local show = formatUnitValue(base) or one
return show and mw.text.nowiki(show) or nil
end
 
local series = {}
for _, v in ipairs(per) do
table.insert(series, formatUnitValue(v) or tostring(v))
end
return dynSpan(series, level)
end


local function valuePairDynamicText(name, block, maxLevel, level, sep)
local txt = valuePairRawText(block)
local lines = valuePairDynamicLines(name, block, maxLevel, level)
return txt and mw.text.nowiki(txt) or nil
if #lines == 0 then return nil end
return table.concat(lines, sep or "<br />")
end
end


----------------------------------------------------------------------
----------------------------------------------------------------------
--Lookups
-- Lookups
----------------------------------------------------------------------
----------------------------------------------------------------------


local function getSkillById(id)
-- getSkillById: locate a skill by internal ID.
id = trim(id)
local function getSkillById(id)
if not id then return nil end
id = trim(id)
if not id then return nil end
local dataset = getSkills()
local dataset = getSkills()
local byId = dataset.byId or { }
return (dataset.byId or {})[id]
return byId[id]
end
end


-- findSkillByName: locate a skill by external/display name.
local function findSkillByName(name)
local function findSkillByName(name)
name = trim(name)
name = trim(name)
if not name then return nil end
if not name then return nil end
 
local dataset = getSkills()
local dataset = getSkills()
local byName = dataset.byName or {}
if byName[name] then
return byName[name]
end


local byName = dataset.byName or { }
for _, rec in ipairs(dataset.records or {}) do
if byName[name] then
if type(rec) == "table" then
return byName[name]
if rec["External Name"] == name or rec.Name == name or rec["Display Name"] == name then
end
return rec
end
end
end


for _, rec in ipairs(dataset.records or {}) do
return nil
if type(rec) == "table" then
if rec["External Name"] == name or rec["Name"] == name or rec["Display Name"] == name then
return rec
end
end
end
return nil
end
end


----------------------------------------------------------------------
----------------------------------------------------------------------
--Formatting helpers
-- Legacy damage helpers
----------------------------------------------------------------------
----------------------------------------------------------------------


local function basisLabel(entry, isHealing)
-- basisLabel: label ATK/MATK basis in legacy damage blocks.
if isHealing then
local function basisLabel(entry, isHealing)
return "Healing"
if isHealing then
end
return "Healing"
local atk = entry and entry["ATK-Based"]
end
 
local atk = entry and entry["ATK-Based"]
local matk = entry and entry["MATK-Based"]
local matk = entry and entry["MATK-Based"]
if atk and matk then
 
return "Attack/Magic Attack"
if atk and matk then
return "Attack/Magic Attack"
elseif atk then
elseif atk then
return "Attack"
return "Attack"
elseif matk then
elseif matk then
return "Magic Attack"
return "Magic Attack"
end
end
return "Damage"
 
return "Damage"
end
end


--Dynamic damage entry:
-- formatDamageEntry: legacy percent damage formatting (dynamic by level).
--Build a series for Lv1..LvMax, and show only the selected one.
local function formatDamageEntry(entry, maxLevel, level)
local function formatDamageEntry(entry, maxLevel, level)
if type(entry) ~= "table" then
if type(entry) ~= "table" then
return nil
return nil
end
end


local isHealing = (entry.Type == "Healing")
local isHealing = (entry.Type == "Healing")
local basis = isHealing and "Healing" or basisLabel(entry)
local basis = isHealing and "Healing" or basisLabel(entry, false)


local baseRaw = entry["Base %"]
local baseRaw = entry["Base %"]
local perRaw = entry["Per Level %"]
local perRaw = entry["Per Level %"]


local baseN = toNum(baseRaw)
local baseN = toNum(baseRaw)
local perN = toNum(perRaw)
local perN = toNum(perRaw)


local function baseIsPresent()
local function baseIsPresent()
if baseN ~= nil then
if baseN ~= nil then
return baseN ~= 0
return baseN ~= 0
end
end
if baseRaw ~= nil then
if baseRaw ~= nil then
local s = tostring(baseRaw)
local s = tostring(baseRaw)
return (s ~= "" and s ~= "0" and s ~= "0.0" and s ~= "0.00")
return (s ~= "" and s ~= "0" and s ~= "0.0" and s ~= "0.00")
end
end
return false
return false
end
end


local baseText = nil
local baseText
if baseIsPresent() then
if baseIsPresent() then
if baseN ~= nil then
baseText = (baseN ~= nil) and (fmtNum(baseN) .. "%") or (tostring(baseRaw) .. "%")
baseText = fmtNum(baseN).. "%"
end
else
baseText = tostring(baseRaw).. "%"
end
end


--No scaling -> just show base
if perN == nil or perN == 0 or not maxLevel or maxLevel <= 0 then
if perN == nil or perN == 0 or not maxLevel or maxLevel <= 0 then
return baseText and mw.text.nowiki(baseText .. " " .. basis) or nil
return baseText and mw.text.nowiki(baseText.. " "..basis) or nil
end
end


--Build series strings for each level
local series = {}
local series = {}
for lv = 1, maxLevel do
for lv = 1, maxLevel do
local perPart = perN * lv
local perPart = perN * lv


if baseText and baseN ~= nil then
if baseText and baseN ~= nil then
local total = baseN + perPart
local total = baseN + perPart
table.insert(series, string.format("%s%% %s", fmtNum(total), basis))
table.insert(series, string.format("%s%% %s", fmtNum(total), basis))
elseif baseText then
elseif baseText then
table.insert(series, string.format("%s + %s%% %s", baseText, fmtNum(perPart), basis))
table.insert(series, string.format("%s + %s%% %s", baseText, fmtNum(perPart), basis))
else
else
table.insert(series, string.format("%s%% %s", fmtNum(perPart), basis))
table.insert(series, string.format("%s%% %s", fmtNum(perPart), basis))
end
end
end
end


return dynSpan(series, level)
return dynSpan(series, level)
end
end


-- formatDamageList: render a list of legacy damage entries into <br/> blocks.
local function formatDamageList(list, maxLevel, level, includeTypePrefix)
local function formatDamageList(list, maxLevel, level, includeTypePrefix)
if type(list) ~= "table" or #list == 0 then
if type(list) ~= "table" or #list == 0 then
return nil
return nil
end
end


local parts = {}
local parts = {}
for _, d in ipairs(list) do
for _, d in ipairs(list) do
if type(d) == "table" then
if type(d) == "table" then
local txt = formatDamageEntry(d, maxLevel, level)
local txt = formatDamageEntry(d, maxLevel, level)
if txt then
if txt then
if includeTypePrefix and d.Type and d.Type ~= "" then
if includeTypePrefix and d.Type and d.Type ~= "" then
table.insert(parts, mw.text.nowiki(tostring(d.Type).. ": ")..txt)
table.insert(parts, mw.text.nowiki(tostring(d.Type) .. ": ") .. txt)
else
else
table.insert(parts, txt)
table.insert(parts, txt)
end
end
end
end
 
return (#parts > 0) and table.concat(parts, "<br />") or nil
end
end
----------------------------------------------------------------------
-- Users matching
----------------------------------------------------------------------
-- skillMatchesUser: check if a skill is used by a specific class/monster/summon/event.
local function skillMatchesUser(rec, userName)
if type(rec) ~= "table" or not userName or userName == "" then
return false
end
local users = rec.Users
if type(users) ~= "table" then
return false
end
local userLower = mw.ustring.lower(userName)
local function listHas(list)
if type(list) ~= "table" then
return false
end
for _, v in ipairs(list) do
if type(v) == "string" and mw.ustring.lower(v) == userLower then
return true
end
end
return false
end
return listHas(users.Classes) or listHas(users.Summons) or listHas(users.Monsters) or listHas(users.Events)
end
end
----------------------------------------------------------------------
-- Hide Users on direct skill pages
----------------------------------------------------------------------
-- isDirectSkillPage: hide Users rows when viewing the skill page itself.
local function isDirectSkillPage(rec)
if type(rec) ~= "table" then
return false
end
local pageTitle = mw.title.getCurrentTitle()
local pageName  = pageTitle and pageTitle.text or ""
pageName = trim(pageName)
if not pageName then
return false
end
pageName = mw.ustring.lower(pageName)
local ext = trim(rec["External Name"] or rec.Name or rec["Display Name"])
local internal = trim(rec["Internal Name"] or rec.InternalName or rec.InternalID)
return (ext and mw.ustring.lower(ext) == pageName) or (internal and mw.ustring.lower(internal) == pageName) or false
end
end
----------------------------------------------------------------------
-- Slot config (edit these tables only to rearrange layout)
----------------------------------------------------------------------
local HERO_SLOT_ASSIGNMENT = {
[1] = "IconName",
[2] = "Description",
[3] = "LevelSelector",
[4] = "SkillType",
[5] = "SourceType",
[6] = "QuickStats",
[7] = "SpecialMechanics",
[8] = "Placeholder",
}
----------------------------------------------------------------------
-- Slot scaffolds
----------------------------------------------------------------------
-- slotBox: standardized wrapper for all hero card slots.
local function slotBox(slot, extraClasses, innerHtml, opts)
opts = opts or {}
local box = mw.html.create("div")
box:addClass("sv-slot")
box:addClass("sv-slot--" .. tostring(slot))
box:attr("data-hero-slot", tostring(slot))
if opts.isFull then
box:addClass("sv-slot--full")
end
if extraClasses then
if type(extraClasses) == "string" then
box:addClass(extraClasses)
elseif type(extraClasses) == "table" then
for _, c in ipairs(extraClasses) do box:addClass(c) end
end
end
if opts.isEmpty then
box:addClass("sv-slot--empty")
end
local body = box:tag("div"):addClass("sv-slot__body")
if innerHtml and innerHtml ~= "" then
body:wikitext(innerHtml)
end
return tostring(box)
end
end


if #parts == 0 then
----------------------------------------------------------------------
return nil
-- Shared helpers (Source + QuickStats + SpecialMechanics)
end
----------------------------------------------------------------------
return table.concat(parts, "<br />")
 
end
-- formatScalingCompactLines: build compact “Scaling” lines for SourceType.
local function formatScalingCompactLines(scaling)
if type(scaling) ~= "table" then
return {}
end


local function formatScaling(list, basisOverride)
local list = scaling
if type(list) ~= "table" or #list == 0 then
if #list == 0 then
return nil
if scaling.Percent ~= nil or scaling["Scaling ID"] or scaling["Scaling Name"] then
end
list = { scaling }
else
return {}
end
end


local parts = {}
local out = {}
for _, s in ipairs(list) do
for _, s in ipairs(list) do
if type(s) == "table" then
if type(s) == "table" then
local stat = s["Scaling Name"] or s["Scaling ID"] or "Unknown"
local stat = s["Scaling Name"] or s["Scaling ID"] or "Unknown"
local pct = s.Percent
local pct = s.Percent
local pctN = toNum(pct)
local pctN = toNum(pct)


local basis = basisOverride or basisLabel(s, false)
if pctN ~= nil and pctN ~= 0 then
table.insert(out, string.format("%s%% %s", fmtNum(pctN), stat))
elseif pct ~= nil and tostring(pct) ~= "" and tostring(pct) ~= "0" then
table.insert(out, string.format("%s%% %s", tostring(pct), stat))
end
end
end


if pctN ~= nil and pctN ~= 0 then
return out
table.insert(parts, string.format("%s%% %s Per %s", fmtNum(pctN), basis, stat))
elseif pct ~= nil and tostring(pct) ~= "" and tostring(pct) ~= "0" then
table.insert(parts, string.format("%s%% %s Per %s", tostring(pct), basis, stat))
end
end
end
-- basisWordFromFlags: compute “Attack/Magic/Hybrid” from ATK/MATK booleans.
local function basisWordFromFlags(atkFlag, matkFlag)
if atkFlag and matkFlag then
return "Hybrid"
elseif atkFlag then
return "Attack"
elseif matkFlag then
return "Magic Attack"
end
return nil
end
end


if #parts == 0 then
-- legacyPercentAtLevel: compute “Base% + PerLevel%*level” for legacy entries.
return nil
local function legacyPercentAtLevel(entry, level)
end
if type(entry) ~= "table" then
return table.concat(parts, "<br />")
return nil
end
 
local baseRaw = entry["Base %"]
local perRaw  = entry["Per Level %"]
local baseN  = toNum(baseRaw)
local perN    = toNum(perRaw)
 
if perN ~= nil and perN ~= 0 then
local total = (baseN or 0) + (perN * level)
return fmtNum(total) .. "%"
end
 
if baseN ~= nil then
return fmtNum(baseN) .. "%"
end
if baseRaw ~= nil and tostring(baseRaw) ~= "" then
return tostring(baseRaw) .. "%"
end
 
return nil
end
end


local function formatArea(area, maxLevel, level)
-- seriesFromValuePair: normalize Base/Per Level blocks into a level-indexed series.
if type(area) ~= "table" then
local function seriesFromValuePair(block, maxLevel)
return nil
if type(block) ~= "table" then
end
return nil
local parts = {}
end


local distLine = valuePairDynamicText("Distance", area["Area Distance"], maxLevel, level, "<br />")
local base = block.Base
if distLine then
local per  = block["Per Level"]
table.insert(parts, distLine)
end


local size = area["Area Size"]
local function pickUnit(v)
if size and size ~= "" then
if type(v) == "table" and v.Unit and v.Unit ~= "" then
table.insert(parts, "Size: "..mw.text.nowiki(tostring(size)))
return v.Unit
end
end
return nil
end
local unit = pickUnit(base) or pickUnit(per)


if #parts == 0 then
local function fmtAny(v)
return nil
local t = formatUnitValue(v)
end
return t and tostring(t) or (v ~= nil and tostring(v) or nil)
return table.concat(parts, "<br />")
end
end


local function formatTimingBlock(bt, maxLevel, level)
local series = {}
if type(bt) ~= "table" then
return nil
end
local parts = {}


local function add(label, key)
-- Expanded per-level series (wikiprep)
local block = bt[key]
if type(per) == "table" and #per > 0 then
if type(block) ~= "table" then
for lv = 1, maxLevel do
return
local raw = per[lv] or per[#per]
end
local one = fmtAny(raw)
local lines = valuePairDynamicLines(label, block, maxLevel, level)
if one == nil or isZeroish(raw) or isZeroish(one) then
for _, line in ipairs(lines) do
one = "—"
table.insert(parts, line)
end
series[lv] = one
end
end
return series
end
end


add("Cast Time", "Cast Time")
-- Empty per-list -> base only
add("Cooldown", "Cooldown")
if type(per) == "table" and #per == 0 then
add("Duration", "Duration")
local one = fmtAny(base)
if one == nil or isZeroish(base) or isZeroish(one) then
one = ""
end
for lv = 1, maxLevel do
series[lv] = one
end
return series
end


if bt["Effect Cast Time"] ~= nil then
-- Scalar per -> compute base + per*level (fallback)
table.insert(parts, "Effect Cast Time: "..mw.text.nowiki(tostring(bt["Effect Cast Time"])))
local baseN = toNum(base) or 0
end
local perN  = toNum(per)
if bt["Damage Delay"] ~= nil then
table.insert(parts, "Damage Delay: "..mw.text.nowiki(tostring(bt["Damage Delay"])))
end
if bt["Effect Remove Delay"] ~= nil then
table.insert(parts, "Effect Remove Delay: "..mw.text.nowiki(tostring(bt["Effect Remove Delay"])))
end


if #parts == 0 then
if perN ~= nil then
return nil
for lv = 1, maxLevel do
end
local total = baseN + (perN * lv)
return table.concat(parts, "<br />")
local v = unit and { Value = total, Unit = unit } or total
end
local one = fmtAny(v)
if one == nil or total == 0 or isZeroish(one) then
one = "—"
end
series[lv] = one
end
return series
end


local function formatResourceCost(rc, maxLevel, level)
-- Base-only scalar
if type(rc) ~= "table" then
local raw = (base ~= nil) and base or per
return nil
local one = fmtAny(raw)
if one == nil then
return nil
end
if isZeroish(raw) or isZeroish(one) then
one = ""
end
for lv = 1, maxLevel do
series[lv] = one
end
return series
end
end
local parts = {}


local manaLines = valuePairDynamicLines("MP", rc["Mana Cost"], maxLevel, level)
-- displayFromSeries: render a series as fixed text or dynSpan (nil if all “—”).
for _, line in ipairs(manaLines) do
local function displayFromSeries(series, level)
table.insert(parts, line)
if type(series) ~= "table" or #series == 0 then
return nil
end
end


local hpLines = valuePairDynamicLines("HP", rc["Health Cost"], maxLevel, level)
local any = false
for _, line in ipairs(hpLines) do
for _, v in ipairs(series) do
table.insert(parts, line)
if v ~= "—" then
any = true
break
end
end
if not any then
return nil
end
end


if #parts == 0 then
if isFlatList(series) then
return nil
return mw.text.nowiki(series[1])
end
end
return table.concat(parts, "<br />")
return dynSpan(series, level)
end
end


local function formatCombo(combo)
-- formatAreaSize: human readable area sizing for QuickStats.
if type(combo) ~= "table" then
-- Shows: "<Area Size> (<number>)" e.g. "Medium (4)"
return nil
local function formatAreaSize(area, maxLevel, level)
end
if type(area) ~= "table" then
local parts = {}
return nil
end
 
-- Helper: pull a number from scalar/unit/valuepair-ish things.
local function extractNumber(v)
if v == nil then return nil end
 
-- Unit block {Value, Unit}
if type(v) == "table" and v.Value ~= nil then
local n = toNum(v.Value)
return n
end
 
-- ValuePair {Base, Per Level} -> prefer Base at current level if series exists
if type(v) == "table" and (v.Base ~= nil or v["Per Level"] ~= nil) then
local s = seriesFromValuePair(v, maxLevel or 1)
if type(s) == "table" and #s > 0 then
local idx = clamp(level or 1, 1, #s)
local txt = s[idx]
if txt and txt ~= "—" then
-- try parse numeric from string (e.g. "4 tiles" -> 4)
local num = tonumber((mw.ustring.gsub(tostring(txt), "[^0-9%.%-]", "")))
return num
end
end
return nil
end


if combo.Type then
-- Plain scalar
table.insert(parts, "Type: "..mw.text.nowiki(tostring(combo.Type)))
if type(v) == "number" then return v end
end
if type(v) == "string" then
local num = tonumber((mw.ustring.gsub(mw.text.trim(v), "[^0-9%.%-]", "")))
return num
end


local durText = formatUnitValue(combo.Duration)
return nil
if durText then
end
table.insert(parts, "Duration: "..mw.text.nowiki(durText))
end


if combo.Percent ~= nil then
-- 1) Read Area Size label/name
local pctText = formatUnitValue(combo.Percent)
local rawSize = area["Area Size"]
if pctText then
if rawSize == nil then
table.insert(parts, "Bonus: "..mw.text.nowiki(pctText))
return nil
end
end
end


if #parts == 0 then
local sizeName = nil
return nil
if type(rawSize) == "table" then
end
sizeName = rawSize.Name or rawSize.ID or rawSize.Value
return table.concat(parts, ", ")
elseif type(rawSize) == "string" then
end
sizeName = rawSize
elseif type(rawSize) == "number" then
-- uncommon; treat as numeric-only label
sizeName = tostring(rawSize)
end


local function valuePairRawText(block)
sizeName = sizeName and mw.text.trim(tostring(sizeName)) or nil
if type(block) ~= "table" then
if not sizeName or sizeName == "" or isNoneLike(sizeName) then
return nil
return nil
end
end


local base = block.Base
-- 2) Find the numeric “exact number” to append
local per = block["Per Level"]
-- Prefer the explicit Area Distance block, then fall back to other known numeric keys.
local num = nil


if type(per) == "table" then
local dist = area["Area Distance"]
if #per == 0 then
if type(dist) == "table" then
return formatUnitValue(base)
-- Prefer Effective Distance if present and non-zero, else Base
end
num = extractNumber(dist["Effective Distance"]) or extractNumber(dist.Effective) or extractNumber(dist["Effective"])
if isFlatList(per) then
if not num or num == 0 then
return formatUnitValue(base) or tostring(per[1])
num = extractNumber(dist.Base) or extractNumber(dist["Base"])
end
local vals = {}
for _, v in ipairs(per) do
table.insert(vals, formatUnitValue(v) or tostring(v))
end
end
return (#vals > 0) and table.concat(vals, " / ") or nil
end
end
 
if not num or num == 0 then
num =
extractNumber(area["Area Value"]) or
extractNumber(area["Area Size Value"]) or
extractNumber(area["Area Number"]) or
extractNumber(area["Area Radius"])
end


local baseText = formatUnitValue(base)
-- 3) Render
local perText = formatUnitValue(per)
-- If size already contains parentheses, assume it already includes the numeric.
if mw.ustring.find(sizeName, "%(") then
return mw.text.nowiki(sizeName)
end


if baseText and perText and isNonZeroScalar(per) then
if num and num ~= 0 then
return string.format("%s (Per Level: %s)", baseText, perText)
return mw.text.nowiki(string.format("%s (%s)", sizeName, fmtNum(num)))
end
end
return baseText or perText
end


--Special Mechanics supports dynamic Per Level lists
return mw.text.nowiki(sizeName)
local function formatMechanicEffects(effects, maxLevel, level)
if type(effects) ~= "table" then
return nil
end
end


local keys = {}
-- skillHasAnyDamage: determine if a skill has any meaningful damage (for non-damaging rules).
for k, _ in pairs(effects) do
local function skillHasAnyDamage(rec, maxLevel)
table.insert(keys, k)
if type(rec.Source) == "table" then
local s = seriesFromValuePair(rec.Source, maxLevel)
if s then
for _, v in ipairs(s) do
if v ~= "—" then return true end
end
end
end
end
table.sort(keys)


local parts = {}
if type(rec.Damage) == "table" then
local dmg = rec.Damage
for _, key in ipairs({ "Main Damage", "Flat Damage", "Reflect Damage" }) do
local lst = dmg[key]
if type(lst) == "table" and #lst > 0 then
return true
end
end
end


local function effectAmount(block)
return false
if type(block) ~= "table" then
return nil
end
end


local per = block["Per Level"]
-- computeDurationPromotion: promote status-duration into QuickStats when a skill is non-damaging.
local function computeDurationPromotion(rec, maxLevel)
if type(rec) ~= "table" then return nil end
if skillHasAnyDamage(rec, maxLevel) then return nil end


if type(per) == "table" and #per > 0 then
local mech = (type(rec.Mechanics) == "table") and rec.Mechanics or {}
if isFlatList(per) then
local bt  = (type(mech["Basic Timings"]) == "table") and mech["Basic Timings"] or {}
return mw.text.nowiki(formatUnitValue(per[1]) or tostring(per[1]))
local durS = seriesFromValuePair(bt["Duration"], maxLevel)
end
 
local series = {}
if durS ~= nil then
for _, v in ipairs(per) do
local any = false
table.insert(series, formatUnitValue(v) or tostring(v))
for _, v in ipairs(durS) do
if v ~= "—" then any = true break end
end
if any then
return nil
end
end
 
local apps = rec["Status Applications"]
if type(apps) ~= "table" then return nil end
 
for idx, app in ipairs(apps) do
if type(app) == "table" and type(app.Duration) == "table" then
local s = seriesFromValuePair(app.Duration, maxLevel)
if s then
for _, v in ipairs(s) do
if v ~= "—" then
return {
durationBlock = app.Duration,
suppressDurationIndex = idx,
}
end
end
end
end
return dynSpan(series, level)
end
end
 
return nil
end
end


local pair = { Base = block.Base, ["Per Level"] = block["Per Level"] }
----------------------------------------------------------------------
local txt = valuePairRawText(pair)
-- Plug-ins
return txt and mw.text.nowiki(txt) or nil
----------------------------------------------------------------------
end
 
local PLUGINS = {}
 
-- PLUGIN: IconName (Hero Bar Slot 1) - icon + name.
function PLUGINS.IconName(rec, ctx)
local icon  = rec.Icon
local title = rec["External Name"] or rec.Name or rec["Internal Name"] or "Unknown Skill"
 
local notesList = {}
if type(rec.Notes) == "table" then
for _, note in ipairs(rec.Notes) do
local n = trim(note)
if n then
table.insert(notesList, mw.text.nowiki(n))
end
end
elseif type(rec.Notes) == "string" then
local n = trim(rec.Notes)
if n then
notesList = { mw.text.nowiki(n) }
end
end


for _, name in ipairs(keys) do
local req = rec.Requirements or {}
local block = effects[name]
local reqSkillsRaw = (type(req["Required Skills"]) == "table") and req["Required Skills"] or {}
if type(block) == "table" then
local reqWeaponsRaw = (type(req["Required Weapons"]) == "table") and req["Required Weapons"] or {}
local t = block.Type
local reqStancesRaw = (type(req["Required Stances"]) == "table") and req["Required Stances"] or {}


if t ~= nil and tostring(t) ~= "" then
local reqSkills = {}
local amt = effectAmount(block)
for _, rs in ipairs(reqSkillsRaw) do
local seg = mw.text.nowiki(tostring(t).. " - "..tostring(name))
if type(rs) == "table" then
if amt then
local nameReq = rs["Skill External Name"] or rs["Skill Internal Name"] or "Unknown"
seg = seg.. " + "..amt
local lvlReq  = rs["Required Level"]
end
if lvlReq then
table.insert(parts, seg)
table.insert(reqSkills, string.format("%s (Lv.%s)", mw.text.nowiki(nameReq), mw.text.nowiki(tostring(lvlReq))))
else
else
local txt = valuePairDynamicText(name, block, maxLevel, level, ", ")
table.insert(reqSkills, mw.text.nowiki(nameReq))
if txt then
end
table.insert(parts, txt)
end
end
end
end
 
end
local reqWeapons = {}
for _, w in ipairs(reqWeaponsRaw) do
local wn = trim(w)
if wn then table.insert(reqWeapons, mw.text.nowiki(wn)) end
end
 
local reqStances = {}
for _, s in ipairs(reqStancesRaw) do
local sn = trim(s)
if sn then table.insert(reqStances, mw.text.nowiki(sn)) end
end
 
local hasNotes = (#notesList > 0)
local hasReq = (#reqSkills > 0) or (#reqWeapons > 0) or (#reqStances > 0)
 
 
local wrap = mw.html.create("div")
wrap:addClass("sv-herobar-1-wrap")
wrap:addClass("sv-tip-scope")
 
local iconBox = wrap:tag("div")
iconBox:addClass("sv-herobar-icon")
 
if icon and icon ~= "" then
iconBox:wikitext(string.format("[[File:%s|80px|link=]]", icon))
end
 
local textBox = wrap:tag("div")
textBox:addClass("sv-herobar-text")
 
local titleRow = textBox:tag("div")
titleRow:addClass("sv-herobar-title-row")
 
local titleBox = titleRow:tag("div")
titleBox:addClass("spiritvale-infobox-title")
titleBox:wikitext(title)
 
if hasNotes then
local notesBtn = mw.html.create("span")
notesBtn:addClass("sv-tip-btn sv-tip-btn--notes")
notesBtn:attr("role", "button")
notesBtn:attr("tabindex", "0")
notesBtn:attr("data-sv-tip", "notes")
notesBtn:attr("aria-label", "Notes")
notesBtn:attr("aria-expanded", "false")
notesBtn:tag("span"):addClass("sv-ico sv-ico--info"):attr("aria-hidden", "true"):wikitext("i")
titleRow:node(notesBtn)
end
 
if hasReq then
local pillRow = wrap:tag("div")
pillRow:addClass("sv-pill-row")
pillRow:addClass("sv-pill-row--req")
local pill = pillRow:tag("span")
pill:addClass("sv-pill sv-pill--req sv-tip-btn")
pill:attr("role", "button")
pill:attr("tabindex", "0")
pill:attr("data-sv-tip", "req")
pill:attr("aria-label", "Requirements")
pill:attr("aria-expanded", "false")
pill:wikitext("Requirements")
end
 
if hasNotes then
local notesContent = wrap:tag("div")
notesContent:addClass("sv-tip-content")
notesContent:attr("data-sv-tip-content", "notes")
notesContent:tag("div"):addClass("sv-tip-title"):wikitext("Notes")
notesContent:tag("div"):wikitext(table.concat(notesList, "<br />"))
end
 
if hasReq then
local reqContent = wrap:tag("div")
reqContent:addClass("sv-tip-content")
reqContent:attr("data-sv-tip-content", "req")
 
if #reqSkills > 0 then
local section = reqContent:tag("div")
section:addClass("sv-tip-section")
section:tag("span"):addClass("sv-tip-label"):wikitext("Required Skills")
section:tag("div"):wikitext(table.concat(reqSkills, "<br />"))
end
 
if #reqWeapons > 0 then
local section = reqContent:tag("div")
section:addClass("sv-tip-section")
section:tag("span"):addClass("sv-tip-label"):wikitext("Required Weapons")
section:tag("div"):wikitext(table.concat(reqWeapons, ", "))
end
 
if #reqStances > 0 then
local section = reqContent:tag("div")
section:addClass("sv-tip-section")
section:tag("span"):addClass("sv-tip-label"):wikitext("Required Stances")
section:tag("div"):wikitext(table.concat(reqStances, ", "))
end
end
 
return {
inner = tostring(wrap),
classes = "module-icon-name",
}
end
end


if #parts == 0 then
-- PLUGIN: SkillType (Hero Bar Slot 2) - 2 rows x 3 cells (desktop + mobile).
return nil
-- Rules:
end
--  - If skill is non-damaging, hide Damage/Element/Hits.
return table.concat(parts, "<br />")
--  - If Hits is empty, hide Hits.
end
--  - If Combo is empty, hide Combo.
-- Ordering:
--  - Desktop: Damage, Element, Hits, Target, Cast, Combo
--  - Mobile:  Damage, Element, Target, Cast, Hits, Combo (CSS reorder)
function PLUGINS.SkillType(rec, ctx)
local typeBlock = (type(rec.Type) == "table") and rec.Type or {}
local mech      = (type(rec.Mechanics) == "table") and rec.Mechanics or {}
 
local level    = ctx.level or 1
local maxLevel = ctx.maxLevel or 1
 
local hideDamageBundle = (ctx.nonDamaging == true)
 
-- valName: extract a display string from typical {Name/ID/Value} objects.
-- NOTE: Includes number support so Hits=2 (number) doesn't get dropped.
local function valName(x)
if x == nil then return nil end
if type(x) == "table" then
if x.Name and x.Name ~= "" then return tostring(x.Name) end
if x.ID and x.ID ~= "" then return tostring(x.ID) end
if x.Value ~= nil then return tostring(x.Value) end
end
if type(x) == "number" then
return tostring(x)
end
if type(x) == "string" and x ~= "" then
return x
end
return nil
end
 
-- hitsDisplay: find + render Hits from multiple possible structured locations.
local function hitsDisplay()
local effects = (type(mech.Effects) == "table") and mech.Effects or {}
 
local h =
typeBlock.Hits or typeBlock["Hits"] or typeBlock["Hit Count"] or typeBlock["Hits Count"] or
mech.Hits or mech["Hits"] or mech["Hit Count"] or mech["Hits Count"] or
effects.Hits or effects["Hits"] or effects["Hit Count"] or effects["Hits Count"] or
rec.Hits or rec["Hits"]
 
if h == nil or isNoneLike(h) then
return nil
end
 
-- ValuePair-style table (Base/Per Level) => dynamic series
if type(h) == "table" then
if h.Base ~= nil or h["Per Level"] ~= nil or type(h["Per Level"]) == "table" then
return displayFromSeries(seriesFromValuePair(h, maxLevel), level)
end
 
-- Unit block {Value, Unit}
if h.Value ~= nil then
local t = formatUnitValue(h)
return t and mw.text.nowiki(t) or nil
end


local function formatModifiers(mods)
-- Fallback name extraction
if type(mods) ~= "table" then
local function valName(x)
return nil
if x == nil then return nil end
end
if type(x) == "table" then
local parts = {}
if x.Name and x.Name ~= "" then return tostring(x.Name) end
if x.ID and x.ID ~= "" then return tostring(x.ID) end
if x.Value ~= nil then return tostring(x.Value) end
end
if type(x) == "number" then return tostring(x) end
if type(x) == "string" and x ~= "" then return x end
return nil
end


local function collect(label, sub)
local vn = valName(h)
if type(sub) ~= "table" then
if vn and not isNoneLike(vn) then
return
return mw.text.nowiki(vn)
end
local flags = {}
for k, v in pairs(sub) do
if v then
table.insert(flags, k)
end
end
end
-- Scalar number/string
if type(h) == "number" then
return mw.text.nowiki(fmtNum(h))
end
if type(h) == "string" then
local t = trim(h)
return (t and not isNoneLike(t)) and mw.text.nowiki(t) or nil
end
return nil
end
-- comboDisplay: render Combo as a compact text block (Type (+ details)).
local function comboDisplay()
local c = (type(mech.Combo) == "table") and mech.Combo or nil
if not c then return nil end
local typ = trim(c.Type)
if not typ or isNoneLike(typ) then
return nil
end
local details = {}
local pct = formatUnitValue(c.Percent)
if pct and not isZeroish(pct) then
table.insert(details, mw.text.nowiki(pct))
end
local dur = formatUnitValue(c.Duration)
if dur and not isZeroish(dur) then
table.insert(details, mw.text.nowiki(dur))
end
if #details > 0 then
return mw.text.nowiki(typ) .. " (" .. table.concat(details, ", ") .. ")"
end
return mw.text.nowiki(typ)
end
local grid = mw.html.create("div")
grid:addClass("sv-type-grid")
grid:addClass("sv-compact-root")
local added = false
-- addChunk: add one labeled value cell (key drives CSS ordering).
local function addChunk(key, label, valueHtml)
if valueHtml == nil or valueHtml == "" then return end
added = true
local chunk = grid:tag("div")
:addClass("sv-type-chunk")
:addClass("sv-type-" .. tostring(key))
:attr("data-type-key", tostring(key))
chunk:tag("div")
:addClass("sv-type-label")
:wikitext(mw.text.nowiki(label))
chunk:tag("div")
:addClass("sv-type-value")
:wikitext(valueHtml)
end
-- Damage + Element + Hits bundle (hidden when non-damaging)
if not hideDamageBundle then
local dmg  = valName(typeBlock.Damage or typeBlock["Damage Type"])
local ele  = valName(typeBlock.Element or typeBlock["Element Type"])
local hits = hitsDisplay()
if dmg and not isNoneLike(dmg) then
addChunk("damage", "Damage", mw.text.nowiki(dmg))
end
if ele and not isNoneLike(ele) then
addChunk("element", "Element", mw.text.nowiki(ele))
end
if hits then
addChunk("hits", "Hits", hits)
end
end
-- Target + Cast
local tgt = valName(typeBlock.Target or typeBlock["Target Type"])
local cst = valName(typeBlock.Cast  or typeBlock["Cast Type"])
if tgt and not isNoneLike(tgt) then
addChunk("target", "Target", mw.text.nowiki(tgt))
end
if cst and not isNoneLike(cst) then
addChunk("cast", "Cast", mw.text.nowiki(cst))
end
-- Combo
local combo = comboDisplay()
if combo then
addChunk("combo", "Combo", combo)
end
        return {
                inner = added and tostring(grid) or "",
                classes = "module-skill-type",
        }
end
end
table.sort(flags)
 
if #flags > 0 then
-- PLUGIN: Description (Hero Slot 3) - primary description text.
table.insert(parts, string.format("%s: %s", label, table.concat(flags, ", ")))
function PLUGINS.Description(rec)
        local desc = trim(rec.Description)
        if not desc then
                return nil
        end
 
        local body = mw.html.create("div")
        body:addClass("sv-description")
        body:wikitext(string.format("''%s''", desc))
 
        return {
                inner = tostring(body),
                classes = "module-description",
        }
end
end
-- PLUGIN: Placeholder (Hero Slot 4) - reserved/blank.
function PLUGINS.Placeholder()
        return nil
end
end


collect("Movement", mods["Movement Modifiers"])
collect("Combat", mods["Combat Modifiers"])
collect("Special", mods["Special Modifiers"])


if #parts == 0 then
-- PLUGIN: SourceType (Hero Module Slot 1) - Modifier + Source + Scaling.
return nil
function PLUGINS.SourceType(rec, ctx)
end
local level = ctx.level or 1
return table.concat(parts, "<br />")
local maxLevel = ctx.maxLevel or 1
end


local function formatStatusApplications(list, maxLevel, level)
local basisWord = nil
if type(list) ~= "table" or #list == 0 then
local sourceKind = nil
return nil
local sourceVal  = nil
end
local scaling    = nil
local parts = {}
for _, s in ipairs(list) do
if type(s) == "table" then
local scope = s.Scope or "Target"
local name = s["Status External Name"] or s["Status Internal Name"] or "Unknown status"


local seg = scope.. " "..tostring(name)
-- sourceValueForLevel: dynamic formatting for structured Source blocks.
local detail = {}
local function sourceValueForLevel(src)
if type(src) ~= "table" then
return nil
end


if type(s.Duration) == "table" then
local per = src["Per Level"]
local t = valuePairDynamicText("Duration", s.Duration, maxLevel, level, "; ")
if type(per) == "table" and #per > 0 then
if t then table.insert(detail, t) end
if isFlatList(per) then
end
local one  = formatUnitValue(per[1]) or tostring(per[1])
local show = formatUnitValue(src.Base) or one
return show and mw.text.nowiki(show) or nil
end
 
local series = {}
for _, v in ipairs(per) do
table.insert(series, formatUnitValue(v) or tostring(v))
end
return dynSpan(series, level)
end


if type(s.Chance) == "table" then
return valuePairDynamicValueOnly(src, maxLevel, level)
local t = valuePairDynamicText("Chance", s.Chance, maxLevel, level, "; ")
end
if t then table.insert(detail, t) end
end


if s["Fixed Duration"] then
if type(rec.Source) == "table" then
table.insert(detail, "Fixed duration")
local src = rec.Source
end
local atkFlag  = (src["ATK-Based"] == true)
local matkFlag = (src["MATK-Based"] == true)
basisWord = basisWordFromFlags(atkFlag, matkFlag)


if #detail > 0 then
sourceKind = src.Type or ((src.Healing == true) and "Healing") or "Damage"
seg = seg.. " ("..table.concat(detail, ", ").. ")"
sourceVal  = sourceValueForLevel(src)
end
scaling    = src.Scaling
end


table.insert(parts, seg)
-- Fallback to legacy Damage lists if Source absent
end
if (sourceVal == nil or sourceVal == "") and type(rec.Damage) == "table" then
end
local dmg = rec.Damage
if #parts == 0 then
scaling = scaling or dmg.Scaling
return nil
end
return table.concat(parts, "<br />")
end


local function formatStatusRemoval(list, maxLevel, level)
local main = dmg["Main Damage"]
if type(list) ~= "table" or #list == 0 then
local refl = dmg["Reflect Damage"]
return nil
local flat = dmg["Flat Damage"]
end
local parts = {}
for _, r in ipairs(list) do
if type(r) == "table" then
local names = r["Status External Name"]
local label
if type(names) == "table" then
label = table.concat(names, ", ")
elseif type(names) == "string" then
label = names
else
label = "Status"
end


local amt = nil
if type(main) == "table" and #main > 0 then
if type(r["Per Level"]) == "table" and #r["Per Level"] > 0 and not isFlatList(r["Per Level"]) then
local pick = nil
local series = {}
for _, d in ipairs(main) do
for _, v in ipairs(r["Per Level"]) do
if type(d) == "table" and d.Type ~= "Healing" then
table.insert(series, formatUnitValue(v) or tostring(v))
pick = d
break
end
end
amt = dynSpan(series, level)
end
else
pick = pick or main[1]
amt = valuePairRawText(r)
 
if amt then amt = mw.text.nowiki(amt) end
if type(pick) == "table" then
end
local atkFlag  = (pick["ATK-Based"] == true)
local matkFlag = (pick["MATK-Based"] == true)
basisWord = basisWord or basisWordFromFlags(atkFlag, matkFlag)
 
sourceKind = (pick.Type == "Healing") and "Healing" or "Damage"
sourceVal  = legacyPercentAtLevel(pick, level)
end
elseif type(refl) == "table" and #refl > 0 and type(refl[1]) == "table" then
local pick = refl[1]
local atkFlag  = (pick["ATK-Based"] == true)
local matkFlag = (pick["MATK-Based"] == true)
basisWord = basisWord or basisWordFromFlags(atkFlag, matkFlag)
 
sourceKind = "Reflect"
sourceVal  = legacyPercentAtLevel(pick, level)
elseif type(flat) == "table" and #flat > 0 and type(flat[1]) == "table" then
local pick = flat[1]
local atkFlag  = (pick["ATK-Based"] == true)
local matkFlag = (pick["MATK-Based"] == true)
basisWord = basisWord or basisWordFromFlags(atkFlag, matkFlag)
 
sourceKind = "Flat"
sourceVal  = legacyPercentAtLevel(pick, level)
end
end
 
local scalingLines = formatScalingCompactLines(scaling)
local hasSource    = (sourceVal ~= nil and tostring(sourceVal) ~= "")
local hasScaling  = (type(scalingLines) == "table" and #scalingLines > 0)
 
if (not hasSource) and (not hasScaling) then
return nil
end


local seg = mw.text.nowiki(label)
local hasMod = (basisWord ~= nil and tostring(basisWord) ~= "")
if amt then
seg = seg.. " – "..amt
end
table.insert(parts, seg)
end
end
if #parts == 0 then
return nil
end
return table.concat(parts, "<br />")
end


local function formatEvents(list)
local extra = { "skill-source-module", "module-source-type" }
if type(list) ~= "table" or #list == 0 then
table.insert(extra, hasMod and "sv-has-mod" or "sv-no-mod")
return nil
end
local parts = {}
for _, ev in ipairs(list) do
if type(ev) == "table" then
local action = ev.Action or "On event"
local name = ev["Skill Internal Name"] or ev["Skill External Name"] or "Unknown skill"
table.insert(parts, string.format("%s → %s", action, name))
end
end
if #parts == 0 then
return nil
end
return table.concat(parts, "<br />")
end


----------------------------------------------------------------------
if hasSource and (not hasScaling) then
--User matching(for auto lists on class pages)
table.insert(extra, "sv-only-source")
----------------------------------------------------------------------
elseif hasScaling and (not hasSource) then
table.insert(extra, "sv-only-scaling")
end


local function skillMatchesUser(rec, userName)
local wrap = mw.html.create("div")
if type(rec) ~= "table" or not userName or userName == "" then
wrap:addClass("sv-source-grid")
return false
wrap:addClass("sv-compact-root")
end


local users = rec.Users
if hasMod then
if type(users) ~= "table" then
local modCol = wrap:tag("div"):addClass("sv-source-col"):addClass("sv-source-modifier")
return false
modCol:tag("div"):addClass("sv-source-pill"):wikitext("Modifier")
end
modCol:tag("div"):addClass("sv-modifier-value"):wikitext(mw.text.nowiki(basisWord))
end


local userLower = mw.ustring.lower(userName)
if hasSource then
local sourceCol = wrap:tag("div"):addClass("sv-source-col"):addClass("sv-source-main")
sourceCol:tag("div"):addClass("sv-source-pill"):wikitext(mw.text.nowiki(sourceKind or "Source"))
sourceCol:tag("div"):addClass("sv-source-value"):wikitext(sourceVal)
end


local function listHas(list)
if hasScaling then
if type(list) ~= "table" then
local scalingCol = wrap:tag("div"):addClass("sv-source-col"):addClass("sv-source-scaling")
return false
scalingCol:tag("div"):addClass("sv-source-pill"):wikitext("Scaling")
end
for _, v in ipairs(list) do
if type(v) == "string" and mw.ustring.lower(v) == userLower then
return true
end
end
return false
end


if listHas(users.Classes) then return true end
local list = scalingCol:tag("div"):addClass("sv-scaling-list")
if listHas(users.Summons) then return true end
for _, line in ipairs(scalingLines) do
if listHas(users.Monsters) then return true end
list:tag("div"):addClass("sv-scaling-item"):wikitext(mw.text.nowiki(line))
if listHas(users.Events)  then return true end
end
end


return false
return {
inner = tostring(wrap),
classes = extra,
}
end
end


----------------------------------------------------------------------
-- PLUGIN: QuickStats (Hero Module Slot 2) - 3x2 grid (range/area/cost/cast/cd/duration).
--Direct page detection(hide Users on the skill's own page)
-- NOTE: Hits does NOT live here (it lives in SkillType).
----------------------------------------------------------------------
function PLUGINS.QuickStats(rec, ctx)
local level = ctx.level or 1
local maxLevel = ctx.maxLevel or 1
local promo = ctx.promo
 
local mech = (type(rec) == "table" and type(rec.Mechanics) == "table") and rec.Mechanics or {}
local bt  = (type(mech["Basic Timings"]) == "table") and mech["Basic Timings"] or {}
local rc  = (type(mech["Resource Cost"]) == "table") and mech["Resource Cost"] or {}


local function isDirectSkillPage(rec)
local function dash() return "" end
if type(rec) ~= "table" then
return false
end


local pageTitle = mw.title.getCurrentTitle()
-- Range (0 => —)
local pageName = pageTitle and pageTitle.text or ""
local rangeVal = nil
pageName = trim(pageName)
if mech.Range ~= nil and not isNoneLike(mech.Range) then
if not pageName then
local n = toNum(mech.Range)
return false
if n ~= nil then
end
if n ~= 0 then
pageName = mw.ustring.lower(pageName)
rangeVal = mw.text.nowiki(formatUnitValue(mech.Range) or tostring(mech.Range))
end
else
local t = mw.text.trim(tostring(mech.Range))
if t ~= "" and not isNoneLike(t) then
rangeVal = mw.text.nowiki(t)
end
end
end


local ext = trim(rec["External Name"] or rec["Name"] or rec["Display Name"])
-- Area
local internal = trim(rec["Internal Name"] or rec["InternalName"] or rec["InternalID"])
local areaVal = formatAreaSize(mech.Area, maxLevel, level)


if ext and mw.ustring.lower(ext) == pageName then
-- Timings
return true
local castVal = displayFromSeries(seriesFromValuePair(bt["Cast Time"], maxLevel), level)
end
local cdVal  = displayFromSeries(seriesFromValuePair(bt["Cooldown"],  maxLevel), level)
if internal and mw.ustring.lower(internal) == pageName then
local durVal  = displayFromSeries(seriesFromValuePair(bt["Duration"],  maxLevel), level)
return true
end
return false
end


----------------------------------------------------------------------
-- Promote status duration if needed
--Infobox builder(Top band: Level Select + Type)
if (durVal == nil) and type(promo) == "table" and type(promo.durationBlock) == "table" then
----------------------------------------------------------------------
durVal = displayFromSeries(seriesFromValuePair(promo.durationBlock, maxLevel), level)
end


local function buildLevelSelectUI(level, maxLevel)
-- Cost: MP + HP
--JS expectations:
local function labeledSeries(block, label)
--  .sv - level - slider placeholder for the < input type = "range" >
local s = seriesFromValuePair(block, maxLevel)
--  .sv - level - num span for updating the number
if not s then return nil end
local wrap = mw.html.create("div")
local any = false
wrap: addClass("sv-level-ui")
for i, v in ipairs(s) do
if v ~= "" then
s[i] = tostring(v) .. " " .. label
any = true
else
s[i] = ""
end
end
return any and s or nil
end


wrap: tag("div")
local mpS = labeledSeries(rc["Mana Cost"], "MP")
: addClass("sv-level-title")
local hpS = labeledSeries(rc["Health Cost"], "HP")
: wikitext("Level Select")


wrap: tag("div")
local costSeries = {}
: addClass("sv-level-label")
for lv = 1, maxLevel do
: wikitext("Level <span class=\"sv-level-num\">"..tostring(level).. "</span> / "..tostring(maxLevel))
local mp = mpS and mpS[lv] or ""
local hp = hpS and hpS[lv] or ""


wrap: tag("div"): addClass("sv-level-slider")
if mp ~= "" and hp ~= "" then
costSeries[lv] = mp .. " + " .. hp
elseif mp ~= "—" then
costSeries[lv] = mp
elseif hp ~= "—" then
costSeries[lv] = hp
else
costSeries[lv] = "—"
end
end


return tostring(wrap)
local costVal = displayFromSeries(costSeries, level)
end


local function buildTypeTableUI(typeBlock)
local grid = mw.html.create("div")
if type(typeBlock) ~= "table" or next(typeBlock) == nil then
grid:addClass("sv-m4-grid")
return nil
grid:addClass("sv-compact-root")
end


local t = mw.html.create("table")
local function addCell(label, val)
t: addClass("sv-type-table")
local cell = grid:tag("div"):addClass("sv-m4-cell")
cell:tag("div"):addClass("sv-m4-label"):wikitext(mw.text.nowiki(label))
cell:tag("div"):addClass("sv-m4-value"):wikitext(val or dash())
end


local added = false
addCell("Range",    rangeVal)
addCell("Area",      areaVal)
addCell("Cost",      costVal)
addCell("Cast Time", castVal)
addCell("Cooldown",  cdVal)
addCell("Duration",  durVal)


local function valName(x)
return {
if x == nil then return nil end
inner = tostring(grid),
if type(x) == "table" then
classes = "module-quick-stats",
if x.Name and x.Name ~= "" then return tostring(x.Name) end
}
if x.ID and x.ID ~= "" then return tostring(x.ID) end
end
if type(x) == "string" and x ~= "" then return x end
return nil
end
end


local function addRowKV(k, v)
-- PLUGIN: SpecialMechanics (Hero Module Slot 3)
if not v or v == "" then return end
-- Shows:
added = true
--  - Flags (deduped)
local r = t: tag("tr")
--  - Special mechanics (mech.Effects)
r: tag("th"): wikitext(mw.text.nowiki(k)): done()
-- NOTE: Combo lives in SkillType (Hero Bar Slot 2).
r: tag("td"): wikitext(mw.text.nowiki(v)): done()
function PLUGINS.SpecialMechanics(rec, ctx)
end
local level = ctx.level or 1
local maxLevel = ctx.maxLevel or 1


addRowKV("Type", valName(typeBlock["Damage Type"]))-- renamed from "Damage Type"
local mech    = (type(rec) == "table" and type(rec.Mechanics) == "table") and rec.Mechanics or {}
addRowKV("Element", valName(typeBlock["Element Type"]))
local effects = (type(mech.Effects) == "table") and mech.Effects or nil
addRowKV("Target", valName(typeBlock["Target Type"]))
local mods    = (type(rec.Modifiers) == "table") and rec.Modifiers or nil
addRowKV("Cast Type", valName(typeBlock["Cast Type"]))


if not added then
------------------------------------------------------------------
return nil
-- Hits guard (we want Hits ONLY in SkillType)
end
------------------------------------------------------------------
return tostring(t)
local function isHitsKey(name)
end
if not name then return false end
local k = mw.ustring.lower(mw.text.trim(tostring(name)))
return (
k == "hit" or
k == "hits" or
k == "hit count" or
k == "hits count" or
k == "hitcount" or
k == "hitscount"
)
end


local function addTopBand(tbl, levelUI, typeUI)
------------------------------------------------------------------
if not levelUI and not typeUI then
-- Flags (flat, de-duped)
return
------------------------------------------------------------------
end
local flagSet = {}


local row = tbl: tag("tr")
local denyFlags = {
local cell = row: tag("td")
["self centered"] = true,
cell: attr("colspan", 2)
["self-centred"] = true,
cell: addClass("sv-topband-cell")
["bond"] = true,
["combo"] = true,
        ["hybrid"] = true,


--Nested table(what Common.css targets)
-- hits variants
local inner = cell: tag("table")
["hit"] = true,
inner: addClass("sv-topband-table")
["hits"] = true,
["hit count"] = true,
["hits count"] = true,
["hitcount"] = true,
["hitscount"] = true,
}


local tr = inner: tag("tr")
local function allowFlag(name)
if not name then return false end
local k = mw.ustring.lower(mw.text.trim(tostring(name)))
if k == "" then return false end
if denyFlags[k] then return false end
return true
end


if levelUI and typeUI then
local function addFlags(sub)
tr: tag("td"): wikitext(levelUI): done()
if type(sub) ~= "table" then return end
tr: tag("td"): wikitext(typeUI): done()
for k, v in pairs(sub) do
elseif levelUI then
if v and allowFlag(k) then
tr: tag("td"): attr("colspan", 2): wikitext(levelUI): done()
flagSet[tostring(k)] = true
else
end
tr: tag("td"): attr("colspan", 2): wikitext(typeUI): done()
end
end
end
end


local function buildInfobox(rec, opts)
if mods then
opts = opts or { }
addFlags(mods["Movement Modifiers"])
local showUsers = (opts.showUsers ~= false)
addFlags(mods["Combat Modifiers"])
addFlags(mods["Special Modifiers"])
for k, v in pairs(mods) do
if type(v) == "boolean" and v and allowFlag(k) then
flagSet[tostring(k)] = true
end
end
end


local maxLevel = tonumber(rec["Max Level"]) or 1
local flags = {}
if maxLevel < 1 then maxLevel = 1 end
for k, _ in pairs(flagSet) do table.insert(flags, k) end
table.sort(flags)


--Always default to max level
------------------------------------------------------------------
local level = clamp(maxLevel, 1, maxLevel)
-- Special mechanics (name => value)
------------------------------------------------------------------
local mechItems = {}


local root = mw.html.create("table")
if effects then
local keys = {}
for k, _ in pairs(effects) do table.insert(keys, k) end
table.sort(keys)


--IMPORTANT:
for _, name in ipairs(keys) do
--Don't use "wikitable" here; it triggers default borders + Citizen wrapper styling.
-- Skip Hits completely (it belongs in SkillType)
root: addClass("spiritvale-skill-infobox")
if not isHitsKey(name) then
local block = effects[name]
if type(block) == "table" then
-- Also skip if the block's Type is "Hits" (some data may encode it that way)
if not isHitsKey(block.Type) then
local disp = displayFromSeries(seriesFromValuePair(block, maxLevel), level)
local t = trim(block.Type)


--JS hook: treat the table itself as the "card"
local value = disp
root: addClass("sv-skill-card")


--Helpful flag for list - mode overrides(optional, but nice)
-- If Type exists and is distinct, prefix it.
if opts and opts.inList then
if t and not isNoneLike(t) and mw.ustring.lower(t) ~= mw.ustring.lower(tostring(name)) then
root: addClass("sv-skill-inlist")
if value then
end
value = mw.text.nowiki(t) .. ": " .. value
else
value = mw.text.nowiki(t)
end
end


--JS hook: treat the table itself as the "card"
if value then
root: addClass("sv-skill-card")
table.insert(mechItems, { label = tostring(name), value = value })
root: attr("data-max-level", tostring(maxLevel))
end
root: attr("data-level", tostring(level))
end
end
end
end
end


--Top "hero" row
local hasFlags = (#flags > 0)
local icon = rec.Icon
local hasMech  = (#mechItems > 0)
local title = rec["External Name"] or rec.Name or rec["Internal Name"] or "Unknown Skill"
local desc = rec.Description or ""


local headerRow = root: tag("tr")
if (not hasFlags) and (not hasMech) then
headerRow: addClass("spiritvale-infobox-main")
local root = mw.html.create("div")
root:addClass("sv-sm-root")
root:addClass("sv-compact-root")
root:tag("div"):addClass("sv-sm-empty"):wikitext("No Special Mechanics")


local leftCell = headerRow: tag("th")
return {
leftCell: addClass("spiritvale-infobox-main-left")
inner = tostring(root),
classes = "module-special-mechanics",
}
end


local leftInner = leftCell: tag("div")
local count = 0
leftInner: addClass("spiritvale-infobox-main-left-inner")
if hasFlags then count = count + 1 end
if hasMech  then count = count + 1 end


if icon and icon ~= "" then
local root = mw.html.create("div")
leftInner: wikitext(string.format("[[File:%s|80px|link=]]", icon))
root:addClass("sv-sm-root")
end
root:addClass("sv-compact-root")


leftInner: tag("div")
local layout = root:tag("div"):addClass("sv-sm-layout")
: addClass("spiritvale-infobox-title")
layout:addClass("sv-sm-count-" .. tostring(count))
: wikitext(title)


local rightCell = headerRow: tag("td")
-- Column 1: Flags
rightCell: addClass("spiritvale-infobox-main-right")
if hasFlags then
local fcol = layout:tag("div"):addClass("sv-sm-col"):addClass("sv-sm-col-flags")
for _, f in ipairs(flags) do
fcol:tag("div"):addClass("sv-sm-flag"):wikitext(mw.text.nowiki(f))
end
end


local rightInner = rightCell: tag("div")
-- Column 2: Special Mechanics (stacked)
rightInner: addClass("spiritvale-infobox-main-right-inner")
if hasMech then
local mcol = layout:tag("div"):addClass("sv-sm-col"):addClass("sv-sm-col-mech")
for _, it in ipairs(mechItems) do
local one = mcol:tag("div"):addClass("sv-sm-mech")
one:tag("div"):addClass("sv-sm-label"):wikitext(mw.text.nowiki(it.label))
one:tag("div"):addClass("sv-sm-value"):wikitext(it.value or "—")
end
end


if desc ~= "" then
return {
rightInner: tag("div")
inner = tostring(root),
: addClass("spiritvale-infobox-description")
classes = "module-special-mechanics",
: wikitext(string.format("''%s''", desc))
}
end
end


------------------------------------------------------------------
-- PLUGIN: LevelSelector (Hero Module Slot 4) - JS level slider.
--Unified top band(Level Select left, Type list right)
function PLUGINS.LevelSelector(rec, ctx)
------------------------------------------------------------------
local level = ctx.level or 1
local levelUI = buildLevelSelectUI(level, maxLevel)
local maxLevel = ctx.maxLevel or 1
local typeUI = buildTypeTableUI(rec.Type or {})
addTopBand(root, levelUI, typeUI)


------------------------------------------------------------------
local inner = mw.html.create("div")
--Users(kept as rows; caller decides show / hide)
inner:addClass("sv-level-ui")
------------------------------------------------------------------
 
if showUsers then
inner:tag("div")
local users = rec.Users or { }
:addClass("sv-level-label")
addRow(root, "Classes", listToText(users.Classes))
:wikitext("Level <span class=\"sv-level-num\">" .. tostring(level) .. "</span> / " .. tostring(maxLevel))
addRow(root, "Summons", listToText(users.Summons))
addRow(root, "Monsters", listToText(users.Monsters))
addRow(root, "Events", listToText(users.Events))
end


------------------------------------------------------------------
local slider = inner:tag("div"):addClass("sv-level-slider")
--Requirements
------------------------------------------------------------------
local req = rec.Requirements or { }
if (req["Required Skills"] and #req["Required Skills"] > 0)
or(req["Required Weapons"] and #req["Required Weapons"] > 0)
or(req["Required Stances"] and #req["Required Stances"] > 0) then


addSectionHeader(root, "Requirements")
if tonumber(maxLevel) and tonumber(maxLevel) > 1 then
slider:tag("input")
:attr("type", "range")
:attr("min", "1")
:attr("max", tostring(maxLevel))
:attr("value", tostring(level))
:addClass("sv-level-range")
:attr("aria-label", "Skill level select")
else
inner:addClass("sv-level-ui-single")
slider:addClass("sv-level-slider-single")
end


if type(req["Required Skills"]) == "table" and #req["Required Skills"] > 0 then
return {
local skillParts = {}
inner = tostring(inner),
for _, rs in ipairs(req["Required Skills"]) do
classes = "module-level-selector",
local nameReq = rs["Skill External Name"] or rs["Skill Internal Name"] or "Unknown"
}
local lvlReq = rs["Required Level"]
if lvlReq then
table.insert(skillParts, string.format("%s (Lv.%s)", nameReq, lvlReq))
else
table.insert(skillParts, nameReq)
end
end
----------------------------------------------------------------------
-- Generic slot renderers
----------------------------------------------------------------------
-- normalizeResult: normalize plugin return values into {inner, classes}.
local function normalizeResult(res)
if res == nil then return nil end
if type(res) == "string" then
return { inner = res, classes = nil }
end
if type(res) == "table" then
local inner = res.inner
if type(inner) ~= "string" then
inner = (inner ~= nil) and tostring(inner) or ""
end
return { inner = inner, classes = res.classes }
end
return { inner = tostring(res), classes = nil }
end
end
addRow(root, "Required Skills", table.concat(skillParts, ", "))
 
-- safeCallPlugin: pcall wrapper to prevent infobox failure on plugin errors.
local function safeCallPlugin(name, rec, ctx)
        local fn = PLUGINS[name]
        if type(fn) ~= "function" then
                return nil
        end
        local ok, out = pcall(fn, rec, ctx)
        if not ok then
                return nil
        end
        return normalizeResult(out)
end
end


addRow(root, "Required Weapons", listToText(req["Required Weapons"]))
-- isEmptySlotContent: true when a slot has no meaningful content.
addRow(root, "Required Stances", listToText(req["Required Stances"]))
-- NOTE: JS placeholders (sv-dyn spans, slider markup) are considered content.
local function isEmptySlotContent(inner)
        if inner == nil then return true end
 
        local raw = tostring(inner)
 
        -- Guard rails for JS-injected regions.
        for _, pat in ipairs({ "sv%-dyn", "data%-series", "sv%-level%-range", "sv%-level%-slider", "sv%-level%-ui" }) do
                if mw.ustring.find(raw, pat) then
                        return false
                end
        end
 
        local trimmed = mw.text.trim(raw)
        if trimmed == "" or trimmed == "—" then
                return true
        end
 
        local withoutTags = mw.text.trim(mw.ustring.gsub(trimmed, "<[^>]+>", ""))
        return (withoutTags == "" or withoutTags == "")
end
end


------------------------------------------------------------------
-- renderHeroSlot: render a standardized hero slot by plugin assignment.
--Mechanics
local function renderHeroSlot(slotIndex, rec, ctx)
------------------------------------------------------------------
        local pluginName = HERO_SLOT_ASSIGNMENT[slotIndex]
local mech = rec.Mechanics or { }
        if not pluginName then
if next(mech) ~= nil then
                return nil
        end


addRow(root, "Range", formatUnitValue(mech.Range))
        local res = safeCallPlugin(pluginName, rec, ctx)
addRow(root, "Area", formatArea(mech.Area, maxLevel, level))
        if not res or isEmptySlotContent(res.inner) then
                return nil
        end


if mech["Autocast Multiplier"] ~= nil then
        return {
addRow(root, "Autocast Multiplier", tostring(mech["Autocast Multiplier"]))
                inner = res.inner,
                classes = res.classes,
        }
end
end


addRow(root, "Timing", formatTimingBlock(mech["Basic Timings"], maxLevel, level))
----------------------------------------------------------------------
addRow(root, "Resource Cost", formatResourceCost(mech["Resource Cost"], maxLevel, level))
-- UI builders
addRow(root, "Combo", formatCombo(mech.Combo))
----------------------------------------------------------------------
addRow(root, "Special Mechanics", formatMechanicEffects(mech.Effects, maxLevel, level))
 
-- buildHeroSlotsUI: build the standardized 4-row slot grid (2 columns).
local function buildHeroSlotsUI(rec, ctx)
        local grid = mw.html.create("div")
        grid:addClass("sv-slot-grid")
 
        local slots = {}
        for slot = 1, 8 do
                slots[slot] = renderHeroSlot(slot, rec, ctx)
        end
 
        local hasSlots = false
        for _, pair in ipairs({ { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }) do
                local left  = slots[pair[1]]
                local right = slots[pair[2]]
 
                if left or right then
                        hasSlots = true
 
                        if left and right then
                                grid:wikitext(slotBox(pair[1], left.classes, left.inner, { isEmpty = false }))
                                grid:wikitext(slotBox(pair[2], right.classes, right.inner, { isEmpty = false }))
                        elseif left then
                                grid:wikitext(slotBox(pair[1], left.classes, left.inner, { isFull = true }))
                        elseif right then
                                grid:wikitext(slotBox(pair[2], right.classes, right.inner, { isFull = true }))
                        end
                end
        end
 
        if not hasSlots then
                return ""
        end
 
        return tostring(grid)
end
end


------------------------------------------------------------------
-- addHeroSlotsRow: add the standardized slot grid into the infobox table.
--Damage & Scaling
local function addHeroSlotsRow(tbl, slotsUI)
------------------------------------------------------------------
        if not slotsUI or slotsUI == "" then
local dmg = rec.Damage or { }
                return
if next(dmg) ~= nil then
        end


local main = dmg["Main Damage"]
        local row = tbl:tag("tr")
local mainNonHeal, healOnly = {}, {}
        row:addClass("sv-slot-row")


if type(main) == "table" then
        local cell = row:tag("td")
for _, d in ipairs(main) do
        cell:attr("colspan", 2)
if type(d) == "table" and d.Type == "Healing" then
        cell:addClass("sv-slot-cell")
table.insert(healOnly, d)
        cell:wikitext(slotsUI)
else
table.insert(mainNonHeal, d)
end
end
end
end


local flatList = dmg["Flat Damage"]
----------------------------------------------------------------------
local reflList = dmg["Reflect Damage"]
-- Infobox builder
----------------------------------------------------------------------
 
-- buildInfobox: render a single skill infobox.
local function buildInfobox(rec, opts)
opts = opts or {}
local showUsers = (opts.showUsers ~= false)
 
local maxLevel = tonumber(rec["Max Level"]) or 1
if maxLevel < 1 then maxLevel = 1 end
local level = clamp(maxLevel, 1, maxLevel)
 
local ctx = {
maxLevel = maxLevel,
level = level,
nonDamaging = false,
promo = nil,
}
 
-- Non-damaging hides Damage/Element/Hits in SkillType
do
local dmgVal = nil
if type(rec.Type) == "table" then
dmgVal = rec.Type.Damage or rec.Type["Damage Type"]
if type(dmgVal) == "table" then
dmgVal = dmgVal.Name or dmgVal.ID or dmgVal.Value
end
end
ctx.nonDamaging = isNoneLike(dmgVal) or (not skillHasAnyDamage(rec, maxLevel))
end
 
ctx.promo = computeDurationPromotion(rec, maxLevel)
 
local root = mw.html.create("table")
root:addClass("spiritvale-skill-infobox")
root:addClass("sv-skill-card")
root:attr("data-max-level", tostring(maxLevel))
root:attr("data-level", tostring(level))
 
if opts.inList then
root:addClass("sv-skill-inlist")
end
 
local internalId = trim(rec["Internal Name"] or rec.InternalID or rec.ID)
if internalId then
root:attr("data-skill-id", internalId)
end
 
-- Standardized slot grid
addHeroSlotsRow(root, buildHeroSlotsUI(rec, ctx))
 
-- Users (hide on direct skill page)
        if showUsers then
                local users = rec.Users or {}
                addRow(root, "Classes",  listToText(users.Classes),  "sv-row-users", "Users.Classes")
                addRow(root, "Summons",  listToText(users.Summons),  "sv-row-users", "Users.Summons")
                addRow(root, "Monsters", listToText(users.Monsters), "sv-row-users", "Users.Monsters")
                do
                        local eventsList = {}
                        if type(users.Events) == "table" then
                                for _, ev in ipairs(users.Events) do
                                        local name = resolveEventName(ev) or ev
                                        if name ~= nil then
                                                table.insert(eventsList, mw.text.nowiki(tostring(name)))
                                        end
                                end
                        end
                        addRow(root, "Events", listToText(eventsList), "sv-row-users", "Users.Events")
                end
        end
 
        -- Mechanics (keep small extras only)
        local mech = rec.Mechanics or {}
        if next(mech) ~= nil then
                if mech["Autocast Multiplier"] ~= nil then
                        addRow(root, "Autocast Multiplier", tostring(mech["Autocast Multiplier"]), "sv-row-mech", "Mechanics.Autocast Multiplier")
                end
        end
 
-- Legacy damage breakdown (only when Source absent)
if type(rec.Source) ~= "table" then
local dmg = rec.Damage or {}
if next(dmg) ~= nil then
local main = dmg["Main Damage"]
local mainNonHeal, healOnly = {}, {}
 
if type(main) == "table" then
for _, d in ipairs(main) do
if type(d) == "table" and d.Type == "Healing" then
table.insert(healOnly, d)
else
table.insert(mainNonHeal, d)
end
end
end
 
addRow(root, "Main Damage",    formatDamageList(mainNonHeal, maxLevel, level, (#mainNonHeal > 1)), "sv-row-source", "Damage.Main Damage")
addRow(root, "Flat Damage",    formatDamageList(dmg["Flat Damage"], maxLevel, level, false),        "sv-row-source", "Damage.Flat Damage")
addRow(root, "Reflect Damage", formatDamageList(dmg["Reflect Damage"], maxLevel, level, false),    "sv-row-source", "Damage.Reflect Damage")
addRow(root, "Healing",        formatDamageList(healOnly, maxLevel, level, false),                  "sv-row-source", "Damage.Healing")
end
end
 
-- Status rows
local function formatStatusApplications(list, suppressDurationIndex)
if type(list) ~= "table" or #list == 0 then return nil end
 
local parts = {}
for idx, s in ipairs(list) do
if type(s) == "table" then
local typ  = s.Type or s.Scope or "Target"
local name = s["Status External Name"] or s["Status Internal Name"] or "Unknown status"
local seg = tostring(typ) .. " – " .. tostring(name)
local detail = {}
 
if idx ~= suppressDurationIndex and type(s.Duration) == "table" then
local t = valuePairDynamicValueOnly(s.Duration, maxLevel, level)
if t then table.insert(detail, "Duration: " .. t) end
end
 
if type(s.Chance) == "table" then
local t = valuePairDynamicValueOnly(s.Chance, maxLevel, level)
if t then table.insert(detail, "Chance: " .. t) end
end
 
if #detail > 0 then
seg = seg .. " (" .. table.concat(detail, ", ") .. ")"
end
 
table.insert(parts, seg)
end
end
 
return (#parts > 0) and table.concat(parts, "<br />") or nil
end
 
local function formatStatusRemoval(list)
if type(list) ~= "table" or #list == 0 then return nil end


local flatHas = (type(flatList) == "table" and #flatList > 0)
local parts = {}
local reflHas = (type(reflList) == "table" and #reflList > 0)
for _, r in ipairs(list) do
if type(r) == "table" then
local names = r["Status External Name"]
local label


local pureHealing = (#healOnly > 0) and(#mainNonHeal == 0) and(not flatHas) and(not reflHas)
if type(names) == "table" then
label = table.concat(names, ", ")
elseif type(names) == "string" then
label = names
else
label = "Status"
end


addRow(root, "Main Damage", formatDamageList(mainNonHeal, maxLevel, level, (#mainNonHeal > 1)))
local amt = valuePairRawText(r)
addRow(root, "Flat Damage", formatDamageList(flatList, maxLevel, level, false))
amt = amt and mw.text.nowiki(amt) or nil
addRow(root, "Reflect Damage", formatDamageList(reflList, maxLevel, level, false))
addRow(root, "Healing", formatDamageList(healOnly, maxLevel, level, false))


addRow(root, "Scaling", formatScaling(dmg.Scaling, pureHealing and "Healing" or nil))
local seg = mw.text.nowiki(label)
end
if amt then
seg = seg .. " " .. amt
end
table.insert(parts, seg)
end
end
 
return (#parts > 0) and table.concat(parts, "<br />") or nil
end


------------------------------------------------------------------
local suppressIdx = (type(ctx.promo) == "table") and ctx.promo.suppressDurationIndex or nil
--Modifiers
local statusApps = formatStatusApplications(rec["Status Applications"], suppressIdx)
------------------------------------------------------------------
local statusRem  = formatStatusRemoval(rec["Status Removal"])
local modsText = formatModifiers(rec.Modifiers)
if statusApps or statusRem then
if modsText then
addRow(root, "Applies", statusApps, "sv-row-status", "Status Applications")
addRow(root, "Flags", modsText)
addRow(root, "Removes", statusRem,  "sv-row-status", "Status Removal")
end
end


------------------------------------------------------------------
        -- Events
--Status
        local function formatEvents(list)
------------------------------------------------------------------
                if type(list) ~= "table" or #list == 0 then return nil end
local statusApps = formatStatusApplications(rec["Status Applications"], maxLevel, level)
                local parts = {}
local statusRem = formatStatusRemoval(rec["Status Removal"], maxLevel, level)
                for _, ev in ipairs(list) do
if statusApps or statusRem then
                        if type(ev) == "table" then
addRow(root, "Applies", statusApps)
                                local action = resolveDisplayName(ev.Action, "event") or ev.Action or "On event"
addRow(root, "Removes", statusRem)
                                local name  = resolveSkillNameFromEvent(ev)
end
                                table.insert(parts, string.format("%s → %s", mw.text.nowiki(action), mw.text.nowiki(name)))
                        end
                end
                return (#parts > 0) and table.concat(parts, "<br />") or nil
        end


------------------------------------------------------------------
--Events
------------------------------------------------------------------
local eventsText = formatEvents(rec.Events)
local eventsText = formatEvents(rec.Events)
if eventsText then
if eventsText then
addRow(root, "Triggers", eventsText)
addRow(root, "Triggers", eventsText, "sv-row-meta", "Events")
end
end
 
------------------------------------------------------------------
--Notes
------------------------------------------------------------------
if type(rec.Notes) == "table" and #rec.Notes > 0 then
addRow(root, "Notes", table.concat(rec.Notes, "<br />"))
end


return tostring(root)
return tostring(root)
Line 1,097: Line 2,097:


----------------------------------------------------------------------
----------------------------------------------------------------------
--Public: list all skills for a given user / class
-- Public: list all skills for a given user/class
----------------------------------------------------------------------
----------------------------------------------------------------------


function p.listForUser(frame)
function p.listForUser(frame)
local args = getArgs(frame)
local args = getArgs(frame)


local userName = args.user or args[1]
local userName = args.user or args[1]
if not userName or userName == "" then
if not userName or userName == "" then
userName = mw.title.getCurrentTitle().text
userName = mw.title.getCurrentTitle().text
end
end


if not userName or userName == "" then
if not userName or userName == "" then
return "<strong>No user name provided to Skill list.</strong>"
return "<strong>No user name provided to Skill list.</strong>"
end
end


local dataset = getSkills()
local dataset = getSkills()
local matches = {}
local matches = {}


for _, rec in ipairs(dataset.records or {}) do
for _, rec in ipairs(dataset.records or {}) do
if skillMatchesUser(rec, userName) then
if skillMatchesUser(rec, userName) then
table.insert(matches, rec)
table.insert(matches, rec)
end
end
end
end
 
if #matches == 0 then
return string.format("<strong>No skills found for:</strong> %s", mw.text.nowiki(userName))
end
 
local out = {}


if #matches == 0 then
for _, rec in ipairs(matches) do
return string.format(
local title = rec["External Name"] or rec.Name or rec["Internal Name"] or "Unknown Skill"
"<strong>No skills found for:</strong> %s",
mw.text.nowiki(userName)
)
end


--ONE wrapper panel(table - like list mode)
-- List mode: emit a raw H3 heading before each standalone card so TOC/anchors work.
local root = mw.html.create("div")
table.insert(out, string.format("=== %s ===", title))
root: addClass("sv-skill-collection")


for _, rec in ipairs(matches) do
-- List mode cards are independent (no single wrapper container).
--Stable per - skill wrapper so dividers work even under Citizen wrappers
table.insert(out, buildInfobox(rec, { showUsers = false, inList = true }))
local item = root: tag("div"): addClass("sv-skill-item")
end
item: wikitext(buildInfobox(rec, { showUsers = false, inList = true }))
end


return tostring(root)
return table.concat(out, "\n")
end
end


----------------------------------------------------------------------
----------------------------------------------------------------------
--Public: single - skill or auto - list dispatcher
-- Public: single-skill or auto-list dispatcher
----------------------------------------------------------------------
----------------------------------------------------------------------


function p.infobox(frame)
function p.infobox(frame)
local args = getArgs(frame)
local args = getArgs(frame)


local raw1 = args[1]
local raw1 = args[1]
local name = args.name or raw1
local name = args.name or raw1
local id = args.id
local id   = args.id


local rec
local rec
if name and name ~= "" then
if name and name ~= "" then
rec = findSkillByName(name)
rec = findSkillByName(name)
end
end
if not rec and id and id ~= "" then
if not rec and id and id ~= "" then
rec = getSkillById(id)
rec = getSkillById(id)
end
end


if not rec then
if not rec then
local pageTitle = mw.title.getCurrentTitle()
local pageTitle = mw.title.getCurrentTitle()
local pageName = pageTitle and pageTitle.text or ""
local pageName = pageTitle and pageTitle.text or ""


local noExplicitArgs =
local noExplicitArgs =
(not raw1 or raw1 == "") and
(not raw1 or raw1 == "") and
(not args.name or args.name == "") and
(not args.name or args.name == "") and
(not id or id == "")
(not id or id == "")


if noExplicitArgs then
if noExplicitArgs then
return p.listForUser(frame)
return p.listForUser(frame)
end
end


if name and name ~= "" and name == pageName and(not id or id == "") then
if name and name ~= "" and name == pageName and (not id or id == "") then
return p.listForUser(frame)
return p.listForUser(frame)
end
end


local label = name or id or "?"
local label = name or id or "?"
return string.format(
return string.format(
"<strong>Unknown Skill:</strong> %s[[Category:Pages with unknown skill|%s]]",
"<strong>Unknown Skill:</strong> %s[[Category:Pages with unknown skill|%s]]",
mw.text.nowiki(label),
mw.text.nowiki(label),
label
label
)
)
end
end


local showUsers = not isDirectSkillPage(rec)
local showUsers = not isDirectSkillPage(rec)
return buildInfobox(rec, { showUsers = showUsers })
return buildInfobox(rec, { showUsers = showUsers })
end
end


return p
return p

Latest revision as of 18:52, 21 December 2025

Module:GameSkills

Module:GameSkills renders skill data from Data:skills.json into a reusable infobox-style table.

It is intended to be used via a template (for example Template:Skill) so that skills can be embedded on any page without creating individual skill pages.

This module:

  • Loads data via Module:GameDataGameData.loadSkills().
  • Looks up skills primarily by display "Name" (what editors use), with "Internal Name" as a fallback.
  • Builds a table with only the fields that actually exist for that skill.

Data source

Skill data comes from Data:skills.json, which is a JSON page with this top-level structure (see Module:GameData/doc for full details):

{
  "version": "SpiritVale-0.8.2",
  "schema_version": 1,
  "generated_at": "2025-12-12T17:24:05.807675+00:00",
  "records": [
    {
      "Name": "Some Skill",
      "Internal Name": "SomeSkillInternalId",
      "...": "other fields specific to skills"
    }
  ]
}

Each record is a single skill. Important keys:

  • "Name" – the display name (what players and editors will usually see and use).
  • "Internal Name" – the stable ID used internally and available as an optional parameter for power users and tooling.

Output

For a given skill, the module renders a table with the CSS class spiritvale-skill-infobox.

Depending on what exists in the JSON record, the table may include:

  • Header row with skill name (and icon, if present).
  • Icon (from "Icon", as a file name like skill-example.webp).
  • Description.
  • Type information:
    • Damage Type
    • Element Type
    • Target Type
    • Cast Type
  • Max Level.
  • Users:
    • Classes
    • Summons
    • Monsters
    • Events
  • Requirements:
    • Required Skills (with required level)
    • Required Weapons
    • Required Stances
  • Mechanics:
    • Range
    • Cast Time / Cooldown / Duration
    • Mana Cost
  • Damage and scaling:
    • Main Damage (base and per-level, where present)
    • Scaling (stat-based contributions)
  • Status interactions:
    • Status Applications (status name, scope, basic duration/chance info)

Rows are only shown if the underlying field exists in the JSON for that skill.


Public interface

The module exposes a single entry point for templates:

GameSkills.infobox(frame)

This is usually called via #invoke from a template, not directly from pages.

It accepts the following parameters (either passed directly or via a wrapper template):

  • 1 – unnamed parameter; treated as the skill "Name".
  • name – explicit display "Name" of the skill (equivalent to 1).
  • id"Internal Name" of the skill (optional fallback / power use).

Lookup order:

  1. If name or the first unnamed parameter is provided and matches a record’s "Name", that record is used.
  2. Otherwise, if id is provided and matches an "Internal Name", that record is used.
  3. If nothing is found, a small error message is returned and the page is categorized for tracking.

Example direct usage (not recommended; normally use a template):

No skills found for: GameSkills

or:

No skills found for: GameSkills

Template:Skill

The recommended way to use this module is via a small wrapper template, for example:

Template:Skill
 No skills found for: GameSkills

Typical usage on any page:

Bash
Requirements
Required Skills
Axe Mastery (Lv.1)
Delivers a crushing blow with a chance to stun the target.
Level 10 / 10
<input type="range" min="1" max="10" value="10" class="sv-level-range" aria-label="Skill level select" />
Damage
Melee
Element
Neutral
Target
Enemy
Cast
Target
Combo
Ready (4s)
Modifier
Attack
Damage
100%
Scaling
2% Strength
Range
Area
Cost
12 MP
Cast Time
Cooldown
Duration
No Special Mechanics
ClassesWarrior
SummonsWerewolf
MonstersAshrend, Goblin King
AppliesTarget – Stun (Duration: 3s, Chance: 30%)

or, explicitly:

Bash
Requirements
Required Skills
Axe Mastery (Lv.1)
Delivers a crushing blow with a chance to stun the target.
Level 10 / 10
<input type="range" min="1" max="10" value="10" class="sv-level-range" aria-label="Skill level select" />
Damage
Melee
Element
Neutral
Target
Enemy
Cast
Target
Combo
Ready (4s)
Modifier
Attack
Damage
100%
Scaling
2% Strength
Range
Area
Cost
12 MP
Cast Time
Cooldown
Duration
No Special Mechanics
ClassesWarrior
SummonsWerewolf
MonstersAshrend, Goblin King
AppliesTarget – Stun (Duration: 3s, Chance: 30%)

Internal IDs can still be used when needed:

Unknown Skill: Bash_InternalId

This keeps page wikitext simple while centralizing all JSON loading and formatting logic inside Lua.


-- Module:GameSkills
--
-- Phase 6.5+ (Plug-in Slot Architecture)
--
-- Layout:
--   Row 1: Slot 1 + Slot 2 (Icon + SkillType)
--   Row 2: Slot 3 + Slot 4 (Description + Placeholder)
--   Row 3: Slot 5 + Slot 6 (SourceType + QuickStats)
--   Row 4: Slot 7 + Slot 8 (SpecialMechanics + LevelSelector)
--
-- Requires Common.js:
--   - updates .sv-dyn spans via data-series
--   - updates .sv-level-num + data-level on .sv-skill-card
--   - binds to input.sv-level-range inside each card

local GameData = require("Module:GameData")

local p = {}

----------------------------------------------------------------------
-- Data cache
----------------------------------------------------------------------

local skillsCache
local eventsCache

-- getSkills: lazy-load + cache skill dataset from GameData.
local function getSkills()
        if not skillsCache then
                skillsCache = GameData.loadSkills()
        end
        return skillsCache
end

local function getEvents()
        if eventsCache == nil then
                if type(GameData.loadEvents) == "function" then
                        eventsCache = GameData.loadEvents()
                else
                        eventsCache = false
                end
        end

        return eventsCache
end

----------------------------------------------------------------------
-- Small utilities
----------------------------------------------------------------------

-- getArgs: read args from parent frame when invoked from a template.
local function getArgs(frame)
	local parent = frame:getParent()
	return (parent and parent.args) or frame.args
end

-- trim: normalize strings (nil if empty).
local function trim(s)
	if type(s) ~= "string" then
		return nil
	end
	s = mw.text.trim(s)
	return (s ~= "" and s) or nil
end

-- toNum: convert common scalar/table forms to a Lua number.
local function toNum(v)
	if type(v) == "number" then
		return v
	end
	if type(v) == "string" then
		return tonumber(v)
	end
	if type(v) == "table" and v.Value ~= nil then
		return toNum(v.Value)
	end
	return nil
end

-- clamp: clamp a number into [lo, hi].
local function clamp(n, lo, hi)
	if type(n) ~= "number" then
		return lo
	end
	if n < lo then return lo end
	if n > hi then return hi end
	return n
end

-- fmtNum: consistent number formatting (trim trailing zeros).
local function fmtNum(n)
	if type(n) ~= "number" then
		return (n ~= nil) and tostring(n) or nil
	end

	if math.abs(n - math.floor(n)) < 1e-9 then
		return tostring(math.floor(n))
	end

	local s = string.format("%.4f", n)
	s = mw.ustring.gsub(s, "0+$", "")
	s = mw.ustring.gsub(s, "%.$", "")
	return s
end

-- listToText: join an array into a readable string.
local function listToText(list, sep)
        if type(list) ~= "table" or #list == 0 then
                return nil
        end
        return table.concat(list, sep or ", ")
end

local function resolveDisplayName(v, kind)
        if v == nil then return nil end

        local function firstString(keys, source)
                for _, key in ipairs(keys) do
                        local candidate = source[key]
                        if type(candidate) == "string" and candidate ~= "" then
                                return candidate
                        end
                end
                return nil
        end

        if type(v) == "table" then
                local primaryKeys = { "External Name", "Display Name", "Name" }
                local extendedKeys = { "Skill External Name", "Status External Name" }
                local internalKeys = { "Internal Name", "Internal ID", "ID", "InternalID", "Skill Internal Name", "InternalID" }

                return firstString(primaryKeys, v)
                        or firstString(extendedKeys, v)
                        or firstString(internalKeys, v)
        end

        if type(v) == "string" then
                if kind == "event" then
                        local events = getEvents()
                        if events and events.byId and events.byId[v] then
                                local mapped = resolveDisplayName(events.byId[v], "event")
                                if mapped then
                                        return mapped
                                end
                        end
                end

                return v
        end

        return tostring(v)
end

local function resolveEventName(v)
        local resolved = resolveDisplayName(v, "event")
        if type(resolved) == "string" then
                return resolved
        end
        return (resolved ~= nil) and tostring(resolved) or nil
end

local function resolveSkillNameFromEvent(ev)
        if type(ev) ~= "table" then
                return resolveDisplayName(ev, "skill") or "Unknown skill"
        end

        local displayKeys = {
                "Skill External Name",
                "External Name",
                "Display Name",
                "Name",
                "Skill Name",
        }

        for _, key in ipairs(displayKeys) do
                local candidate = resolveDisplayName(ev[key], "skill")
                if candidate then
                        return candidate
                end
        end

        local internalKeys = {
                "Skill Internal Name",
                "Skill ID",
                "Internal Name",
                "Internal ID",
                "ID",
        }

        for _, key in ipairs(internalKeys) do
                local candidate = ev[key]
                if type(candidate) == "string" and candidate ~= "" then
                        return candidate
                end
        end

        return "Unknown skill"
end

-- isNoneLike: treat common "none" spellings as empty.
local function isNoneLike(v)
	if v == nil then return true end
	local s = mw.text.trim(tostring(v))
	if s == "" then return true end
	s = mw.ustring.lower(s)
	return (s == "none" or s == "no" or s == "n/a" or s == "na" or s == "null")
end

-- addRow: add a standard <tr><th>Label</th><td>Value</td></tr> row.
local function addRow(tbl, label, value, rowClass, dataKey)
	if value == nil or value == "" then
		return
	end

	local row = tbl:tag("tr")
	row:addClass("sv-row")
	if rowClass then row:addClass(rowClass) end
	if dataKey then row:attr("data-field", dataKey) end

	row:tag("th"):wikitext(label):done()
	row:tag("td"):wikitext(value):done()
end

-- formatUnitValue: format {Value, Unit} blocks (or scalar) for display.
local function formatUnitValue(v)
	if type(v) == "table" and v.Value ~= nil then
		local unit = v.Unit
		local val  = v.Value

		if unit == "percent_decimal" or unit == "percent_whole" or unit == "percent" then
			return tostring(val) .. "%"
		elseif unit == "seconds" then
			return tostring(val) .. "s"
		elseif unit == "meters" then
			return tostring(val) .. "m"
		elseif unit == "tiles" then
			return tostring(val) .. " tiles"
		elseif unit and unit ~= "" then
			return tostring(val) .. " " .. tostring(unit)
		else
			return tostring(val)
		end
	end

	return (v ~= nil) and tostring(v) or nil
end

----------------------------------------------------------------------
-- Dynamic spans (JS-driven)
----------------------------------------------------------------------

-- dynSpan: render a JS-updated span for a level series.
local function dynSpan(series, level)
	if type(series) ~= "table" or #series == 0 then
		return nil
	end

	level = clamp(level or #series, 1, #series)

	local span = mw.html.create("span")
	span:addClass("sv-dyn")
	span:attr("data-series", mw.text.jsonEncode(series))
	span:wikitext(mw.text.nowiki(series[level] or ""))

	return tostring(span)
end

-- isFlatList: true if all values in list are identical.
local function isFlatList(list)
	if type(list) ~= "table" or #list == 0 then
		return false
	end
	local first = tostring(list[1])
	for i = 2, #list do
		if tostring(list[i]) ~= first then
			return false
		end
	end
	return true
end

-- isNonZeroScalar: detect if a value is present and not effectively zero.
local function isNonZeroScalar(v)
	if v == nil then return false end
	if type(v) == "number" then return v ~= 0 end
	if type(v) == "string" then
		local n = tonumber(v)
		if n == nil then return v ~= "" end
		return n ~= 0
	end
	if type(v) == "table" and v.Value ~= nil then
		return isNonZeroScalar(v.Value)
	end
	return true
end

-- isZeroish: aggressively treat common “zero” text forms as zero.
local function isZeroish(v)
	if v == nil then return true end
	if type(v) == "number" then return v == 0 end
	if type(v) == "table" and v.Value ~= nil then
		return isZeroish(v.Value)
	end

	local s = mw.text.trim(tostring(v))
	if s == "" then return true end
	if s == "0" or s == "0.0" or s == "0.00" then return true end
	if s == "0s" or s == "0 s" then return true end
	if s == "0m" or s == "0 m" then return true end
	if s == "0%" or s == "0 %" then return true end

	local n = tonumber((mw.ustring.gsub(s, "[^0-9%.%-]", "")))
	return (n ~= nil and n == 0)
end

-- valuePairRawText: render Base/Per Level blocks into readable text (fallback).
local function valuePairRawText(block)
	if type(block) ~= "table" then
		return nil
	end

	local base = block.Base
	local per  = block["Per Level"]

	if type(per) == "table" then
		if #per == 0 then
			return formatUnitValue(base)
		end
		if isFlatList(per) then
			return formatUnitValue(base) or tostring(per[1])
		end

		local vals = {}
		for _, v in ipairs(per) do
			table.insert(vals, formatUnitValue(v) or tostring(v))
		end
		return (#vals > 0) and table.concat(vals, " / ") or nil
	end

	local baseText = formatUnitValue(base)
	local perText  = formatUnitValue(per)

	if baseText and perText and isNonZeroScalar(per) then
		return string.format("%s (Per Level: %s)", baseText, perText)
	end

	return baseText or perText
end

-- valuePairDynamicValueOnly: render Base/Per Level blocks using dyn spans where possible.
local function valuePairDynamicValueOnly(block, maxLevel, level)
	if type(block) ~= "table" then
		return nil
	end

	local base = block.Base
	local per  = block["Per Level"]

	if type(per) == "table" then
		if #per == 0 then
			local baseText = formatUnitValue(base)
			return baseText and mw.text.nowiki(baseText) or nil
		end

		if isFlatList(per) then
			local one  = formatUnitValue(per[1]) or tostring(per[1])
			local show = formatUnitValue(base) or one
			return show and mw.text.nowiki(show) or nil
		end

		local series = {}
		for _, v in ipairs(per) do
			table.insert(series, formatUnitValue(v) or tostring(v))
		end
		return dynSpan(series, level)
	end

	local txt = valuePairRawText(block)
	return txt and mw.text.nowiki(txt) or nil
end

----------------------------------------------------------------------
-- Lookups
----------------------------------------------------------------------

-- getSkillById: locate a skill by internal ID.
local function getSkillById(id)
	id = trim(id)
	if not id then return nil end
	local dataset = getSkills()
	return (dataset.byId or {})[id]
end

-- findSkillByName: locate a skill by external/display name.
local function findSkillByName(name)
	name = trim(name)
	if not name then return nil end

	local dataset = getSkills()
	local byName = dataset.byName or {}

	if byName[name] then
		return byName[name]
	end

	for _, rec in ipairs(dataset.records or {}) do
		if type(rec) == "table" then
			if rec["External Name"] == name or rec.Name == name or rec["Display Name"] == name then
				return rec
			end
		end
	end

	return nil
end

----------------------------------------------------------------------
-- Legacy damage helpers
----------------------------------------------------------------------

-- basisLabel: label ATK/MATK basis in legacy damage blocks.
local function basisLabel(entry, isHealing)
	if isHealing then
		return "Healing"
	end

	local atk  = entry and entry["ATK-Based"]
	local matk = entry and entry["MATK-Based"]

	if atk and matk then
		return "Attack/Magic Attack"
	elseif atk then
		return "Attack"
	elseif matk then
		return "Magic Attack"
	end

	return "Damage"
end

-- formatDamageEntry: legacy percent damage formatting (dynamic by level).
local function formatDamageEntry(entry, maxLevel, level)
	if type(entry) ~= "table" then
		return nil
	end

	local isHealing = (entry.Type == "Healing")
	local basis = isHealing and "Healing" or basisLabel(entry, false)

	local baseRaw = entry["Base %"]
	local perRaw  = entry["Per Level %"]

	local baseN = toNum(baseRaw)
	local perN  = toNum(perRaw)

	local function baseIsPresent()
		if baseN ~= nil then
			return baseN ~= 0
		end
		if baseRaw ~= nil then
			local s = tostring(baseRaw)
			return (s ~= "" and s ~= "0" and s ~= "0.0" and s ~= "0.00")
		end
		return false
	end

	local baseText
	if baseIsPresent() then
		baseText = (baseN ~= nil) and (fmtNum(baseN) .. "%") or (tostring(baseRaw) .. "%")
	end

	if perN == nil or perN == 0 or not maxLevel or maxLevel <= 0 then
		return baseText and mw.text.nowiki(baseText .. " " .. basis) or nil
	end

	local series = {}
	for lv = 1, maxLevel do
		local perPart = perN * lv

		if baseText and baseN ~= nil then
			local total = baseN + perPart
			table.insert(series, string.format("%s%% %s", fmtNum(total), basis))
		elseif baseText then
			table.insert(series, string.format("%s + %s%% %s", baseText, fmtNum(perPart), basis))
		else
			table.insert(series, string.format("%s%% %s", fmtNum(perPart), basis))
		end
	end

	return dynSpan(series, level)
end

-- formatDamageList: render a list of legacy damage entries into <br/> blocks.
local function formatDamageList(list, maxLevel, level, includeTypePrefix)
	if type(list) ~= "table" or #list == 0 then
		return nil
	end

	local parts = {}
	for _, d in ipairs(list) do
		if type(d) == "table" then
			local txt = formatDamageEntry(d, maxLevel, level)
			if txt then
				if includeTypePrefix and d.Type and d.Type ~= "" then
					table.insert(parts, mw.text.nowiki(tostring(d.Type) .. ": ") .. txt)
				else
					table.insert(parts, txt)
				end
			end
		end
	end

	return (#parts > 0) and table.concat(parts, "<br />") or nil
end

----------------------------------------------------------------------
-- Users matching
----------------------------------------------------------------------

-- skillMatchesUser: check if a skill is used by a specific class/monster/summon/event.
local function skillMatchesUser(rec, userName)
	if type(rec) ~= "table" or not userName or userName == "" then
		return false
	end

	local users = rec.Users
	if type(users) ~= "table" then
		return false
	end

	local userLower = mw.ustring.lower(userName)

	local function listHas(list)
		if type(list) ~= "table" then
			return false
		end
		for _, v in ipairs(list) do
			if type(v) == "string" and mw.ustring.lower(v) == userLower then
				return true
			end
		end
		return false
	end

	return listHas(users.Classes) or listHas(users.Summons) or listHas(users.Monsters) or listHas(users.Events)
end

----------------------------------------------------------------------
-- Hide Users on direct skill pages
----------------------------------------------------------------------

-- isDirectSkillPage: hide Users rows when viewing the skill page itself.
local function isDirectSkillPage(rec)
	if type(rec) ~= "table" then
		return false
	end

	local pageTitle = mw.title.getCurrentTitle()
	local pageName  = pageTitle and pageTitle.text or ""
	pageName = trim(pageName)
	if not pageName then
		return false
	end

	pageName = mw.ustring.lower(pageName)

	local ext = trim(rec["External Name"] or rec.Name or rec["Display Name"])
	local internal = trim(rec["Internal Name"] or rec.InternalName or rec.InternalID)

	return (ext and mw.ustring.lower(ext) == pageName) or (internal and mw.ustring.lower(internal) == pageName) or false
end

----------------------------------------------------------------------
-- Slot config (edit these tables only to rearrange layout)
----------------------------------------------------------------------

local HERO_SLOT_ASSIGNMENT = {
	[1] = "IconName",
	[2] = "Description",
	[3] = "LevelSelector",
	[4] = "SkillType",
	[5] = "SourceType",
	[6] = "QuickStats",
	[7] = "SpecialMechanics",
	[8] = "Placeholder",
}

----------------------------------------------------------------------
-- Slot scaffolds
----------------------------------------------------------------------

-- slotBox: standardized wrapper for all hero card slots.
local function slotBox(slot, extraClasses, innerHtml, opts)
	opts = opts or {}

	local box = mw.html.create("div")
	box:addClass("sv-slot")
	box:addClass("sv-slot--" .. tostring(slot))
	box:attr("data-hero-slot", tostring(slot))

	if opts.isFull then
		box:addClass("sv-slot--full")
	end

	if extraClasses then
		if type(extraClasses) == "string" then
			box:addClass(extraClasses)
		elseif type(extraClasses) == "table" then
			for _, c in ipairs(extraClasses) do box:addClass(c) end
		end
	end

	if opts.isEmpty then
		box:addClass("sv-slot--empty")
	end

	local body = box:tag("div"):addClass("sv-slot__body")
	if innerHtml and innerHtml ~= "" then
		body:wikitext(innerHtml)
	end

	return tostring(box)
end

----------------------------------------------------------------------
-- Shared helpers (Source + QuickStats + SpecialMechanics)
----------------------------------------------------------------------

-- formatScalingCompactLines: build compact “Scaling” lines for SourceType.
local function formatScalingCompactLines(scaling)
	if type(scaling) ~= "table" then
		return {}
	end

	local list = scaling
	if #list == 0 then
		if scaling.Percent ~= nil or scaling["Scaling ID"] or scaling["Scaling Name"] then
			list = { scaling }
		else
			return {}
		end
	end

	local out = {}
	for _, s in ipairs(list) do
		if type(s) == "table" then
			local stat = s["Scaling Name"] or s["Scaling ID"] or "Unknown"
			local pct  = s.Percent
			local pctN = toNum(pct)

			if pctN ~= nil and pctN ~= 0 then
				table.insert(out, string.format("%s%% %s", fmtNum(pctN), stat))
			elseif pct ~= nil and tostring(pct) ~= "" and tostring(pct) ~= "0" then
				table.insert(out, string.format("%s%% %s", tostring(pct), stat))
			end
		end
	end

	return out
end

-- basisWordFromFlags: compute “Attack/Magic/Hybrid” from ATK/MATK booleans.
local function basisWordFromFlags(atkFlag, matkFlag)
	if atkFlag and matkFlag then
		return "Hybrid"
	elseif atkFlag then
		return "Attack"
	elseif matkFlag then
		return "Magic Attack"
	end
	return nil
end

-- legacyPercentAtLevel: compute “Base% + PerLevel%*level” for legacy entries.
local function legacyPercentAtLevel(entry, level)
	if type(entry) ~= "table" then
		return nil
	end

	local baseRaw = entry["Base %"]
	local perRaw  = entry["Per Level %"]
	local baseN   = toNum(baseRaw)
	local perN    = toNum(perRaw)

	if perN ~= nil and perN ~= 0 then
		local total = (baseN or 0) + (perN * level)
		return fmtNum(total) .. "%"
	end

	if baseN ~= nil then
		return fmtNum(baseN) .. "%"
	end
	if baseRaw ~= nil and tostring(baseRaw) ~= "" then
		return tostring(baseRaw) .. "%"
	end

	return nil
end

-- seriesFromValuePair: normalize Base/Per Level blocks into a level-indexed series.
local function seriesFromValuePair(block, maxLevel)
	if type(block) ~= "table" then
		return nil
	end

	local base = block.Base
	local per  = block["Per Level"]

	local function pickUnit(v)
		if type(v) == "table" and v.Unit and v.Unit ~= "" then
			return v.Unit
		end
		return nil
	end
	local unit = pickUnit(base) or pickUnit(per)

	local function fmtAny(v)
		local t = formatUnitValue(v)
		return t and tostring(t) or (v ~= nil and tostring(v) or nil)
	end

	local series = {}

	-- Expanded per-level series (wikiprep)
	if type(per) == "table" and #per > 0 then
		for lv = 1, maxLevel do
			local raw = per[lv] or per[#per]
			local one = fmtAny(raw)
			if one == nil or isZeroish(raw) or isZeroish(one) then
				one = "—"
			end
			series[lv] = one
		end
		return series
	end

	-- Empty per-list -> base only
	if type(per) == "table" and #per == 0 then
		local one = fmtAny(base)
		if one == nil or isZeroish(base) or isZeroish(one) then
			one = "—"
		end
		for lv = 1, maxLevel do
			series[lv] = one
		end
		return series
	end

	-- Scalar per -> compute base + per*level (fallback)
	local baseN = toNum(base) or 0
	local perN  = toNum(per)

	if perN ~= nil then
		for lv = 1, maxLevel do
			local total = baseN + (perN * lv)
			local v = unit and { Value = total, Unit = unit } or total
			local one = fmtAny(v)
			if one == nil or total == 0 or isZeroish(one) then
				one = "—"
			end
			series[lv] = one
		end
		return series
	end

	-- Base-only scalar
	local raw = (base ~= nil) and base or per
	local one = fmtAny(raw)
	if one == nil then
		return nil
	end
	if isZeroish(raw) or isZeroish(one) then
		one = "—"
	end
	for lv = 1, maxLevel do
		series[lv] = one
	end
	return series
end

-- displayFromSeries: render a series as fixed text or dynSpan (nil if all “—”).
local function displayFromSeries(series, level)
	if type(series) ~= "table" or #series == 0 then
		return nil
	end

	local any = false
	for _, v in ipairs(series) do
		if v ~= "—" then
			any = true
			break
		end
	end
	if not any then
		return nil
	end

	if isFlatList(series) then
		return mw.text.nowiki(series[1])
	end
	return dynSpan(series, level)
end

-- formatAreaSize: human readable area sizing for QuickStats.
-- Shows: "<Area Size> (<number>)" e.g. "Medium (4)"
local function formatAreaSize(area, maxLevel, level)
	if type(area) ~= "table" then
		return nil
	end

	-- Helper: pull a number from scalar/unit/valuepair-ish things.
	local function extractNumber(v)
		if v == nil then return nil end

		-- Unit block {Value, Unit}
		if type(v) == "table" and v.Value ~= nil then
			local n = toNum(v.Value)
			return n
		end

		-- ValuePair {Base, Per Level} -> prefer Base at current level if series exists
		if type(v) == "table" and (v.Base ~= nil or v["Per Level"] ~= nil) then
			local s = seriesFromValuePair(v, maxLevel or 1)
			if type(s) == "table" and #s > 0 then
				local idx = clamp(level or 1, 1, #s)
				local txt = s[idx]
				if txt and txt ~= "—" then
					-- try parse numeric from string (e.g. "4 tiles" -> 4)
					local num = tonumber((mw.ustring.gsub(tostring(txt), "[^0-9%.%-]", "")))
					return num
				end
			end
			return nil
		end

		-- Plain scalar
		if type(v) == "number" then return v end
		if type(v) == "string" then
			local num = tonumber((mw.ustring.gsub(mw.text.trim(v), "[^0-9%.%-]", "")))
			return num
		end

		return nil
	end

	-- 1) Read Area Size label/name
	local rawSize = area["Area Size"]
	if rawSize == nil then
		return nil
	end

	local sizeName = nil
	if type(rawSize) == "table" then
		sizeName = rawSize.Name or rawSize.ID or rawSize.Value
	elseif type(rawSize) == "string" then
		sizeName = rawSize
	elseif type(rawSize) == "number" then
		-- uncommon; treat as numeric-only label
		sizeName = tostring(rawSize)
	end

	sizeName = sizeName and mw.text.trim(tostring(sizeName)) or nil
	if not sizeName or sizeName == "" or isNoneLike(sizeName) then
		return nil
	end

	-- 2) Find the numeric “exact number” to append
	-- Prefer the explicit Area Distance block, then fall back to other known numeric keys.
	local num = nil

	local dist = area["Area Distance"]
	if type(dist) == "table" then
		-- Prefer Effective Distance if present and non-zero, else Base
		num = extractNumber(dist["Effective Distance"]) or extractNumber(dist.Effective) or extractNumber(dist["Effective"])
		if not num or num == 0 then
			num = extractNumber(dist.Base) or extractNumber(dist["Base"])
		end
	end

	if not num or num == 0 then
		num =
			extractNumber(area["Area Value"]) or
			extractNumber(area["Area Size Value"]) or
			extractNumber(area["Area Number"]) or
			extractNumber(area["Area Radius"])
	end

	-- 3) Render
	-- If size already contains parentheses, assume it already includes the numeric.
	if mw.ustring.find(sizeName, "%(") then
		return mw.text.nowiki(sizeName)
	end

	if num and num ~= 0 then
		return mw.text.nowiki(string.format("%s (%s)", sizeName, fmtNum(num)))
	end

	return mw.text.nowiki(sizeName)
end

-- skillHasAnyDamage: determine if a skill has any meaningful damage (for non-damaging rules).
local function skillHasAnyDamage(rec, maxLevel)
	if type(rec.Source) == "table" then
		local s = seriesFromValuePair(rec.Source, maxLevel)
		if s then
			for _, v in ipairs(s) do
				if v ~= "—" then return true end
			end
		end
	end

	if type(rec.Damage) == "table" then
		local dmg = rec.Damage
		for _, key in ipairs({ "Main Damage", "Flat Damage", "Reflect Damage" }) do
			local lst = dmg[key]
			if type(lst) == "table" and #lst > 0 then
				return true
			end
		end
	end

	return false
end

-- computeDurationPromotion: promote status-duration into QuickStats when a skill is non-damaging.
local function computeDurationPromotion(rec, maxLevel)
	if type(rec) ~= "table" then return nil end
	if skillHasAnyDamage(rec, maxLevel) then return nil end

	local mech = (type(rec.Mechanics) == "table") and rec.Mechanics or {}
	local bt   = (type(mech["Basic Timings"]) == "table") and mech["Basic Timings"] or {}
	local durS = seriesFromValuePair(bt["Duration"], maxLevel)

	if durS ~= nil then
		local any = false
		for _, v in ipairs(durS) do
			if v ~= "—" then any = true break end
		end
		if any then
			return nil
		end
	end

	local apps = rec["Status Applications"]
	if type(apps) ~= "table" then return nil end

	for idx, app in ipairs(apps) do
		if type(app) == "table" and type(app.Duration) == "table" then
			local s = seriesFromValuePair(app.Duration, maxLevel)
			if s then
				for _, v in ipairs(s) do
					if v ~= "—" then
						return {
							durationBlock = app.Duration,
							suppressDurationIndex = idx,
						}
					end
				end
			end
		end
	end

	return nil
end

----------------------------------------------------------------------
-- Plug-ins
----------------------------------------------------------------------

local PLUGINS = {}

-- PLUGIN: IconName (Hero Bar Slot 1) - icon + name.
function PLUGINS.IconName(rec, ctx)
	local icon  = rec.Icon
	local title = rec["External Name"] or rec.Name or rec["Internal Name"] or "Unknown Skill"

	local notesList = {}
	if type(rec.Notes) == "table" then
		for _, note in ipairs(rec.Notes) do
			local n = trim(note)
			if n then
				table.insert(notesList, mw.text.nowiki(n))
			end
		end
	elseif type(rec.Notes) == "string" then
		local n = trim(rec.Notes)
		if n then
			notesList = { mw.text.nowiki(n) }
		end
	end

	local req = rec.Requirements or {}
	local reqSkillsRaw = (type(req["Required Skills"]) == "table") and req["Required Skills"] or {}
	local reqWeaponsRaw = (type(req["Required Weapons"]) == "table") and req["Required Weapons"] or {}
	local reqStancesRaw = (type(req["Required Stances"]) == "table") and req["Required Stances"] or {}

	local reqSkills = {}
	for _, rs in ipairs(reqSkillsRaw) do
		if type(rs) == "table" then
			local nameReq = rs["Skill External Name"] or rs["Skill Internal Name"] or "Unknown"
			local lvlReq  = rs["Required Level"]
			if lvlReq then
				table.insert(reqSkills, string.format("%s (Lv.%s)", mw.text.nowiki(nameReq), mw.text.nowiki(tostring(lvlReq))))
			else
				table.insert(reqSkills, mw.text.nowiki(nameReq))
			end
		end
	end

	local reqWeapons = {}
	for _, w in ipairs(reqWeaponsRaw) do
		local wn = trim(w)
		if wn then table.insert(reqWeapons, mw.text.nowiki(wn)) end
	end

	local reqStances = {}
	for _, s in ipairs(reqStancesRaw) do
		local sn = trim(s)
		if sn then table.insert(reqStances, mw.text.nowiki(sn)) end
	end

	local hasNotes = (#notesList > 0)
	local hasReq = (#reqSkills > 0) or (#reqWeapons > 0) or (#reqStances > 0)


	local wrap = mw.html.create("div")
	wrap:addClass("sv-herobar-1-wrap")
	wrap:addClass("sv-tip-scope")

	local iconBox = wrap:tag("div")
	iconBox:addClass("sv-herobar-icon")

	if icon and icon ~= "" then
		iconBox:wikitext(string.format("[[File:%s|80px|link=]]", icon))
	end

	local textBox = wrap:tag("div")
	textBox:addClass("sv-herobar-text")

	local titleRow = textBox:tag("div")
	titleRow:addClass("sv-herobar-title-row")

	local titleBox = titleRow:tag("div")
	titleBox:addClass("spiritvale-infobox-title")
	titleBox:wikitext(title)

	if hasNotes then
		local notesBtn = mw.html.create("span")
		notesBtn:addClass("sv-tip-btn sv-tip-btn--notes")
		notesBtn:attr("role", "button")
		notesBtn:attr("tabindex", "0")
		notesBtn:attr("data-sv-tip", "notes")
		notesBtn:attr("aria-label", "Notes")
		notesBtn:attr("aria-expanded", "false")
		notesBtn:tag("span"):addClass("sv-ico sv-ico--info"):attr("aria-hidden", "true"):wikitext("i")
		titleRow:node(notesBtn)
	end

	if hasReq then
		local pillRow = wrap:tag("div")
		pillRow:addClass("sv-pill-row")
		pillRow:addClass("sv-pill-row--req")
		local pill = pillRow:tag("span")
		pill:addClass("sv-pill sv-pill--req sv-tip-btn")
		pill:attr("role", "button")
		pill:attr("tabindex", "0")
		pill:attr("data-sv-tip", "req")
		pill:attr("aria-label", "Requirements")
		pill:attr("aria-expanded", "false")
		pill:wikitext("Requirements")
	end

	if hasNotes then
		local notesContent = wrap:tag("div")
		notesContent:addClass("sv-tip-content")
		notesContent:attr("data-sv-tip-content", "notes")
		notesContent:tag("div"):addClass("sv-tip-title"):wikitext("Notes")
		notesContent:tag("div"):wikitext(table.concat(notesList, "<br />"))
	end

	if hasReq then
		local reqContent = wrap:tag("div")
		reqContent:addClass("sv-tip-content")
		reqContent:attr("data-sv-tip-content", "req")

		if #reqSkills > 0 then
			local section = reqContent:tag("div")
			section:addClass("sv-tip-section")
			section:tag("span"):addClass("sv-tip-label"):wikitext("Required Skills")
			section:tag("div"):wikitext(table.concat(reqSkills, "<br />"))
		end

		if #reqWeapons > 0 then
			local section = reqContent:tag("div")
			section:addClass("sv-tip-section")
			section:tag("span"):addClass("sv-tip-label"):wikitext("Required Weapons")
			section:tag("div"):wikitext(table.concat(reqWeapons, ", "))
		end

		if #reqStances > 0 then
			local section = reqContent:tag("div")
			section:addClass("sv-tip-section")
			section:tag("span"):addClass("sv-tip-label"):wikitext("Required Stances")
			section:tag("div"):wikitext(table.concat(reqStances, ", "))
		end
	end

	return {
		inner = tostring(wrap),
		classes = "module-icon-name",
	}
end

-- PLUGIN: SkillType (Hero Bar Slot 2) - 2 rows x 3 cells (desktop + mobile).
-- Rules:
--   - If skill is non-damaging, hide Damage/Element/Hits.
--   - If Hits is empty, hide Hits.
--   - If Combo is empty, hide Combo.
-- Ordering:
--   - Desktop: Damage, Element, Hits, Target, Cast, Combo
--   - Mobile:  Damage, Element, Target, Cast, Hits, Combo (CSS reorder)
function PLUGINS.SkillType(rec, ctx)
	local typeBlock = (type(rec.Type) == "table") and rec.Type or {}
	local mech      = (type(rec.Mechanics) == "table") and rec.Mechanics or {}

	local level    = ctx.level or 1
	local maxLevel = ctx.maxLevel or 1

	local hideDamageBundle = (ctx.nonDamaging == true)

	-- valName: extract a display string from typical {Name/ID/Value} objects.
	-- NOTE: Includes number support so Hits=2 (number) doesn't get dropped.
	local function valName(x)
		if x == nil then return nil end
		if type(x) == "table" then
			if x.Name and x.Name ~= "" then return tostring(x.Name) end
			if x.ID and x.ID ~= "" then return tostring(x.ID) end
			if x.Value ~= nil then return tostring(x.Value) end
		end
		if type(x) == "number" then
			return tostring(x)
		end
		if type(x) == "string" and x ~= "" then
			return x
		end
		return nil
	end

	-- hitsDisplay: find + render Hits from multiple possible structured locations.
	local function hitsDisplay()
		local effects = (type(mech.Effects) == "table") and mech.Effects or {}

		local h =
			typeBlock.Hits or typeBlock["Hits"] or typeBlock["Hit Count"] or typeBlock["Hits Count"] or
			mech.Hits or mech["Hits"] or mech["Hit Count"] or mech["Hits Count"] or
			effects.Hits or effects["Hits"] or effects["Hit Count"] or effects["Hits Count"] or
			rec.Hits or rec["Hits"]

		if h == nil or isNoneLike(h) then
			return nil
		end

		-- ValuePair-style table (Base/Per Level) => dynamic series
		if type(h) == "table" then
			if h.Base ~= nil or h["Per Level"] ~= nil or type(h["Per Level"]) == "table" then
				return displayFromSeries(seriesFromValuePair(h, maxLevel), level)
			end

			-- Unit block {Value, Unit}
			if h.Value ~= nil then
				local t = formatUnitValue(h)
				return t and mw.text.nowiki(t) or nil
			end

			-- Fallback name extraction
			local function valName(x)
				if x == nil then return nil end
				if type(x) == "table" then
					if x.Name and x.Name ~= "" then return tostring(x.Name) end
					if x.ID and x.ID ~= "" then return tostring(x.ID) end
					if x.Value ~= nil then return tostring(x.Value) end
				end
				if type(x) == "number" then return tostring(x) end
				if type(x) == "string" and x ~= "" then return x end
				return nil
			end

			local vn = valName(h)
			if vn and not isNoneLike(vn) then
				return mw.text.nowiki(vn)
			end
		end

		-- Scalar number/string
		if type(h) == "number" then
			return mw.text.nowiki(fmtNum(h))
		end
		if type(h) == "string" then
			local t = trim(h)
			return (t and not isNoneLike(t)) and mw.text.nowiki(t) or nil
		end

		return nil
	end

	-- comboDisplay: render Combo as a compact text block (Type (+ details)).
	local function comboDisplay()
		local c = (type(mech.Combo) == "table") and mech.Combo or nil
		if not c then return nil end

		local typ = trim(c.Type)
		if not typ or isNoneLike(typ) then
			return nil
		end

		local details = {}

		local pct = formatUnitValue(c.Percent)
		if pct and not isZeroish(pct) then
			table.insert(details, mw.text.nowiki(pct))
		end

		local dur = formatUnitValue(c.Duration)
		if dur and not isZeroish(dur) then
			table.insert(details, mw.text.nowiki(dur))
		end

		if #details > 0 then
			return mw.text.nowiki(typ) .. " (" .. table.concat(details, ", ") .. ")"
		end
		return mw.text.nowiki(typ)
	end

	local grid = mw.html.create("div")
	grid:addClass("sv-type-grid")
	grid:addClass("sv-compact-root")

	local added = false

	-- addChunk: add one labeled value cell (key drives CSS ordering).
	local function addChunk(key, label, valueHtml)
		if valueHtml == nil or valueHtml == "" then return end
		added = true

		local chunk = grid:tag("div")
			:addClass("sv-type-chunk")
			:addClass("sv-type-" .. tostring(key))
			:attr("data-type-key", tostring(key))

		chunk:tag("div")
			:addClass("sv-type-label")
			:wikitext(mw.text.nowiki(label))

		chunk:tag("div")
			:addClass("sv-type-value")
			:wikitext(valueHtml)
	end

	-- Damage + Element + Hits bundle (hidden when non-damaging)
	if not hideDamageBundle then
		local dmg  = valName(typeBlock.Damage or typeBlock["Damage Type"])
		local ele  = valName(typeBlock.Element or typeBlock["Element Type"])
		local hits = hitsDisplay()

		if dmg and not isNoneLike(dmg) then
			addChunk("damage", "Damage", mw.text.nowiki(dmg))
		end
		if ele and not isNoneLike(ele) then
			addChunk("element", "Element", mw.text.nowiki(ele))
		end
		if hits then
			addChunk("hits", "Hits", hits)
		end
	end

	-- Target + Cast
	local tgt = valName(typeBlock.Target or typeBlock["Target Type"])
	local cst = valName(typeBlock.Cast   or typeBlock["Cast Type"])

	if tgt and not isNoneLike(tgt) then
		addChunk("target", "Target", mw.text.nowiki(tgt))
	end
	if cst and not isNoneLike(cst) then
		addChunk("cast", "Cast", mw.text.nowiki(cst))
	end

	-- Combo
	local combo = comboDisplay()
	if combo then
		addChunk("combo", "Combo", combo)
	end

        return {
                inner = added and tostring(grid) or "",
                classes = "module-skill-type",
        }
end

-- PLUGIN: Description (Hero Slot 3) - primary description text.
function PLUGINS.Description(rec)
        local desc = trim(rec.Description)
        if not desc then
                return nil
        end

        local body = mw.html.create("div")
        body:addClass("sv-description")
        body:wikitext(string.format("''%s''", desc))

        return {
                inner = tostring(body),
                classes = "module-description",
        }
end

-- PLUGIN: Placeholder (Hero Slot 4) - reserved/blank.
function PLUGINS.Placeholder()
        return nil
end


-- PLUGIN: SourceType (Hero Module Slot 1) - Modifier + Source + Scaling.
function PLUGINS.SourceType(rec, ctx)
	local level = ctx.level or 1
	local maxLevel = ctx.maxLevel or 1

	local basisWord = nil
	local sourceKind = nil
	local sourceVal  = nil
	local scaling    = nil

	-- sourceValueForLevel: dynamic formatting for structured Source blocks.
	local function sourceValueForLevel(src)
		if type(src) ~= "table" then
			return nil
		end

		local per = src["Per Level"]
		if type(per) == "table" and #per > 0 then
			if isFlatList(per) then
				local one  = formatUnitValue(per[1]) or tostring(per[1])
				local show = formatUnitValue(src.Base) or one
				return show and mw.text.nowiki(show) or nil
			end

			local series = {}
			for _, v in ipairs(per) do
				table.insert(series, formatUnitValue(v) or tostring(v))
			end
			return dynSpan(series, level)
		end

		return valuePairDynamicValueOnly(src, maxLevel, level)
	end

	if type(rec.Source) == "table" then
		local src = rec.Source
		local atkFlag  = (src["ATK-Based"] == true)
		local matkFlag = (src["MATK-Based"] == true)
		basisWord = basisWordFromFlags(atkFlag, matkFlag)

		sourceKind = src.Type or ((src.Healing == true) and "Healing") or "Damage"
		sourceVal  = sourceValueForLevel(src)
		scaling    = src.Scaling
	end

	-- Fallback to legacy Damage lists if Source absent
	if (sourceVal == nil or sourceVal == "") and type(rec.Damage) == "table" then
		local dmg = rec.Damage
		scaling = scaling or dmg.Scaling

		local main = dmg["Main Damage"]
		local refl = dmg["Reflect Damage"]
		local flat = dmg["Flat Damage"]

		if type(main) == "table" and #main > 0 then
			local pick = nil
			for _, d in ipairs(main) do
				if type(d) == "table" and d.Type ~= "Healing" then
					pick = d
					break
				end
			end
			pick = pick or main[1]

			if type(pick) == "table" then
				local atkFlag  = (pick["ATK-Based"] == true)
				local matkFlag = (pick["MATK-Based"] == true)
				basisWord = basisWord or basisWordFromFlags(atkFlag, matkFlag)

				sourceKind = (pick.Type == "Healing") and "Healing" or "Damage"
				sourceVal  = legacyPercentAtLevel(pick, level)
			end
		elseif type(refl) == "table" and #refl > 0 and type(refl[1]) == "table" then
			local pick = refl[1]
			local atkFlag  = (pick["ATK-Based"] == true)
			local matkFlag = (pick["MATK-Based"] == true)
			basisWord = basisWord or basisWordFromFlags(atkFlag, matkFlag)

			sourceKind = "Reflect"
			sourceVal  = legacyPercentAtLevel(pick, level)
		elseif type(flat) == "table" and #flat > 0 and type(flat[1]) == "table" then
			local pick = flat[1]
			local atkFlag  = (pick["ATK-Based"] == true)
			local matkFlag = (pick["MATK-Based"] == true)
			basisWord = basisWord or basisWordFromFlags(atkFlag, matkFlag)

			sourceKind = "Flat"
			sourceVal  = legacyPercentAtLevel(pick, level)
		end
	end

	local scalingLines = formatScalingCompactLines(scaling)
	local hasSource    = (sourceVal ~= nil and tostring(sourceVal) ~= "")
	local hasScaling   = (type(scalingLines) == "table" and #scalingLines > 0)

	if (not hasSource) and (not hasScaling) then
		return nil
	end

	local hasMod = (basisWord ~= nil and tostring(basisWord) ~= "")

local extra = { "skill-source-module", "module-source-type" }
	table.insert(extra, hasMod and "sv-has-mod" or "sv-no-mod")

	if hasSource and (not hasScaling) then
		table.insert(extra, "sv-only-source")
	elseif hasScaling and (not hasSource) then
		table.insert(extra, "sv-only-scaling")
	end

	local wrap = mw.html.create("div")
	wrap:addClass("sv-source-grid")
	wrap:addClass("sv-compact-root")

	if hasMod then
		local modCol = wrap:tag("div"):addClass("sv-source-col"):addClass("sv-source-modifier")
		modCol:tag("div"):addClass("sv-source-pill"):wikitext("Modifier")
		modCol:tag("div"):addClass("sv-modifier-value"):wikitext(mw.text.nowiki(basisWord))
	end

	if hasSource then
		local sourceCol = wrap:tag("div"):addClass("sv-source-col"):addClass("sv-source-main")
		sourceCol:tag("div"):addClass("sv-source-pill"):wikitext(mw.text.nowiki(sourceKind or "Source"))
		sourceCol:tag("div"):addClass("sv-source-value"):wikitext(sourceVal)
	end

	if hasScaling then
		local scalingCol = wrap:tag("div"):addClass("sv-source-col"):addClass("sv-source-scaling")
		scalingCol:tag("div"):addClass("sv-source-pill"):wikitext("Scaling")

		local list = scalingCol:tag("div"):addClass("sv-scaling-list")
		for _, line in ipairs(scalingLines) do
			list:tag("div"):addClass("sv-scaling-item"):wikitext(mw.text.nowiki(line))
		end
	end

	return {
		inner = tostring(wrap),
		classes = extra,
	}
end

-- PLUGIN: QuickStats (Hero Module Slot 2) - 3x2 grid (range/area/cost/cast/cd/duration).
-- NOTE: Hits does NOT live here (it lives in SkillType).
function PLUGINS.QuickStats(rec, ctx)
	local level = ctx.level or 1
	local maxLevel = ctx.maxLevel or 1
	local promo = ctx.promo

	local mech = (type(rec) == "table" and type(rec.Mechanics) == "table") and rec.Mechanics or {}
	local bt   = (type(mech["Basic Timings"]) == "table") and mech["Basic Timings"] or {}
	local rc   = (type(mech["Resource Cost"]) == "table") and mech["Resource Cost"] or {}

	local function dash() return "—" end

	-- Range (0 => —)
	local rangeVal = nil
	if mech.Range ~= nil and not isNoneLike(mech.Range) then
		local n = toNum(mech.Range)
		if n ~= nil then
			if n ~= 0 then
				rangeVal = mw.text.nowiki(formatUnitValue(mech.Range) or tostring(mech.Range))
			end
		else
			local t = mw.text.trim(tostring(mech.Range))
			if t ~= "" and not isNoneLike(t) then
				rangeVal = mw.text.nowiki(t)
			end
		end
	end

	-- Area
local areaVal = formatAreaSize(mech.Area, maxLevel, level)

	-- Timings
	local castVal = displayFromSeries(seriesFromValuePair(bt["Cast Time"], maxLevel), level)
	local cdVal   = displayFromSeries(seriesFromValuePair(bt["Cooldown"],  maxLevel), level)
	local durVal  = displayFromSeries(seriesFromValuePair(bt["Duration"],  maxLevel), level)

	-- Promote status duration if needed
	if (durVal == nil) and type(promo) == "table" and type(promo.durationBlock) == "table" then
		durVal = displayFromSeries(seriesFromValuePair(promo.durationBlock, maxLevel), level)
	end

	-- Cost: MP + HP
	local function labeledSeries(block, label)
		local s = seriesFromValuePair(block, maxLevel)
		if not s then return nil end
		local any = false
		for i, v in ipairs(s) do
			if v ~= "—" then
				s[i] = tostring(v) .. " " .. label
				any = true
			else
				s[i] = "—"
			end
		end
		return any and s or nil
	end

	local mpS = labeledSeries(rc["Mana Cost"], "MP")
	local hpS = labeledSeries(rc["Health Cost"], "HP")

	local costSeries = {}
	for lv = 1, maxLevel do
		local mp = mpS and mpS[lv] or "—"
		local hp = hpS and hpS[lv] or "—"

		if mp ~= "—" and hp ~= "—" then
			costSeries[lv] = mp .. " + " .. hp
		elseif mp ~= "—" then
			costSeries[lv] = mp
		elseif hp ~= "—" then
			costSeries[lv] = hp
		else
			costSeries[lv] = "—"
		end
	end

	local costVal = displayFromSeries(costSeries, level)

	local grid = mw.html.create("div")
	grid:addClass("sv-m4-grid")
	grid:addClass("sv-compact-root")

	local function addCell(label, val)
		local cell = grid:tag("div"):addClass("sv-m4-cell")
		cell:tag("div"):addClass("sv-m4-label"):wikitext(mw.text.nowiki(label))
		cell:tag("div"):addClass("sv-m4-value"):wikitext(val or dash())
	end

	addCell("Range",     rangeVal)
	addCell("Area",      areaVal)
	addCell("Cost",      costVal)
	addCell("Cast Time", castVal)
	addCell("Cooldown",  cdVal)
	addCell("Duration",  durVal)

	return {
		inner = tostring(grid),
		classes = "module-quick-stats",
	}
end

-- PLUGIN: SpecialMechanics (Hero Module Slot 3)
-- Shows:
--   - Flags (deduped)
--   - Special mechanics (mech.Effects)
-- NOTE: Combo lives in SkillType (Hero Bar Slot 2).
function PLUGINS.SpecialMechanics(rec, ctx)
	local level = ctx.level or 1
	local maxLevel = ctx.maxLevel or 1

	local mech    = (type(rec) == "table" and type(rec.Mechanics) == "table") and rec.Mechanics or {}
	local effects = (type(mech.Effects) == "table") and mech.Effects or nil
	local mods    = (type(rec.Modifiers) == "table") and rec.Modifiers or nil

	------------------------------------------------------------------
	-- Hits guard (we want Hits ONLY in SkillType)
	------------------------------------------------------------------
	local function isHitsKey(name)
		if not name then return false end
		local k = mw.ustring.lower(mw.text.trim(tostring(name)))
		return (
			k == "hit" or
			k == "hits" or
			k == "hit count" or
			k == "hits count" or
			k == "hitcount" or
			k == "hitscount"
		)
	end

	------------------------------------------------------------------
	-- Flags (flat, de-duped)
	------------------------------------------------------------------
	local flagSet = {}

	local denyFlags = {
		["self centered"] = true,
		["self-centred"] = true,
		["bond"] = true,
		["combo"] = true,
        ["hybrid"] = true,

		-- hits variants
		["hit"] = true,
		["hits"] = true,
		["hit count"] = true,
		["hits count"] = true,
		["hitcount"] = true,
		["hitscount"] = true,
	}

	local function allowFlag(name)
		if not name then return false end
		local k = mw.ustring.lower(mw.text.trim(tostring(name)))
		if k == "" then return false end
		if denyFlags[k] then return false end
		return true
	end

	local function addFlags(sub)
		if type(sub) ~= "table" then return end
		for k, v in pairs(sub) do
			if v and allowFlag(k) then
				flagSet[tostring(k)] = true
			end
		end
	end

	if mods then
		addFlags(mods["Movement Modifiers"])
		addFlags(mods["Combat Modifiers"])
		addFlags(mods["Special Modifiers"])
		for k, v in pairs(mods) do
			if type(v) == "boolean" and v and allowFlag(k) then
				flagSet[tostring(k)] = true
			end
		end
	end

	local flags = {}
	for k, _ in pairs(flagSet) do table.insert(flags, k) end
	table.sort(flags)

	------------------------------------------------------------------
	-- Special mechanics (name => value)
	------------------------------------------------------------------
	local mechItems = {}

	if effects then
		local keys = {}
		for k, _ in pairs(effects) do table.insert(keys, k) end
		table.sort(keys)

		for _, name in ipairs(keys) do
			-- Skip Hits completely (it belongs in SkillType)
			if not isHitsKey(name) then
				local block = effects[name]
				if type(block) == "table" then
					-- Also skip if the block's Type is "Hits" (some data may encode it that way)
					if not isHitsKey(block.Type) then
						local disp = displayFromSeries(seriesFromValuePair(block, maxLevel), level)
						local t = trim(block.Type)

						local value = disp

						-- If Type exists and is distinct, prefix it.
						if t and not isNoneLike(t) and mw.ustring.lower(t) ~= mw.ustring.lower(tostring(name)) then
							if value then
								value = mw.text.nowiki(t) .. ": " .. value
							else
								value = mw.text.nowiki(t)
							end
						end

						if value then
							table.insert(mechItems, { label = tostring(name), value = value })
						end
					end
				end
			end
		end
	end

	local hasFlags = (#flags > 0)
	local hasMech  = (#mechItems > 0)

	if (not hasFlags) and (not hasMech) then
		local root = mw.html.create("div")
		root:addClass("sv-sm-root")
		root:addClass("sv-compact-root")
		root:tag("div"):addClass("sv-sm-empty"):wikitext("No Special Mechanics")

		return {
			inner = tostring(root),
			classes = "module-special-mechanics",
		}
	end

	local count = 0
	if hasFlags then count = count + 1 end
	if hasMech  then count = count + 1 end

	local root = mw.html.create("div")
	root:addClass("sv-sm-root")
	root:addClass("sv-compact-root")

	local layout = root:tag("div"):addClass("sv-sm-layout")
	layout:addClass("sv-sm-count-" .. tostring(count))

	-- Column 1: Flags
	if hasFlags then
		local fcol = layout:tag("div"):addClass("sv-sm-col"):addClass("sv-sm-col-flags")
		for _, f in ipairs(flags) do
			fcol:tag("div"):addClass("sv-sm-flag"):wikitext(mw.text.nowiki(f))
		end
	end

	-- Column 2: Special Mechanics (stacked)
	if hasMech then
		local mcol = layout:tag("div"):addClass("sv-sm-col"):addClass("sv-sm-col-mech")
		for _, it in ipairs(mechItems) do
			local one = mcol:tag("div"):addClass("sv-sm-mech")
			one:tag("div"):addClass("sv-sm-label"):wikitext(mw.text.nowiki(it.label))
			one:tag("div"):addClass("sv-sm-value"):wikitext(it.value or "—")
		end
	end

	return {
		inner = tostring(root),
		classes = "module-special-mechanics",
	}
end

-- PLUGIN: LevelSelector (Hero Module Slot 4) - JS level slider.
function PLUGINS.LevelSelector(rec, ctx)
	local level = ctx.level or 1
	local maxLevel = ctx.maxLevel or 1

	local inner = mw.html.create("div")
	inner:addClass("sv-level-ui")

	inner:tag("div")
		:addClass("sv-level-label")
		:wikitext("Level <span class=\"sv-level-num\">" .. tostring(level) .. "</span> / " .. tostring(maxLevel))

	local slider = inner:tag("div"):addClass("sv-level-slider")

	if tonumber(maxLevel) and tonumber(maxLevel) > 1 then
		slider:tag("input")
			:attr("type", "range")
			:attr("min", "1")
			:attr("max", tostring(maxLevel))
			:attr("value", tostring(level))
			:addClass("sv-level-range")
			:attr("aria-label", "Skill level select")
	else
		inner:addClass("sv-level-ui-single")
		slider:addClass("sv-level-slider-single")
	end

	return {
		inner = tostring(inner),
		classes = "module-level-selector",
	}
end

----------------------------------------------------------------------
-- Generic slot renderers
----------------------------------------------------------------------

-- normalizeResult: normalize plugin return values into {inner, classes}.
local function normalizeResult(res)
	if res == nil then return nil end
	if type(res) == "string" then
		return { inner = res, classes = nil }
	end
	if type(res) == "table" then
		local inner = res.inner
		if type(inner) ~= "string" then
			inner = (inner ~= nil) and tostring(inner) or ""
		end
		return { inner = inner, classes = res.classes }
	end
	return { inner = tostring(res), classes = nil }
end

-- safeCallPlugin: pcall wrapper to prevent infobox failure on plugin errors.
local function safeCallPlugin(name, rec, ctx)
        local fn = PLUGINS[name]
        if type(fn) ~= "function" then
                return nil
        end
        local ok, out = pcall(fn, rec, ctx)
        if not ok then
                return nil
        end
        return normalizeResult(out)
end

-- isEmptySlotContent: true when a slot has no meaningful content.
-- NOTE: JS placeholders (sv-dyn spans, slider markup) are considered content.
local function isEmptySlotContent(inner)
        if inner == nil then return true end

        local raw = tostring(inner)

        -- Guard rails for JS-injected regions.
        for _, pat in ipairs({ "sv%-dyn", "data%-series", "sv%-level%-range", "sv%-level%-slider", "sv%-level%-ui" }) do
                if mw.ustring.find(raw, pat) then
                        return false
                end
        end

        local trimmed = mw.text.trim(raw)
        if trimmed == "" or trimmed == "—" then
                return true
        end

        local withoutTags = mw.text.trim(mw.ustring.gsub(trimmed, "<[^>]+>", ""))
        return (withoutTags == "" or withoutTags == "—")
end

-- renderHeroSlot: render a standardized hero slot by plugin assignment.
local function renderHeroSlot(slotIndex, rec, ctx)
        local pluginName = HERO_SLOT_ASSIGNMENT[slotIndex]
        if not pluginName then
                return nil
        end

        local res = safeCallPlugin(pluginName, rec, ctx)
        if not res or isEmptySlotContent(res.inner) then
                return nil
        end

        return {
                inner = res.inner,
                classes = res.classes,
        }
end

----------------------------------------------------------------------
-- UI builders
----------------------------------------------------------------------

-- buildHeroSlotsUI: build the standardized 4-row slot grid (2 columns).
local function buildHeroSlotsUI(rec, ctx)
        local grid = mw.html.create("div")
        grid:addClass("sv-slot-grid")

        local slots = {}
        for slot = 1, 8 do
                slots[slot] = renderHeroSlot(slot, rec, ctx)
        end

        local hasSlots = false
        for _, pair in ipairs({ { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }) do
                local left  = slots[pair[1]]
                local right = slots[pair[2]]

                if left or right then
                        hasSlots = true

                        if left and right then
                                grid:wikitext(slotBox(pair[1], left.classes, left.inner, { isEmpty = false }))
                                grid:wikitext(slotBox(pair[2], right.classes, right.inner, { isEmpty = false }))
                        elseif left then
                                grid:wikitext(slotBox(pair[1], left.classes, left.inner, { isFull = true }))
                        elseif right then
                                grid:wikitext(slotBox(pair[2], right.classes, right.inner, { isFull = true }))
                        end
                end
        end

        if not hasSlots then
                return ""
        end

        return tostring(grid)
end

-- addHeroSlotsRow: add the standardized slot grid into the infobox table.
local function addHeroSlotsRow(tbl, slotsUI)
        if not slotsUI or slotsUI == "" then
                return
        end

        local row = tbl:tag("tr")
        row:addClass("sv-slot-row")

        local cell = row:tag("td")
        cell:attr("colspan", 2)
        cell:addClass("sv-slot-cell")
        cell:wikitext(slotsUI)
end

----------------------------------------------------------------------
-- Infobox builder
----------------------------------------------------------------------

-- buildInfobox: render a single skill infobox.
local function buildInfobox(rec, opts)
	opts = opts or {}
	local showUsers = (opts.showUsers ~= false)

	local maxLevel = tonumber(rec["Max Level"]) or 1
	if maxLevel < 1 then maxLevel = 1 end
	local level = clamp(maxLevel, 1, maxLevel)

	local ctx = {
		maxLevel = maxLevel,
		level = level,
		nonDamaging = false,
		promo = nil,
	}

	-- Non-damaging hides Damage/Element/Hits in SkillType
	do
		local dmgVal = nil
		if type(rec.Type) == "table" then
			dmgVal = rec.Type.Damage or rec.Type["Damage Type"]
			if type(dmgVal) == "table" then
				dmgVal = dmgVal.Name or dmgVal.ID or dmgVal.Value
			end
		end
		ctx.nonDamaging = isNoneLike(dmgVal) or (not skillHasAnyDamage(rec, maxLevel))
	end

	ctx.promo = computeDurationPromotion(rec, maxLevel)

	local root = mw.html.create("table")
	root:addClass("spiritvale-skill-infobox")
	root:addClass("sv-skill-card")
	root:attr("data-max-level", tostring(maxLevel))
	root:attr("data-level", tostring(level))

	if opts.inList then
		root:addClass("sv-skill-inlist")
	end

	local internalId = trim(rec["Internal Name"] or rec.InternalID or rec.ID)
	if internalId then
		root:attr("data-skill-id", internalId)
	end

	-- Standardized slot grid
	addHeroSlotsRow(root, buildHeroSlotsUI(rec, ctx))

	-- Users (hide on direct skill page)
        if showUsers then
                local users = rec.Users or {}
                addRow(root, "Classes",  listToText(users.Classes),  "sv-row-users", "Users.Classes")
                addRow(root, "Summons",  listToText(users.Summons),  "sv-row-users", "Users.Summons")
                addRow(root, "Monsters", listToText(users.Monsters), "sv-row-users", "Users.Monsters")
                do
                        local eventsList = {}
                        if type(users.Events) == "table" then
                                for _, ev in ipairs(users.Events) do
                                        local name = resolveEventName(ev) or ev
                                        if name ~= nil then
                                                table.insert(eventsList, mw.text.nowiki(tostring(name)))
                                        end
                                end
                        end
                        addRow(root, "Events", listToText(eventsList), "sv-row-users", "Users.Events")
                end
        end

        -- Mechanics (keep small extras only)
        local mech = rec.Mechanics or {}
        if next(mech) ~= nil then
                if mech["Autocast Multiplier"] ~= nil then
                        addRow(root, "Autocast Multiplier", tostring(mech["Autocast Multiplier"]), "sv-row-mech", "Mechanics.Autocast Multiplier")
                end
        end

	-- Legacy damage breakdown (only when Source absent)
	if type(rec.Source) ~= "table" then
		local dmg = rec.Damage or {}
		if next(dmg) ~= nil then
			local main = dmg["Main Damage"]
			local mainNonHeal, healOnly = {}, {}

			if type(main) == "table" then
				for _, d in ipairs(main) do
					if type(d) == "table" and d.Type == "Healing" then
						table.insert(healOnly, d)
					else
						table.insert(mainNonHeal, d)
					end
				end
			end

			addRow(root, "Main Damage",    formatDamageList(mainNonHeal, maxLevel, level, (#mainNonHeal > 1)), "sv-row-source", "Damage.Main Damage")
			addRow(root, "Flat Damage",    formatDamageList(dmg["Flat Damage"], maxLevel, level, false),        "sv-row-source", "Damage.Flat Damage")
			addRow(root, "Reflect Damage", formatDamageList(dmg["Reflect Damage"], maxLevel, level, false),     "sv-row-source", "Damage.Reflect Damage")
			addRow(root, "Healing",        formatDamageList(healOnly, maxLevel, level, false),                  "sv-row-source", "Damage.Healing")
		end
	end

	-- Status rows
	local function formatStatusApplications(list, suppressDurationIndex)
		if type(list) ~= "table" or #list == 0 then return nil end

		local parts = {}
		for idx, s in ipairs(list) do
			if type(s) == "table" then
				local typ  = s.Type or s.Scope or "Target"
				local name = s["Status External Name"] or s["Status Internal Name"] or "Unknown status"
				local seg = tostring(typ) .. " – " .. tostring(name)
				local detail = {}

				if idx ~= suppressDurationIndex and type(s.Duration) == "table" then
					local t = valuePairDynamicValueOnly(s.Duration, maxLevel, level)
					if t then table.insert(detail, "Duration: " .. t) end
				end

				if type(s.Chance) == "table" then
					local t = valuePairDynamicValueOnly(s.Chance, maxLevel, level)
					if t then table.insert(detail, "Chance: " .. t) end
				end

				if #detail > 0 then
					seg = seg .. " (" .. table.concat(detail, ", ") .. ")"
				end

				table.insert(parts, seg)
			end
		end

		return (#parts > 0) and table.concat(parts, "<br />") or nil
	end

	local function formatStatusRemoval(list)
		if type(list) ~= "table" or #list == 0 then return nil end

		local parts = {}
		for _, r in ipairs(list) do
			if type(r) == "table" then
				local names = r["Status External Name"]
				local label

				if type(names) == "table" then
					label = table.concat(names, ", ")
				elseif type(names) == "string" then
					label = names
				else
					label = "Status"
				end

				local amt = valuePairRawText(r)
				amt = amt and mw.text.nowiki(amt) or nil

				local seg = mw.text.nowiki(label)
				if amt then
					seg = seg .. " – " .. amt
				end
				table.insert(parts, seg)
			end
		end

		return (#parts > 0) and table.concat(parts, "<br />") or nil
	end

	local suppressIdx = (type(ctx.promo) == "table") and ctx.promo.suppressDurationIndex or nil
	local statusApps = formatStatusApplications(rec["Status Applications"], suppressIdx)
	local statusRem  = formatStatusRemoval(rec["Status Removal"])
	if statusApps or statusRem then
		addRow(root, "Applies", statusApps, "sv-row-status", "Status Applications")
		addRow(root, "Removes", statusRem,  "sv-row-status", "Status Removal")
	end

        -- Events
        local function formatEvents(list)
                if type(list) ~= "table" or #list == 0 then return nil end
                local parts = {}
                for _, ev in ipairs(list) do
                        if type(ev) == "table" then
                                local action = resolveDisplayName(ev.Action, "event") or ev.Action or "On event"
                                local name   = resolveSkillNameFromEvent(ev)
                                table.insert(parts, string.format("%s → %s", mw.text.nowiki(action), mw.text.nowiki(name)))
                        end
                end
                return (#parts > 0) and table.concat(parts, "<br />") or nil
        end

	local eventsText = formatEvents(rec.Events)
	if eventsText then
		addRow(root, "Triggers", eventsText, "sv-row-meta", "Events")
	end

return tostring(root)
end

----------------------------------------------------------------------
-- Public: list all skills for a given user/class
----------------------------------------------------------------------

function p.listForUser(frame)
	local args = getArgs(frame)

	local userName = args.user or args[1]
	if not userName or userName == "" then
		userName = mw.title.getCurrentTitle().text
	end

	if not userName or userName == "" then
		return "<strong>No user name provided to Skill list.</strong>"
	end

	local dataset = getSkills()
	local matches = {}

	for _, rec in ipairs(dataset.records or {}) do
		if skillMatchesUser(rec, userName) then
			table.insert(matches, rec)
		end
	end

	if #matches == 0 then
		return string.format("<strong>No skills found for:</strong> %s", mw.text.nowiki(userName))
	end

	local out = {}

	for _, rec in ipairs(matches) do
		local title = rec["External Name"] or rec.Name or rec["Internal Name"] or "Unknown Skill"

		-- List mode: emit a raw H3 heading before each standalone card so TOC/anchors work.
		table.insert(out, string.format("=== %s ===", title))

		-- List mode cards are independent (no single wrapper container).
		table.insert(out, buildInfobox(rec, { showUsers = false, inList = true }))
	end

	return table.concat(out, "\n")
end

----------------------------------------------------------------------
-- Public: single-skill or auto-list dispatcher
----------------------------------------------------------------------

function p.infobox(frame)
	local args = getArgs(frame)

	local raw1 = args[1]
	local name = args.name or raw1
	local id   = args.id

	local rec
	if name and name ~= "" then
		rec = findSkillByName(name)
	end
	if not rec and id and id ~= "" then
		rec = getSkillById(id)
	end

	if not rec then
		local pageTitle = mw.title.getCurrentTitle()
		local pageName  = pageTitle and pageTitle.text or ""

		local noExplicitArgs =
			(not raw1 or raw1 == "") and
			(not args.name or args.name == "") and
			(not id or id == "")

		if noExplicitArgs then
			return p.listForUser(frame)
		end

		if name and name ~= "" and name == pageName and (not id or id == "") then
			return p.listForUser(frame)
		end

		local label = name or id or "?"
		return string.format(
			"<strong>Unknown Skill:</strong> %s[[Category:Pages with unknown skill|%s]]",
			mw.text.nowiki(label),
			label
		)
	end

	local showUsers = not isDirectSkillPage(rec)
	return buildInfobox(rec, { showUsers = showUsers })
end

return p