-- ApiVault Interface Resource Catalog runtime helpers. Generated for ApiVault v0.2.4.
-- Compact metadata only. No runtime file reads and no bundled source files are TOC-loaded.
local addonName, ApiVault = ...

ApiVault.InterfaceResources = ApiVault.InterfaceResources or {}
local Resources = ApiVault.InterfaceResources

Resources.files = Resources.files or {}
Resources.folders = Resources.folders or {}
Resources.modules = Resources.modules or {}
Resources.metadata = {
	version = "0.2.4",
	interface = 120007,
	source_archive = "Interface.zip",
	resource_root = "Interface/AddOns/ApiVault/Examples/Interface",
	internal_root = "ApiVault/Examples/Interface",
	file_entries = 3673,
	folder_entries = 506,
	module_entries = 317,
	generated_at = "2026-06-28T11:34:02.551982+00:00",
}

local function lower(value)
	return string.lower(tostring(value or ""))
end

function Resources:RegisterFileShard(shard)
	if type(shard) ~= "table" then
		return
	end
	for _, fileInfo in ipairs(shard) do
		table.insert(self.files, fileInfo)
	end
end

function Resources:RegisterFolderShard(shard)
	if type(shard) ~= "table" then
		return
	end
	for _, folderInfo in ipairs(shard) do
		table.insert(self.folders, folderInfo)
	end
end

function Resources:RegisterModuleData(data)
	if type(data) ~= "table" then
		return
	end
	for moduleName, summary in pairs(data) do
		self.modules[moduleName] = summary
	end
end

function Resources:GetStats()
	local meta = self.metadata or {}
	return meta.file_entries or #self.files, meta.folder_entries or #self.folders, meta.module_entries or 0
end

function Resources:GetRoot()
	return (self.metadata and self.metadata.resource_root) or "Interface/AddOns/ApiVault/Examples/Interface"
end

function Resources:ToAddonPath(sourcePath)
	if type(sourcePath) ~= "string" or sourcePath == "" then
		return "?"
	end
	if string.sub(sourcePath, 1, 45) == "Interface/AddOns/ApiVault/Examples/Interface/" then
		return sourcePath
	end
	if sourcePath == "Interface" then
		return "Interface/AddOns/ApiVault/Examples/Interface"
	end
	if string.sub(sourcePath, 1, 10) == "Interface/" then
		return "Interface/AddOns/ApiVault/Examples/" .. sourcePath
	end
	if string.sub(sourcePath, 1, 28) == "ApiVault/Examples/Interface/" then
		return "Interface/AddOns/" .. sourcePath
	end
	return sourcePath
end

function Resources:Search(query, filter, limit)
	query = lower(query)
	filter = filter or "All"
	limit = limit or 300
	local results = {}
	if filter == "Folder" then
		for _, folderInfo in ipairs(self.folders or {}) do
			local haystack = lower((folderInfo.sp or "") .. " " .. (folderInfo.ap or ""))
			if query == "" or string.find(haystack, query, 1, true) then
				table.insert(results, { kind = "Folder", data = folderInfo, display = folderInfo.ap or folderInfo.sp or "?" })
				if #results >= limit then return results end
			end
		end
		return results
	elseif filter == "Module" then
		for moduleName, summary in pairs(self.modules or {}) do
			local haystack = lower(moduleName)
			if query == "" or string.find(haystack, query, 1, true) then
				table.insert(results, { kind = "Module", name = moduleName, data = summary, display = moduleName })
				if #results >= limit then return results end
			end
		end
		table.sort(results, function(a, b) return tostring(a.display) < tostring(b.display) end)
		return results
	end
	for _, fileInfo in ipairs(self.files or {}) do
		local fileType = fileInfo.ty or "other"
		if filter == "All" or string.lower(filter) == fileType then
			local haystack = lower((fileInfo.sp or "") .. " " .. (fileInfo.ap or "") .. " " .. (fileInfo.mod or "") .. " " .. fileType)
			if query == "" or string.find(haystack, query, 1, true) then
				table.insert(results, { kind = "File", data = fileInfo, display = fileInfo.ap or fileInfo.sp or "?" })
				if #results >= limit then return results end
			end
		end
	end
	return results
end

