/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-29d58ba | Cube Dev Team */
//#region src/tasty/parser/lru.ts
var Lru = class {
	map = /* @__PURE__ */ new Map();
	head = null;
	tail = null;
	onEvict;
	constructor(limit = 1e3, onEvict) {
		this.limit = limit;
		let normalized = Number.isFinite(this.limit) ? Math.floor(this.limit) : 1e3;
		if (normalized <= 0) normalized = 1e3;
		this.limit = normalized;
		this.onEvict = onEvict;
	}
	setOnEvict(fn) {
		this.onEvict = fn;
	}
	get(key) {
		const node = this.map.get(key);
		if (!node) return void 0;
		this.touch(key, node);
		return node.value;
	}
	set(key, value) {
		let node = this.map.get(key);
		if (node) {
			node.value = value;
			this.touch(key, node);
			return;
		}
		node = {
			prev: null,
			next: this.head,
			value
		};
		if (this.head) {
			const headNode = this.map.get(this.head);
			if (headNode) headNode.prev = key;
		}
		this.head = key;
		if (!this.tail) this.tail = key;
		this.map.set(key, node);
		if (this.map.size > this.limit) this.evict();
	}
	delete(key) {
		const node = this.map.get(key);
		if (!node) return;
		if (node.prev) {
			const prevNode = this.map.get(node.prev);
			if (prevNode) prevNode.next = node.next;
		}
		if (node.next) {
			const nextNode = this.map.get(node.next);
			if (nextNode) nextNode.prev = node.prev;
		}
		if (this.head === key) this.head = node.next;
		if (this.tail === key) this.tail = node.prev;
		this.map.delete(key);
	}
	keys() {
		return this.map.keys();
	}
	touch(key, node) {
		if (this.head === key) return;
		if (node.prev) {
			const prevNode = this.map.get(node.prev);
			if (prevNode) prevNode.next = node.next;
		}
		if (node.next) {
			const nextNode = this.map.get(node.next);
			if (nextNode) nextNode.prev = node.prev;
		}
		if (this.tail === key) this.tail = node.prev;
		node.prev = null;
		node.next = this.head;
		if (this.head) {
			const headNode = this.map.get(this.head);
			if (headNode) headNode.prev = key;
		}
		this.head = key;
	}
	evict() {
		const old = this.tail;
		if (!old) return;
		const node = this.map.get(old);
		if (!node) {
			if (this.head === old) this.head = null;
			this.tail = null;
			return;
		}
		if (node.prev) {
			const prevNode = this.map.get(node.prev);
			if (prevNode) prevNode.next = null;
		}
		this.tail = node.prev;
		if (this.head === old) this.head = null;
		this.map.delete(old);
		if (this.onEvict) try {
			this.onEvict(old, node.value);
		} catch {}
	}
	clear() {
		this.map.clear();
		this.head = this.tail = null;
	}
};

//#endregion
export { Lru };
//# sourceMappingURL=lru.js.map