import { stream } from "../utils/stream.js";
import { getDocument } from "../utils/ast-utils.js";
import { cancellation_exports } from "../utils/cancellation.js";
import { UriUtils } from "../utils/uri-utils.js";
import { ContextCache } from "../utils/caching.js";
var DefaultIndexManager = class {
	constructor(e) {
		this.symbolIndex = /* @__PURE__ */ new Map(), this.symbolByTypeIndex = new ContextCache(), this.referenceIndex = /* @__PURE__ */ new Map(), this.documents = e.workspace.LangiumDocuments, this.serviceRegistry = e.ServiceRegistry, this.astReflection = e.AstReflection;
	}
	findAllReferences(n, i) {
		let a = getDocument(n).uri, o = [];
		return this.referenceIndex.forEach((e) => {
			e.forEach((e) => {
				UriUtils.equals(e.targetUri, a) && e.targetPath === i && o.push(e);
			});
		}), stream(o);
	}
	allElements(t, n) {
		let r = stream(this.symbolIndex.keys());
		return n && (r = r.filter((e) => !n || n.has(e))), r.map((e) => this.getFileDescriptions(e, t)).flat();
	}
	getFileDescriptions(e, t) {
		return t ? this.symbolByTypeIndex.get(e, t, () => (this.symbolIndex.get(e) ?? []).filter((e) => this.astReflection.isSubtype(e.type, t))) : this.symbolIndex.get(e) ?? [];
	}
	remove(e) {
		this.removeContent(e), this.removeReferences(e);
	}
	removeContent(e) {
		let t = e.toString();
		this.symbolIndex.delete(t), this.symbolByTypeIndex.clear(t);
	}
	removeReferences(e) {
		let t = e.toString();
		this.referenceIndex.delete(t);
	}
	async updateContent(e, t = cancellation_exports.CancellationToken.None) {
		let r = await this.serviceRegistry.getServices(e.uri).references.ScopeComputation.collectExportedSymbols(e, t), i = e.uri.toString();
		this.symbolIndex.set(i, r), this.symbolByTypeIndex.clear(i);
	}
	async updateReferences(e, t = cancellation_exports.CancellationToken.None) {
		let r = await this.serviceRegistry.getServices(e.uri).workspace.ReferenceDescriptionProvider.createDescriptions(e, t);
		this.referenceIndex.set(e.uri.toString(), r);
	}
	isAffected(e, t) {
		let n = this.referenceIndex.get(e.uri.toString());
		return n ? n.some((e) => !e.local && t.has(e.targetUri.toString())) : !1;
	}
};
export { DefaultIndexManager };