function Resources:DescribeEntry(entry)
	if not entry then
		return "No resource selected."
	end
	local data = entry.data or {}
	local lines = {}
	if entry.kind == "File" then
		table.insert(lines, "File")
		table.insert(lines, "sourcePath: " .. tostring(data.sp or "?"))
		table.insert(lines, "addonPath: " .. tostring(data.ap or self:ToAddonPath(data.sp)))
		table.insert(lines, "type: " .. tostring(data.ty or "?"))
		table.insert(lines, "module: " .. tostring(data.mod or "?"))
		if ApiVault.ModuleCatalog and ApiVault.ModuleCatalog.GetModuleSummary and data.mod then
			local summary = ApiVault.ModuleCatalog:GetModuleSummary(data.mod)
			if summary then
				table.insert(lines, "moduleTOCCount: " .. tostring(summary.tocEntries or 0))
				table.insert(lines, "moduleLoadOnDemand: " .. tostring(summary.loadOnDemand or ""))
				table.insert(lines, "moduleAllowLoadGameType: " .. tostring(summary.allowLoadGameType or ""))
			end
		end
		table.insert(lines, "size: " .. tostring(data.bytes or 0) .. " bytes")
		table.insert(lines, "lineCount: " .. tostring(data.lines or 0))
		table.insert(lines, "sha256: " .. tostring(data.sha or "?"))
		table.insert(lines, "apiUsageCount: " .. tostring(data.api or 0))
		table.insert(lines, "eventUsageCount: " .. tostring(data.evt or 0))
		local xmlTemplateCount = data.xml or 0
			if ApiVault.XMLCatalog and ApiVault.XMLCatalog.GetFileTemplateCount then
				xmlTemplateCount = ApiVault.XMLCatalog:GetFileTemplateCount(data.sp) or xmlTemplateCount
			end
			table.insert(lines, "xmlTemplateCount: " .. tostring(xmlTemplateCount))
		if type(data.toc) == "table" then
			table.insert(lines, "")
			table.insert(lines, "tocMetadata:")
			for key, value in pairs(data.toc) do
				table.insert(lines, "- " .. tostring(key) .. ": " .. tostring(value))
			end
		end
	elseif entry.kind == "Folder" then
		table.insert(lines, "Folder")
		table.insert(lines, "sourcePath: " .. tostring(data.sp or "?"))
		table.insert(lines, "addonPath: " .. tostring(data.ap or self:ToAddonPath(data.sp)))
		table.insert(lines, "direct files: " .. tostring(data.files or 0))
		table.insert(lines, "direct folders: " .. tostring(data.dirs or 0))
		table.insert(lines, "recursive files: " .. tostring(data.total or 0))
		table.insert(lines, "recursive size: " .. tostring(data.bytes or 0) .. " bytes")
		table.insert(lines, "lua/xml/toc/other: " .. tostring(data.lua or 0) .. "/" .. tostring(data.xml or 0) .. "/" .. tostring(data.toc or 0) .. "/" .. tostring(data.other or 0))
	elseif entry.kind == "Module" then
		table.insert(lines, "Module")
		table.insert(lines, "name: " .. tostring(entry.name or "?"))
		table.insert(lines, "files: " .. tostring(data.files or 0))
		table.insert(lines, "lua/xml/toc/other: " .. tostring(data.lua or 0) .. "/" .. tostring(data.xml or 0) .. "/" .. tostring(data.toc or 0) .. "/" .. tostring(data.other or 0))
		table.insert(lines, "size: " .. tostring(data.size or 0) .. " bytes")
		if ApiVault.ModuleCatalog and ApiVault.ModuleCatalog.GetModuleSummary then
			local summary = ApiVault.ModuleCatalog:GetModuleSummary(entry.name)
			if summary then
				table.insert(lines, "moduleTOCCount: " .. tostring(summary.tocEntries or 0))
				table.insert(lines, "moduleLoadOnDemand: " .. tostring(summary.loadOnDemand or ""))
				table.insert(lines, "moduleAllowLoadGameType: " .. tostring(summary.allowLoadGameType or ""))
			end
		end
		table.insert(lines, "apiUsageCount: " .. tostring(data.api or 0))
		table.insert(lines, "eventUsageCount: " .. tostring(data.event or 0))
		local moduleXmlTemplates = data.xmlTemplates or 0
			if ApiVault.XMLCatalog and ApiVault.XMLCatalog.GetModuleTemplateCount then
				moduleXmlTemplates = ApiVault.XMLCatalog:GetModuleTemplateCount(entry.name) or moduleXmlTemplates
			end
			table.insert(lines, "xmlTemplateCount: " .. tostring(moduleXmlTemplates))
	end
	if ApiVault.SecurePatterns then
		if entry.kind == "File" then
			lines = { ApiVault.SecurePatterns:AppendFileSummary(table.concat(lines, "\n"), data.sp, 5) }
		elseif entry.kind == "Module" then
			lines = { ApiVault.SecurePatterns:AppendModuleSummaryText(table.concat(lines, "\n"), entry.name, 6) }
		end
	end
	table.insert(lines, "")
	table.insert(lines, "Note: this is generated reference metadata. ApiVault does not read or execute these source files at runtime.")
	return table.concat(lines, "\n")
end
