-- ApiVault Catalog Index. Generated for ApiVault v0.5.5.
-- Constants, slash commands, bindings and settings metadata only. No runtime file reads or execution.
local addonName = ...
local ApiVault = _G.ApiVault

ApiVault.Catalogs = ApiVault.Catalogs or {}
local Catalogs = ApiVault.Catalogs

Catalogs.entries = Catalogs.entries or {}
Catalogs.byType = Catalogs.byType or {}
Catalogs.byName = Catalogs.byName or {}
Catalogs.metadata = {
	bindings = 1076,
	constants = 7942,
	generated_at = "2026-06-28T18:34:35.267368+00:00",
	interface = 120007,
	policy = "metadata-only; no runtime file reads; no slash/CVar/binding execution",
	settings = 1865,
	slashCommands = 10,
	source_archive = "Interface.zip",
	source_sha256 = "e04620731330f6c61c16f735ecba4ff550f7ad1baaf9912b6f8715a965ff0055",
	target_package = "ApiVault-v0.5.4.zip",
	target_sha256 = "f072948ec1f253f8de047ccf93e1404c6ed950e2a728ae573a58804812a09c51",
	totalCatalogEntries = 10893,
	version = "0.5.5",
}

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 Catalogs:RegisterShard(kind, shard)
    if type(shard) ~= "table" then return end
    for _, entry in ipairs(shard) do
        entry.catalogFamily = kind or entry.catalogFamily or entry.catalogType or "catalog"
        table.insert(self.entries, entry)
        bucket(self.byType, entry.catalogFamily, entry)
        bucket(self.byType, entry.catalogType, entry)
        bucket(self.byName, entry.name, entry)
    end
end

function Catalogs:GetStats()
    local m = self.metadata or {}
    return m.totalCatalogEntries or #(self.entries or {}), m.constants or 0, m.slashCommands or 0, m.bindings or 0, m.settings or 0
end

function Catalogs:FilterMatches(entry, filter)
    filter = filter or "All"
    if filter == "All" then return true end
    if filter == "Constants" then return entry.catalogFamily == "Constants" end
    if filter == "Slash" then return entry.catalogFamily == "Slash" end
    if filter == "Bindings" then return entry.catalogFamily == "Bindings" end
    if filter == "Settings" then return entry.catalogFamily == "Settings" end
    return true
end

function Catalogs: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.module, query) or contains(entry.sourcePath, query) or contains(entry.contextLabel, query) or contains(entry.catalogType, query)) then
            table.insert(out, { kind = entry.catalogFamily or entry.catalogType or "Catalog", display = tostring(entry.name or "?") .. " [" .. tostring(entry.catalogType or "catalog") .. "]", name = entry.name, data = entry })
            if #out >= limit then break end
        end
    end
    return out
end

function Catalogs:Describe(entryOrResult)
    local entry = entryOrResult and entryOrResult.data or entryOrResult
    if type(entry) ~= "table" then return "No catalog entry selected." end
    local lines = {}
    table.insert(lines, tostring(entry.name or "?"))
    table.insert(lines, "catalogFamily: " .. tostring(entry.catalogFamily or ""))
    table.insert(lines, "catalogType: " .. tostring(entry.catalogType 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"))
    if entry.value then table.insert(lines, "value: " .. tostring(entry.value)) end
    if entry.command then table.insert(lines, "slash command: " .. tostring(entry.command)) end
    if entry.handler then table.insert(lines, "handler: " .. tostring(entry.handler)) end
    if entry.category then table.insert(lines, "category: " .. tostring(entry.category)) end
    if entry.api then table.insert(lines, "api: " .. tostring(entry.api)) end
    table.insert(lines, "context: " .. tostring(entry.contextLabel or ""))
    table.insert(lines, "")
    table.insert(lines, "Policy: metadata-only. ApiVault does not execute slash commands, bindings, settings calls or CVar changes from this catalog.")
    table.insert(lines, tostring(entry.notes or ""))
    return table.concat(lines, "\n")
end
