-- ApiVault Complete Function & Symbol Coverage runtime helpers. Generated for ApiVault v0.5.3.
-- Compact metadata only. No bundled source is read, displayed or executed at runtime.
local addonName = ...
local ApiVault = _G.ApiVault

ApiVault.Symbols = ApiVault.Symbols or {}
local Symbols = ApiVault.Symbols

Symbols.entries = Symbols.entries or {}
Symbols.byName = Symbols.byName or {}
Symbols.byModule = Symbols.byModule or {}
Symbols.byType = Symbols.byType or {}
Symbols.byFile = Symbols.byFile or {}
Symbols.slashCommands = Symbols.slashCommands or {}
Symbols.metadata = {
	documented_api_symbols = 6144,
	generated_at = "2026-06-28T18:10:10.558431+00:00",
	interface = 120007,
	lua_files_scanned = 2298,
	mixin_methods = 25470,
	policy = "metadata-only; no runtime file reads; no function auto-execution; runtime globals are read-only and session-specific",
	resource_root = "Interface/AddOns/ApiVault/Examples/Interface",
	slash_command_symbols = 9,
	source_archive = "Interface.zip",
	source_global_functions = 7830,
	source_local_functions = 4130,
	source_methods = 2655,
	source_sha256 = "e04620731330f6c61c16f735ecba4ff550f7ad1baaf9912b6f8715a965ff0055",
	source_table_functions = 1957,
	symbol_entries = 58544,
	target_package = "ApiVault-v0.5.2.zip",
	target_sha256 = "e8c7145c76a2f4af1dfb4fb4cac419489e7a2a629325b3e4889dafc90b94da61",
	type_counts = {
		["documented-api"] = 6144,
		["mixin-method"] = 25470,
		["slash-command-declaration"] = 2,
		["slash-command-handler"] = 7,
		["source-global-function"] = 7830,
		["source-local-function"] = 4130,
		["source-method"] = 2655,
		["source-table-function"] = 1957,
		["xml-script-handler"] = 10349,
	},
	version = "0.5.3",
	xml_files_scanned = 1028,
	xml_script_handlers = 10349,
}

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 addToBucket(bucket, key, entry)
	if key == nil or key == "" then
		return
	end
	bucket[key] = bucket[key] or {}
	table.insert(bucket[key], entry)
end

function Symbols:RegisterShard(shard)
	if type(shard) ~= "table" then
		return
	end
	for _, symbol in ipairs(shard) do
		table.insert(self.entries, symbol)
		addToBucket(self.byName, symbol.symbolName, symbol)
		addToBucket(self.byModule, symbol.module, symbol)
		addToBucket(self.byType, symbol.symbolType, symbol)
		addToBucket(self.byFile, symbol.sourcePath, symbol)
		addToBucket(self.byFile, symbol.addonPath, symbol)
	end
end

function Symbols:RegisterSlashCommands(data)
	if type(data) ~= "table" then
		return
	end
	for _, entry in ipairs(data) do
		table.insert(self.slashCommands, entry)
	end
end

function Symbols:GetStats()
	local meta = self.metadata or {}
	return meta.symbol_entries or #(self.entries or {}), meta.documented_api_symbols or 0, meta.mixin_methods or 0, meta.xml_script_handlers or 0, meta.slash_command_symbols or 0
end

function Symbols:GetForName(name, limit)
	local list = self.byName and self.byName[name] or {}
	limit = limit or #list
	local out = {}
	for _, entry in ipairs(list) do
		table.insert(out, entry)
		if #out >= limit then break end
	end
	return out
end

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

function Symbols:GetForModule(moduleName, limit)
	local list = self.byModule and self.byModule[moduleName] or {}
	limit = limit or #list
	local out = {}
	for _, entry in ipairs(list) do
		table.insert(out, entry)
		if #out >= limit then break end
	end
	return out
end

