import { createLogger } from "../utils/logger/index.js";
import { init_logger } from "../utils/logger.js";
//#region src/extensions/diagnostics.ts
init_logger();
const log = createLogger("Extension:Diagnostics");
const DEFAULT_CACHE_TTL = 300 * 1e3;
/**
* Simple in-memory cache with TTL support
*/
var ExtensionLoaderCacheImpl = class {
	cache = /* @__PURE__ */ new Map();
	/**
	* Build a cache key from loader options
	*/
	buildKey(options, enabledIds) {
		return JSON.stringify({
			workspaceDir: options.workspaceDir,
			globalDir: options.globalDir,
			bundledDir: options.bundledDir,
			enabledIds: enabledIds?.sort()
		});
	}
	/**
	* Get cached value
	*/
	get(key) {
		const entry = this.cache.get(key);
		if (!entry) return;
		if (Date.now() > entry.expiresAt) {
			this.cache.delete(key);
			return;
		}
		return entry.value;
	}
	/**
	* Set cached value with TTL
	*/
	set(key, value, ttl = DEFAULT_CACHE_TTL) {
		this.cache.set(key, {
			value,
			expiresAt: Date.now() + ttl
		});
	}
	/**
	* Invalidate cache
	*/
	invalidate(key) {
		if (key) this.cache.delete(key);
		else this.cache.clear();
		log.info(key ? `Cache invalidated for: ${key}` : "All cache invalidated");
	}
	/**
	* Check if cache exists and is valid
	*/
	has(key) {
		return this.get(key) !== void 0;
	}
	/**
	* Get cache stats
	*/
	getStats() {
		return {
			size: this.cache.size,
			keys: Array.from(this.cache.keys())
		};
	}
};
/**
* Extension diagnostics collector
*/
var ExtensionDiagnostics = class {
	diagnostics = [];
	maxDiagnostics = 1e3;
	/**
	* Add a diagnostic
	*/
	addDiagnostic(diagnostic) {
		if (this.diagnostics.length >= this.maxDiagnostics) this.diagnostics = this.diagnostics.slice(-this.maxDiagnostics / 2);
		this.diagnostics.push({
			...diagnostic,
			timestamp: diagnostic.timestamp || Date.now()
		});
		(log[diagnostic.level === "error" ? "error" : diagnostic.level === "warn" ? "warn" : "info"]?.bind(log) || (() => {}))({
			extensionId: diagnostic.extensionId,
			code: diagnostic.code
		}, diagnostic.message);
	}
	/**
	* Add info diagnostic
	*/
	info(extensionId, message, details) {
		this.addDiagnostic({
			level: "info",
			extensionId,
			message,
			details,
			timestamp: Date.now()
		});
	}
	/**
	* Add warning diagnostic
	*/
	warn(extensionId, message, details) {
		this.addDiagnostic({
			level: "warn",
			extensionId,
			message,
			details,
			timestamp: Date.now()
		});
	}
	/**
	* Add error diagnostic
	*/
	error(extensionId, message, details) {
		this.addDiagnostic({
			level: "error",
			extensionId,
			message,
			details,
			timestamp: Date.now()
		});
	}
	/**
	* Get all diagnostics
	*/
	getAll() {
		return [...this.diagnostics];
	}
	/**
	* Get diagnostics for a specific extension
	*/
	getForExtension(extensionId) {
		return this.diagnostics.filter((d) => d.extensionId === extensionId);
	}
	/**
	* Get diagnostics by level
	*/
	getByLevel(level) {
		return this.diagnostics.filter((d) => d.level === level);
	}
	/**
	* Get errors
	*/
	getErrors() {
		return this.getByLevel("error");
	}
	/**
	* Get warnings
	*/
	getWarnings() {
		return this.getByLevel("warn");
	}
	/**
	* Clear diagnostics
	*/
	clear() {
		this.diagnostics = [];
	}
	/**
	* Get summary
	*/
	getSummary() {
		return {
			errorCount: this.getByLevel("error").length,
			warnCount: this.getByLevel("warn").length,
			infoCount: this.getByLevel("info").length
		};
	}
};
let globalCache = null;
let globalDiagnostics = null;
function getExtensionCache() {
	if (!globalCache) globalCache = new ExtensionLoaderCacheImpl();
	return globalCache;
}
function getExtensionDiagnostics() {
	if (!globalDiagnostics) globalDiagnostics = new ExtensionDiagnostics();
	return globalDiagnostics;
}
function setExtensionCache(cache) {
	globalCache = cache;
}
function setExtensionDiagnostics(diagnostics) {
	globalDiagnostics = diagnostics;
}
//#endregion
export { ExtensionDiagnostics, ExtensionLoaderCacheImpl, getExtensionCache, getExtensionDiagnostics, setExtensionCache, setExtensionDiagnostics };

//# sourceMappingURL=diagnostics.js.map