//#region src/utils/LRUCache.ts
/**
* A simple LRU (Least Recently Used) cache implementation
*/
var LRUCache = class {
	constructor(maxSize) {
		this.cache = /* @__PURE__ */ new Map();
		this.maxSize = maxSize;
	}
	get(key) {
		const value = this.cache.get(key);
		if (value) {
			this.cache.delete(key);
			this.cache.set(key, value);
		}
		return value;
	}
	set(key, value) {
		if (this.cache.has(key)) this.cache.delete(key);
		else if (this.cache.size >= this.maxSize) {
			const firstKey = this.cache.keys().next().value;
			if (firstKey) this.cache.delete(firstKey);
		}
		this.cache.set(key, value);
	}
	has(key) {
		return this.cache.has(key);
	}
	delete(key) {
		return this.cache.delete(key);
	}
	clear() {
		this.cache.clear();
	}
	get size() {
		return this.cache.size;
	}
};
/**
* Size-aware LRU cache that tracks memory usage in bytes
* Evicts entries when total size exceeds the maximum
*/
var SizeAwareLRUCache = class {
	constructor(maxSizeBytes) {
		this.cache = /* @__PURE__ */ new Map();
		this.sizes = /* @__PURE__ */ new Map();
		this.currentSize = 0;
		this.maxSizeBytes = maxSizeBytes;
	}
	get(key) {
		const value = this.cache.get(key);
		if (value) {
			const size = this.sizes.get(key) || 0;
			this.cache.delete(key);
			this.cache.set(key, value);
			this.sizes.delete(key);
			this.sizes.set(key, size);
		}
		return value;
	}
	set(key, value) {
		if (this.cache.has(key)) {
			const oldSize = this.sizes.get(key) || 0;
			this.currentSize -= oldSize;
			this.cache.delete(key);
			this.sizes.delete(key);
		}
		const sizeTrackingPromise = value.then((buffer) => {
			const bufferSize = buffer.byteLength;
			this.sizes.set(key, bufferSize);
			this.currentSize += bufferSize;
			this.evictIfNecessary();
			return buffer;
		}).catch((error) => {
			this.cache.delete(key);
			this.sizes.delete(key);
			throw error;
		});
		sizeTrackingPromise.catch(() => {});
		this.cache.set(key, sizeTrackingPromise);
	}
	evictIfNecessary() {
		while (this.currentSize > this.maxSizeBytes && this.cache.size > 0) {
			const firstKey = this.cache.keys().next().value;
			if (firstKey) {
				const size = this.sizes.get(firstKey) || 0;
				this.currentSize -= size;
				this.cache.delete(firstKey);
				this.sizes.delete(firstKey);
			} else break;
		}
	}
	has(key) {
		return this.cache.has(key);
	}
	delete(key) {
		const size = this.sizes.get(key) || 0;
		this.currentSize -= size;
		this.sizes.delete(key);
		return this.cache.delete(key);
	}
	clear() {
		this.cache.clear();
		this.sizes.clear();
		this.currentSize = 0;
	}
	get size() {
		return this.cache.size;
	}
	get currentSizeBytes() {
		return this.currentSize;
	}
	get maxSize() {
		return this.maxSizeBytes;
	}
};

//#endregion
export { LRUCache, SizeAwareLRUCache };
//# sourceMappingURL=LRUCache.js.map