-- ApiVault TOC Module Browser runtime helpers. Generated for ApiVault v0.2.5.
-- Compact metadata only. No bundled Blizzard source is read or executed at runtime.
local addonName, ApiVault = ...

ApiVault.ModuleCatalog = ApiVault.ModuleCatalog or {}
local Modules = ApiVault.ModuleCatalog

Modules.tocs = Modules.tocs or {}
Modules.byModule = Modules.byModule or {}
Modules.byPath = Modules.byPath or {}
Modules.metadata = {
	generated_at = "2026-06-28T12:17:06.771189+00:00",
	interface = 120007,
	module_entries = 317,
	resource_root = "Interface/AddOns/ApiVault/Examples/Interface",
	source_archive = "Interface.zip",
	source_sha256 = "e04620731330f6c61c16f735ecba4ff550f7ad1baaf9912b6f8715a965ff0055",
	target_package = "ApiVault-v0.2.4.zip",
	target_sha256 = "55fab8519375b21bc27839e040314fdd0c55b961aa29730e758cedef1078667c",
	toc_entries = 346,
	toc_files_scanned = 346,
	toc_reference_entries = 3679,
	version = "0.2.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 asBoolean(value)
	return value == true or value == 1 or value == "1" or value == "true" or value == "enabled"
end

function Modules:RegisterTOCShard(shard)
	if type(shard) ~= "table" then
		return
	end
	for _, entry in ipairs(shard) do
		table.insert(self.tocs, entry)
		if entry.moduleName then
			self.byModule[entry.moduleName] = self.byModule[entry.moduleName] or {}
			table.insert(self.byModule[entry.moduleName], entry)
		end
		if entry.tocPath then
			self.byPath[entry.tocPath] = entry
		end
	end
end

function Modules:GetStats()
	local meta = self.metadata or {}
	return meta.toc_entries or #(self.tocs or {}), meta.module_entries or 0, meta.toc_reference_entries or 0
end

function Modules:GetModuleEntries(moduleName)
	return self.byModule and self.byModule[moduleName]
end

function Modules:GetTOC(path)
	return self.byPath and self.byPath[path]
end

function Modules:GetModuleSummary(moduleName)
	local entries = self:GetModuleEntries(moduleName)
	if type(entries) ~= "table" or #entries == 0 then
		return nil
	end
	local first = entries[1]
	return {
		moduleName = moduleName,
		tocEntries = #entries,
		fileCount = first.fileCount or 0,
		luaCount = first.luaCount or 0,
		xmlCount = first.xmlCount or 0,
		otherCount = first.otherCount or 0,
		apiUsageCount = first.apiUsageCount or 0,
		eventUsageCount = first.eventUsageCount or 0,
		xmlTemplateCount = first.xmlTemplateCount or 0,
		loadOnDemand = first.loadOnDemand or "",
		allowLoad = first.allowLoad or "",
		allowLoadGameType = first.allowLoadGameType or "",
	}
end

function Modules:Search(query, filter, limit)
	query = lower(query)
	filter = filter or "All"
	limit = limit or 300
	local results = {}
	for _, entry in ipairs(self.tocs or {}) do
		local include = false
		if filter == "All" then
			include = true
		elseif filter == "LoadOnDemand" then
			include = asBoolean(entry.loadOnDemand)
		elseif filter == "HasDeps" then
			include = (type(entry.dependencies) == "table" and #entry.dependencies > 0) or (type(entry.requiredDeps) == "table" and #entry.requiredDeps > 0) or (type(entry.optionalDeps) == "table" and #entry.optionalDeps > 0)
		elseif filter == "SavedVars" then
			include = (type(entry.savedVariables) == "table" and #entry.savedVariables > 0) or (type(entry.savedVariablesPerCharacter) == "table" and #entry.savedVariablesPerCharacter > 0)
		elseif filter == "Secure" then
			include = asBoolean(entry.secure) or asBoolean(entry.useSecureEnvironment)
		elseif filter == "Lua" then
			include = (entry.tocLuaCount or 0) > 0
		elseif filter == "XML" then
			include = (entry.tocXmlCount or 0) > 0
		else
			include = true
		end
		if include then
			local haystack = tostring(entry.moduleName or "") .. " " .. tostring(entry.tocName or "") .. " " .. tostring(entry.title or "") .. " " .. tostring(entry.notes or "") .. " " .. tostring(entry.tocPath or "") .. " " .. table.concat(entry.dependencies or {}, " ") .. " " .. table.concat(entry.requiredDeps or {}, " ") .. " " .. table.concat(entry.optionalDeps or {}, " ")
			if contains(haystack, query) then
				table.insert(results, { kind = "ModuleTOC", data = entry, display = tostring(entry.moduleName or "?") .. " / " .. tostring(entry.tocName or "?") })
				if #results >= limit then return results end
			end
		end
	end
	return results
end

local function appendList(lines, title, values, limit)
	if type(values) ~= "table" or #values == 0 then
		return
	end
	table.insert(lines, "")
	table.insert(lines, title .. ":")
	limit = limit or 30
	for index, value in ipairs(values) do
		if index > limit then
			table.insert(lines, "- ... +" .. tostring(#values - limit) .. " more")
			break
		end
		if type(value) == "table" then
			local path = value.ap or value.sp or "?"
			local suffix = value.cond and (" [" .. tostring(value.cond) .. "]") or ""
			if value.dynamic == 1 then
				suffix = suffix .. " [dynamic path]"
			end
			table.insert(lines, "- " .. tostring(path) .. suffix)
		else
			table.insert(lines, "- " .. tostring(value))
		end
	end
end

function Modules:DescribeModule(entryOrResult)
	local entry = entryOrResult and (entryOrResult.data or entryOrResult) or nil
	if not entry then
		return "No module selected."
	end
	local lines = {}
	table.insert(lines, "TOC module metadata")
	table.insert(lines, "moduleName: " .. tostring(entry.moduleName or "?"))
	table.insert(lines, "tocPath: " .. tostring(entry.tocPath or "?"))
	table.insert(lines, "addonPath: " .. tostring(entry.addonPath or "?"))
	table.insert(lines, "title: " .. tostring(entry.title or ""))
	if entry.notes and entry.notes ~= "" then
		table.insert(lines, "notes: " .. tostring(entry.notes))
	end
	table.insert(lines, "interfaceVersion: " .. tostring(entry.interfaceVersion or ""))
	table.insert(lines, "loadOnDemand: " .. tostring(entry.loadOnDemand or ""))
	table.insert(lines, "allowLoad: " .. tostring(entry.allowLoad or ""))
	table.insert(lines, "allowLoadGameType: " .. tostring(entry.allowLoadGameType or ""))
	table.insert(lines, "defaultState: " .. tostring(entry.defaultState or ""))
	table.insert(lines, "secure: " .. tostring(entry.secure or ""))
	table.insert(lines, "useSecureEnvironment: " .. tostring(entry.useSecureEnvironment or ""))

	appendList(lines, "dependencies", entry.dependencies, 20)
	appendList(lines, "requiredDeps", entry.requiredDeps, 20)
	appendList(lines, "optionalDeps", entry.optionalDeps, 20)
	appendList(lines, "savedVariables", entry.savedVariables, 20)
	appendList(lines, "savedVariablesPerCharacter", entry.savedVariablesPerCharacter, 20)

	if type(entry.otherTags) == "table" then
		local tagLines = {}
		for key, value in pairs(entry.otherTags) do
			table.insert(tagLines, tostring(key) .. "=" .. tostring(value))
		end
		if #tagLines > 0 then
			table.sort(tagLines)
			table.insert(lines, "")
			table.insert(lines, "other TOC tags:")
			for _, text in ipairs(tagLines) do
				table.insert(lines, "- " .. text)
			end
		end
	end

	table.insert(lines, "")
	table.insert(lines, "TOC-listed files: " .. tostring(entry.tocFileCount or 0) .. " total / lua " .. tostring(entry.tocLuaCount or 0) .. " / xml " .. tostring(entry.tocXmlCount or 0) .. " / other " .. tostring(entry.tocOtherCount or 0))
	table.insert(lines, "Module resource summary: " .. tostring(entry.fileCount or 0) .. " files / lua " .. tostring(entry.luaCount or 0) .. " / xml " .. tostring(entry.xmlCount or 0) .. " / other " .. tostring(entry.otherCount or 0))
	table.insert(lines, "Linked usage summary: APIs " .. tostring(entry.apiUsageCount or 0) .. " calls / events " .. tostring(entry.eventUsageCount or 0) .. " registrations / XML templates " .. tostring(entry.xmlTemplateCount or 0))

	appendList(lines, "luaFiles", entry.luaFiles, 35)
	appendList(lines, "xmlFiles", entry.xmlFiles, 35)
	appendList(lines, "otherFiles", entry.otherFiles, 35)

	if ApiVault.SecurePatterns and ApiVault.SecurePatterns.AppendModuleSummaryText then
		lines = { ApiVault.SecurePatterns:AppendModuleSummaryText(table.concat(lines, "\n"), entry.moduleName, 8) }
	end
	table.insert(lines, "")
	table.insert(lines, "Note: generated metadata only. ApiVault does not read, display, or execute bundled Blizzard source files at runtime.")
	return table.concat(lines, "\n")
end
