import { __commonJSMin } from "../../../../_virtual/rolldown_runtime.js";
var require_linkedMap = /* @__PURE__ */ __commonJSMin(((e) => {
	var t;
	Object.defineProperty(e, "__esModule", { value: !0 }), e.LRUCache = e.LinkedMap = e.Touch = void 0;
	var n;
	(function(e) {
		e.None = 0, e.First = 1, e.AsOld = e.First, e.Last = 2, e.AsNew = e.Last;
	})(n || (e.Touch = n = {}));
	var r = class {
		constructor() {
			this[t] = "LinkedMap", this._map = /* @__PURE__ */ new Map(), this._head = void 0, this._tail = void 0, this._size = 0, this._state = 0;
		}
		clear() {
			this._map.clear(), this._head = void 0, this._tail = void 0, this._size = 0, this._state++;
		}
		isEmpty() {
			return !this._head && !this._tail;
		}
		get size() {
			return this._size;
		}
		get first() {
			return this._head?.value;
		}
		get last() {
			return this._tail?.value;
		}
		has(e) {
			return this._map.has(e);
		}
		get(e, t = n.None) {
			let r = this._map.get(e);
			if (r) return t !== n.None && this.touch(r, t), r.value;
		}
		set(e, t, r = n.None) {
			let i = this._map.get(e);
			if (i) i.value = t, r !== n.None && this.touch(i, r);
			else {
				switch (i = {
					key: e,
					value: t,
					next: void 0,
					previous: void 0
				}, r) {
					case n.None:
						this.addItemLast(i);
						break;
					case n.First:
						this.addItemFirst(i);
						break;
					case n.Last:
						this.addItemLast(i);
						break;
					default:
						this.addItemLast(i);
						break;
				}
				this._map.set(e, i), this._size++;
			}
			return this;
		}
		delete(e) {
			return !!this.remove(e);
		}
		remove(e) {
			let t = this._map.get(e);
			if (t) return this._map.delete(e), this.removeItem(t), this._size--, t.value;
		}
		shift() {
			if (!this._head && !this._tail) return;
			if (!this._head || !this._tail) throw Error("Invalid list");
			let e = this._head;
			return this._map.delete(e.key), this.removeItem(e), this._size--, e.value;
		}
		forEach(e, t) {
			let n = this._state, r = this._head;
			for (; r;) {
				if (t ? e.bind(t)(r.value, r.key, this) : e(r.value, r.key, this), this._state !== n) throw Error("LinkedMap got modified during iteration.");
				r = r.next;
			}
		}
		keys() {
			let e = this._state, t = this._head, n = {
				[Symbol.iterator]: () => n,
				next: () => {
					if (this._state !== e) throw Error("LinkedMap got modified during iteration.");
					if (t) {
						let e = {
							value: t.key,
							done: !1
						};
						return t = t.next, e;
					} else return {
						value: void 0,
						done: !0
					};
				}
			};
			return n;
		}
		values() {
			let e = this._state, t = this._head, n = {
				[Symbol.iterator]: () => n,
				next: () => {
					if (this._state !== e) throw Error("LinkedMap got modified during iteration.");
					if (t) {
						let e = {
							value: t.value,
							done: !1
						};
						return t = t.next, e;
					} else return {
						value: void 0,
						done: !0
					};
				}
			};
			return n;
		}
		entries() {
			let e = this._state, t = this._head, n = {
				[Symbol.iterator]: () => n,
				next: () => {
					if (this._state !== e) throw Error("LinkedMap got modified during iteration.");
					if (t) {
						let e = {
							value: [t.key, t.value],
							done: !1
						};
						return t = t.next, e;
					} else return {
						value: void 0,
						done: !0
					};
				}
			};
			return n;
		}
		[(t = Symbol.toStringTag, Symbol.iterator)]() {
			return this.entries();
		}
		trimOld(e) {
			if (e >= this.size) return;
			if (e === 0) {
				this.clear();
				return;
			}
			let t = this._head, n = this.size;
			for (; t && n > e;) this._map.delete(t.key), t = t.next, n--;
			this._head = t, this._size = n, t && (t.previous = void 0), this._state++;
		}
		addItemFirst(e) {
			if (!this._head && !this._tail) this._tail = e;
			else if (this._head) e.next = this._head, this._head.previous = e;
			else throw Error("Invalid list");
			this._head = e, this._state++;
		}
		addItemLast(e) {
			if (!this._head && !this._tail) this._head = e;
			else if (this._tail) e.previous = this._tail, this._tail.next = e;
			else throw Error("Invalid list");
			this._tail = e, this._state++;
		}
		removeItem(e) {
			if (e === this._head && e === this._tail) this._head = void 0, this._tail = void 0;
			else if (e === this._head) {
				if (!e.next) throw Error("Invalid list");
				e.next.previous = void 0, this._head = e.next;
			} else if (e === this._tail) {
				if (!e.previous) throw Error("Invalid list");
				e.previous.next = void 0, this._tail = e.previous;
			} else {
				let t = e.next, n = e.previous;
				if (!t || !n) throw Error("Invalid list");
				t.previous = n, n.next = t;
			}
			e.next = void 0, e.previous = void 0, this._state++;
		}
		touch(e, t) {
			if (!this._head || !this._tail) throw Error("Invalid list");
			if (!(t !== n.First && t !== n.Last)) {
				if (t === n.First) {
					if (e === this._head) return;
					let t = e.next, n = e.previous;
					e === this._tail ? (n.next = void 0, this._tail = n) : (t.previous = n, n.next = t), e.previous = void 0, e.next = this._head, this._head.previous = e, this._head = e, this._state++;
				} else if (t === n.Last) {
					if (e === this._tail) return;
					let t = e.next, n = e.previous;
					e === this._head ? (t.previous = void 0, this._head = t) : (t.previous = n, n.next = t), e.next = void 0, e.previous = this._tail, this._tail.next = e, this._tail = e, this._state++;
				}
			}
		}
		toJSON() {
			let e = [];
			return this.forEach((t, n) => {
				e.push([n, t]);
			}), e;
		}
		fromJSON(e) {
			this.clear();
			for (let [t, n] of e) this.set(t, n);
		}
	};
	e.LinkedMap = r, e.LRUCache = class extends r {
		constructor(e, t = 1) {
			super(), this._limit = e, this._ratio = Math.min(Math.max(0, t), 1);
		}
		get limit() {
			return this._limit;
		}
		set limit(e) {
			this._limit = e, this.checkTrim();
		}
		get ratio() {
			return this._ratio;
		}
		set ratio(e) {
			this._ratio = Math.min(Math.max(0, e), 1), this.checkTrim();
		}
		get(e, t = n.AsNew) {
			return super.get(e, t);
		}
		peek(e) {
			return super.get(e, n.None);
		}
		set(e, t) {
			return super.set(e, t, n.Last), this.checkTrim(), this;
		}
		checkTrim() {
			this.size > this._limit && this.trimOld(Math.round(this._limit * this._ratio));
		}
	};
}));
export default require_linkedMap();
export { require_linkedMap };
