-- ApiVault XML Template Catalog runtime helpers. Generated for ApiVault v0.2.4.
-- Compact metadata only. No XML source is read at runtime and no bundled XML is TOC-loaded.
local addonName, ApiVault = ...

ApiVault.XMLCatalog = ApiVault.XMLCatalog or {}
local XML = ApiVault.XMLCatalog

XML.templates = XML.templates or {}
XML.byName = XML.byName or {}
XML.byFile = XML.byFile or {}
XML.inheritedBy = XML.inheritedBy or {}
XML.scriptHandlers = XML.scriptHandlers or {}
XML.modules = XML.modules or {}
XML.fileCounts = XML.fileCounts or {}
XML.metadata = {
	generated_at = "2026-06-28T12:02:08.634398+00:00",
	interface = 120007,
	resource_root = "Interface/AddOns/ApiVault/Examples/Interface",
	scripted_templates = 2807,
	secure_templates = 56,
	source_archive = "Interface.zip",
	template_entries = 8066,
	type_counts = {
		Alpha = 2,
		Animation = 2,
		AnimationGroup = 17,
		Browser = 6,
		Button = 1797,
		CheckButton = 206,
		CinematicModel = 4,
		Cooldown = 16,
		DressUpModel = 5,
		EditBox = 76,
		FogOfWarFrame = 2,
		FontString = 684,
		Frame = 2876,
		GameTooltip = 42,
		Line = 5,
		MaskTexture = 2,
		MessageFrame = 1,
		ModelScene = 20,
		PlayerModel = 11,
		ScrollFrame = 62,
		ScrollingMessageFrame = 14,
		SimpleHTML = 3,
		Slider = 32,
		StatusBar = 110,
		Texture = 2071,
	},
	version = "0.2.4",
	virtual_templates = 3240,
	xml_files_scanned = 1028,
}

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

function XML:RegisterTemplateShard(shard)
	if type(shard) ~= "table" then
		return
	end
	for _, entry in ipairs(shard) do
		table.insert(self.templates, entry)
		if entry.n then
			self.byName[entry.n] = entry
		end
		if entry.sp then
			self.byFile[entry.sp] = self.byFile[entry.sp] or {}
			table.insert(self.byFile[entry.sp], entry)
		end
	end
end

function XML:RegisterInheritanceData(data)
	if type(data) ~= "table" then
		return
	end
	self.inheritedBy = data.inheritedBy or self.inheritedBy or {}
	self.fileCounts = data.fileCounts or self.fileCounts or {}
end

function XML:RegisterScriptData(data)
	if type(data) ~= "table" then
		return
	end
	self.scriptHandlers = data.handlers or self.scriptHandlers or {}
end

function XML:RegisterModuleData(data)
	if type(data) ~= "table" then
		return
	end
	self.modules = data
end

function XML:GetStats()
	local meta = self.metadata or {}
	return meta.template_entries or #self.templates, meta.xml_files_scanned or 0, meta.virtual_templates or 0, meta.secure_templates or 0
end

function XML:GetTemplate(name)
	return self.byName and self.byName[name]
end

function XML:GetFileTemplateCount(sourcePath)
	local info = self.fileCounts and self.fileCounts[sourcePath]
	return info and info.templates or 0
end

function XML:GetModuleTemplateCount(moduleName)
	local info = self.modules and self.modules[moduleName]
	return info and info.templates or 0
end

function XML:Search(query, filter, limit)
	query = lower(query)
	filter = filter or "All"
	limit = limit or 300
	local results = {}
	for _, entry in ipairs(self.templates or {}) do
		local include = false
		if filter == "All" then
			include = true
		elseif filter == "Virtual" then
			include = entry.v == 1
		elseif filter == "Secure" then
			include = entry.sec == 1
		elseif filter == "Scripted" then
			include = type(entry.s) == "table" and #entry.s > 0
		else
			include = entry.ty == filter
		end
		if include then
			local haystack = tostring(entry.n or "") .. " " .. tostring(entry.ty or "") .. " " .. tostring(entry.inh or "") .. " " .. tostring(entry.mix or "") .. " " .. tostring(entry.sp or "") .. " " .. tostring(entry.mod or "")
			if contains(haystack, query) then
				table.insert(results, { kind = "XMLTemplate", data = entry, display = tostring(entry.n or "?") })
				if #results >= limit then return results end
			end
		end
	end
	return results
end

function XML:DescribeTemplate(entryOrResult)
	local entry = entryOrResult and (entryOrResult.data or entryOrResult) or nil
	if not entry then
		return "No XML template selected."
	end
	local lines = {}
	table.insert(lines, "XML template/frame metadata")
	table.insert(lines, "name: " .. tostring(entry.n or "?"))
	table.insert(lines, "type: " .. tostring(entry.ty or "?"))
	table.insert(lines, "kind: " .. tostring(entry.k or (entry.v == 1 and "template" or "frame")))
	table.insert(lines, "virtual: " .. tostring(entry.v == 1))
	table.insert(lines, "secure/reference flag: " .. tostring(entry.sec == 1))
	table.insert(lines, "module: " .. tostring(entry.mod or "?"))
	table.insert(lines, "sourcePath: " .. tostring(entry.sp or "?"))
	table.insert(lines, "addonPath: " .. tostring(entry.ap or "?"))
	table.insert(lines, "line: " .. tostring(entry.ln or "?"))
	if entry.inh then
		table.insert(lines, "inherits: " .. tostring(entry.inh))
	end
	if type(entry.ih) == "table" and #entry.ih > 0 then
		table.insert(lines, "inherits list:")
		for _, parent in ipairs(entry.ih) do
			table.insert(lines, "- " .. tostring(parent))
		end
	end
	if entry.mix then
		table.insert(lines, "mixin: " .. tostring(entry.mix))
	end
	if entry.pk then
		table.insert(lines, "parentKey: " .. tostring(entry.pk))
	end
	if entry.strata then
		table.insert(lines, "frameStrata: " .. tostring(entry.strata))
	end
	if type(entry.s) == "table" and #entry.s > 0 then
		table.insert(lines, "")
		table.insert(lines, "script handlers:")
		for _, handler in ipairs(entry.s) do
			table.insert(lines, "- " .. tostring(handler))
		end
	end
	local children = entry.n and self.inheritedBy and self.inheritedBy[entry.n]
	if type(children) == "table" and #children > 0 then
		table.insert(lines, "")
		table.insert(lines, "inherited by samples:")
		for index, childName in ipairs(children) do
			if index > 20 then break end
			table.insert(lines, "- " .. tostring(childName))
		end
	end
	if ApiVault.SecurePatterns and ApiVault.SecurePatterns.AppendTemplateSummary then
		lines = { ApiVault.SecurePatterns:AppendTemplateSummary(table.concat(lines, "\n"), entry.n, entry.sp, 6) }
	end
	table.insert(lines, "")
	table.insert(lines, "Note: generated metadata only. ApiVault does not read, display, or execute the bundled XML source files at runtime.")
	return table.concat(lines, "\n")
end
