var DisposableCache = class {
	constructor() {
		this.toDispose = [], this.isDisposed = !1;
	}
	onDispose(e) {
		this.toDispose.push(e);
	}
	dispose() {
		this.throwIfDisposed(), this.clear(), this.isDisposed = !0, this.toDispose.forEach((e) => e.dispose());
	}
	throwIfDisposed() {
		if (this.isDisposed) throw Error("This cache has already been disposed");
	}
}, SimpleCache = class extends DisposableCache {
	constructor() {
		super(...arguments), this.cache = /* @__PURE__ */ new Map();
	}
	has(e) {
		return this.throwIfDisposed(), this.cache.has(e);
	}
	set(e, n) {
		this.throwIfDisposed(), this.cache.set(e, n);
	}
	get(e, n) {
		if (this.throwIfDisposed(), this.cache.has(e)) return this.cache.get(e);
		if (n) {
			let r = n();
			return this.cache.set(e, r), r;
		} else return;
	}
	delete(e) {
		return this.throwIfDisposed(), this.cache.delete(e);
	}
	clear() {
		this.throwIfDisposed(), this.cache.clear();
	}
}, ContextCache = class extends DisposableCache {
	constructor(e) {
		super(), this.cache = /* @__PURE__ */ new Map(), this.converter = e ?? ((e) => e);
	}
	has(e, n) {
		return this.throwIfDisposed(), this.cacheForContext(e).has(n);
	}
	set(e, n, r) {
		this.throwIfDisposed(), this.cacheForContext(e).set(n, r);
	}
	get(e, n, r) {
		this.throwIfDisposed();
		let i = this.cacheForContext(e);
		if (i.has(n)) return i.get(n);
		if (r) {
			let e = r();
			return i.set(n, e), e;
		} else return;
	}
	delete(e, n) {
		return this.throwIfDisposed(), this.cacheForContext(e).delete(n);
	}
	clear(e) {
		if (this.throwIfDisposed(), e) {
			let n = this.converter(e);
			this.cache.delete(n);
		} else this.cache.clear();
	}
	cacheForContext(e) {
		let n = this.converter(e), r = this.cache.get(n);
		return r || (r = /* @__PURE__ */ new Map(), this.cache.set(n, r)), r;
	}
}, DocumentCache = class extends ContextCache {
	constructor(e, n) {
		super((e) => e.toString()), n ? (this.toDispose.push(e.workspace.DocumentBuilder.onDocumentPhase(n, (e) => {
			this.clear(e.uri.toString());
		})), this.toDispose.push(e.workspace.DocumentBuilder.onUpdate((e, n) => {
			for (let e of n) this.clear(e);
		}))) : this.toDispose.push(e.workspace.DocumentBuilder.onUpdate((e, n) => {
			let r = e.concat(n);
			for (let e of r) this.clear(e);
		}));
	}
}, WorkspaceCache = class extends SimpleCache {
	constructor(e, n) {
		super(), n ? (this.toDispose.push(e.workspace.DocumentBuilder.onBuildPhase(n, () => {
			this.clear();
		})), this.toDispose.push(e.workspace.DocumentBuilder.onUpdate((e, n) => {
			n.length > 0 && this.clear();
		}))) : this.toDispose.push(e.workspace.DocumentBuilder.onUpdate(() => {
			this.clear();
		}));
	}
};
export { ContextCache, DisposableCache, DocumentCache, SimpleCache, WorkspaceCache };