function Symbols:FilterMatches(symbol, filter)
	filter = filter or "All"
	if filter == "All" then return true end
	local t = symbol.symbolType or ""
	if filter == "DocumentedAPI" then return t == "documented-api" end
	if filter == "Global" then return t == "source-global-function" end
	if filter == "Local" then return t == "source-local-function" end
	if filter == "TableFunction" then return t == "source-table-function" end
	if filter == "Method" then return t == "source-method" or t == "source-table-function" end
	if filter == "Mixin" then return t == "mixin-method" end
	if filter == "XMLScript" then return t == "xml-script-handler" end
	if filter == "Slash" then return t == "slash-command-handler" or t == "slash-command-declaration" end
	return true
end

function Symbols:Search(query, filter, limit)
	query = lower(query)
	filter = filter or "All"
	limit = limit or 300
	local results = {}
	for _, symbol in ipairs(self.entries or {}) do
		if self:FilterMatches(symbol, filter)
			and (query == "" or contains(symbol.symbolName, query) or contains(symbol.module, query) or contains(symbol.ownerTable, query) or contains(symbol.sourcePath, query) or contains(symbol.symbolType, query)) then
			table.insert(results, {
				kind = symbol.symbolType or "symbol",
				display = tostring(symbol.symbolName or "?") .. " [" .. tostring(symbol.symbolType or "?") .. "]",
				name = symbol.symbolName,
				data = symbol,
			})
			if #results >= limit then
				break
			end
		end
	end
	return results
end

function Symbols:DescribeSymbol(entry)
	local symbol = entry and entry.data or entry
	if type(symbol) ~= "table" then
		return "No symbol selected."
	end
	local lines = {}
	table.insert(lines, tostring(symbol.symbolName or "?"))
	table.insert(lines, "type: " .. tostring(symbol.symbolType or "?"))
	table.insert(lines, "policy: " .. tostring(symbol.callablePolicy or "inspect-only"))
	table.insert(lines, "module: " .. tostring(symbol.module or ""))
	table.insert(lines, "ownerTable: " .. tostring(symbol.ownerTable or ""))
	table.insert(lines, "methodName: " .. tostring(symbol.methodName or ""))
	table.insert(lines, "sourcePath: " .. tostring(symbol.sourcePath or ""))
	table.insert(lines, "addonPath: " .. tostring(symbol.addonPath or ""))
	table.insert(lines, "line: " .. tostring(symbol.line or ""))
	table.insert(lines, "linkedAPI: " .. tostring(symbol.linkedAPI or ""))
	table.insert(lines, "linkedModule: " .. tostring(symbol.linkedModule or ""))
	table.insert(lines, "linkedTemplate: " .. tostring(symbol.linkedTemplate or ""))
	table.insert(lines, "linkedSecurePatterns: " .. tostring(symbol.linkedSecurePatterns or 0))
	table.insert(lines, "context: " .. tostring(symbol.contextLabel or ""))
	table.insert(lines, "")
	table.insert(lines, "Coverage label:")
	if symbol.symbolType == "documented-api" then
		table.insert(lines, "- Documented API")
	elseif symbol.symbolType == "source-local-function" then
		table.insert(lines, "- Local source-only")
	elseif symbol.symbolType == "mixin-method" then
		table.insert(lines, "- Mixin method")
	elseif symbol.symbolType == "xml-script-handler" then
		table.insert(lines, "- XML script handler")
	elseif symbol.symbolType == "slash-command-handler" or symbol.symbolType == "slash-command-declaration" then
		table.insert(lines, "- Slash command metadata")
	else
		table.insert(lines, "- Source-backed symbol")
	end
	table.insert(lines, "")
	table.insert(lines, "Execution policy:")
	table.insert(lines, "This symbol catalog is inspect-only. ApiVault does not automatically call discovered functions.")
	table.insert(lines, tostring(symbol.uncertainNotes or ""))
	return table.concat(lines, "\n")
end
