-- ApiVault Widget / Frame / ScriptObject Method Explorer. Generated for ApiVault v0.5.5.
-- Metadata only. No runtime source reads, no automatic method calls, no secure execution.
local addonName, ApiVault = ...

ApiVault.Widgets = ApiVault.Widgets or {}
local Widgets = ApiVault.Widgets

Widgets.entries = Widgets.entries or {}
Widgets.byType = Widgets.byType or {}
Widgets.byName = Widgets.byName or {}
Widgets.byFile = Widgets.byFile or {}
Widgets.metadata = {
	createFrameUsage = 311,
	generated_at = "2026-06-28T18:34:35.267368+00:00",
	interface = 120007,
	policy = "metadata-only; no runtime file reads; no widget/script/method auto-calls",
	scriptHandlers = 10349,
	setScriptUsage = 1605,
	source_archive = "Interface.zip",
	source_sha256 = "e04620731330f6c61c16f735ecba4ff550f7ad1baaf9912b6f8715a965ff0055",
	target_package = "ApiVault-v0.5.4.zip",
	target_sha256 = "f072948ec1f253f8de047ccf93e1404c6ed950e2a728ae573a58804812a09c51",
	totalWidgetEntries = 36740,
	version = "0.5.5",
	widgetMethods = 24475,
}

local function lower(value) return string.lower(tostring(value or "")) end
local function contains(haystack, needle) haystack = lower(haystack); needle = lower(needle); return needle == "" or string.find(haystack, needle, 1, true) ~= nil end
local function bucket(target, key, entry) if key == nil or key == "" then return end target[key] = target[key] or {}; table.insert(target[key], entry) end

function Widgets:RegisterShard(kind, shard)
    if type(shard) ~= "table" then return end
    for _, entry in ipairs(shard) do
        entry.widgetFamily = kind or entry.widgetFamily or entry.widgetType or "widget"
        table.insert(self.entries, entry)
        bucket(self.byType, entry.widgetFamily, entry)
        bucket(self.byType, entry.widgetType, entry)
        bucket(self.byName, entry.name, entry)
        bucket(self.byName, entry.methodName, entry)
        bucket(self.byFile, entry.sourcePath, entry)
        bucket(self.byFile, entry.addonPath, entry)
    end
end

function Widgets:GetStats()
    local m = self.metadata or {}
    return m.totalWidgetEntries or #(self.entries or {}), m.widgetMethods or 0, m.scriptHandlers or 0, m.createFrameUsage or 0, m.setScriptUsage or 0
end

function Widgets:FilterMatches(entry, filter)
    filter = filter or "All"
    if filter == "All" then return true end
    if filter == "Methods" then return entry.widgetFamily == "Methods" end
    if filter == "Scripts" then return entry.widgetFamily == "Scripts" or entry.widgetFamily == "SetScript" end
    if filter == "CreateFrame" then return entry.widgetFamily == "CreateFrame" end
    if filter == "XMLWidgets" then return entry.widgetType == "xml-widget-template" end
    if filter == "Protected" then return entry.riskClass == "protected" or entry.riskClass == "secure-sensitive" end
    return true
end

function Widgets:Search(query, filter, limit)
    query = lower(query)
    filter = filter or "All"
    limit = limit or 300
    local out = {}
    for _, entry in ipairs(self.entries or {}) do
        if self:FilterMatches(entry, filter) and (query == "" or contains(entry.name, query) or contains(entry.methodName, query) or contains(entry.frameType, query) or contains(entry.template, query) or contains(entry.module, query) or contains(entry.sourcePath, query) or contains(entry.widgetType, query)) then
            table.insert(out, { kind = entry.widgetFamily or entry.widgetType or "Widget", display = tostring(entry.name or entry.methodName or "?") .. " [" .. tostring(entry.widgetType or "widget") .. "]", name = entry.name or entry.methodName, data = entry })
            if #out >= limit then break end
        end
    end
    return out
end

function Widgets:GetForFile(path, limit)
    local list = self.byFile and self.byFile[path] or {}
    local out = {}
    limit = limit or #list
    for _, entry in ipairs(list) do table.insert(out, entry); if #out >= limit then break end end
    return out
end

function Widgets:Describe(entryOrResult)
    local entry = entryOrResult and entryOrResult.data or entryOrResult
    if type(entry) ~= "table" then return "No widget entry selected." end
    local lines = {}
    table.insert(lines, tostring(entry.name or entry.methodName or "?"))
    table.insert(lines, "widgetFamily: " .. tostring(entry.widgetFamily or ""))
    table.insert(lines, "widgetType: " .. tostring(entry.widgetType or ""))
    table.insert(lines, "frameType: " .. tostring(entry.frameType or ""))
    table.insert(lines, "methodName: " .. tostring(entry.methodName or ""))
    table.insert(lines, "scriptName: " .. tostring(entry.scriptName or ""))
    table.insert(lines, "template: " .. tostring(entry.template or ""))
    table.insert(lines, "module: " .. tostring(entry.module or ""))
    table.insert(lines, "sourcePath: " .. tostring(entry.sourcePath or ""))
    table.insert(lines, "addonPath: " .. tostring(entry.addonPath or ""))
    table.insert(lines, "line: " .. tostring(entry.line or ""))
    table.insert(lines, "riskClass: " .. tostring(entry.riskClass or "metadata"))
    table.insert(lines, "callablePolicy: " .. tostring(entry.callablePolicy or "inspect-only"))
    table.insert(lines, "context: " .. tostring(entry.contextLabel or ""))
    table.insert(lines, "")
    table.insert(lines, "Policy: inspect-only metadata. ApiVault does not call widget methods, attach scripts or create frames from this catalog.")
    table.insert(lines, tostring(entry.notes or ""))
    return table.concat(lines, "\n")
end
