"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const syntaxTree = require("./lib-syntax-tree.cjs");
const astUtils = require("./lib-utils-ast-utils.cjs");
const cstUtils = require("./lib-utils-cst-utils.cjs");
const promiseUtils = require("./lib-utils-promise-utils.cjs");
const uriUtils = require("./lib-utils-uri-utils.cjs");
const cancellation = require("../../_virtual/cancellation.cjs");
class DefaultAstNodeDescriptionProvider {
  constructor(services) {
    this.astNodeLocator = services.workspace.AstNodeLocator;
    this.nameProvider = services.references.NameProvider;
  }
  createDescription(node, name, document) {
    const doc = document !== null && document !== void 0 ? document : astUtils.getDocument(node);
    name !== null && name !== void 0 ? name : name = this.nameProvider.getName(node);
    const path = this.astNodeLocator.getAstNodePath(node);
    if (!name) {
      throw new Error(`Node at path ${path} has no name.`);
    }
    let nameNodeSegment;
    const nameSegmentGetter = () => {
      var _a;
      return nameNodeSegment !== null && nameNodeSegment !== void 0 ? nameNodeSegment : nameNodeSegment = cstUtils.toDocumentSegment((_a = this.nameProvider.getNameNode(node)) !== null && _a !== void 0 ? _a : node.$cstNode);
    };
    return {
      node,
      name,
      get nameSegment() {
        return nameSegmentGetter();
      },
      selectionSegment: cstUtils.toDocumentSegment(node.$cstNode),
      type: node.$type,
      documentUri: doc.uri,
      path
    };
  }
}
class DefaultReferenceDescriptionProvider {
  constructor(services) {
    this.nodeLocator = services.workspace.AstNodeLocator;
  }
  async createDescriptions(document, cancelToken = cancellation.cancellationExports.CancellationToken.None) {
    const descr = [];
    const rootNode = document.parseResult.value;
    for (const astNode of astUtils.streamAst(rootNode)) {
      await promiseUtils.interruptAndCheck(cancelToken);
      astUtils.streamReferences(astNode).filter((refInfo) => !syntaxTree.isLinkingError(refInfo)).forEach((refInfo) => {
        const description = this.createDescription(refInfo);
        if (description) {
          descr.push(description);
        }
      });
    }
    return descr;
  }
  createDescription(refInfo) {
    const targetNodeDescr = refInfo.reference.$nodeDescription;
    const refCstNode = refInfo.reference.$refNode;
    if (!targetNodeDescr || !refCstNode) {
      return void 0;
    }
    const docUri = astUtils.getDocument(refInfo.container).uri;
    return {
      sourceUri: docUri,
      sourcePath: this.nodeLocator.getAstNodePath(refInfo.container),
      targetUri: targetNodeDescr.documentUri,
      targetPath: targetNodeDescr.path,
      segment: cstUtils.toDocumentSegment(refCstNode),
      local: uriUtils.UriUtils.equals(targetNodeDescr.documentUri, docUri)
    };
  }
}
exports.DefaultAstNodeDescriptionProvider = DefaultAstNodeDescriptionProvider;
exports.DefaultReferenceDescriptionProvider = DefaultReferenceDescriptionProvider;
//# sourceMappingURL=lib-workspace-ast-descriptions.cjs.map
