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