import { setNodeLocationFull as i, setNodeLocationOnlyOffset as n, addTerminalToCst as c, addNoneTerminalToCst as l } from "../../cst/cst.js";
import { createBaseSemanticVisitorConstructor as h, createBaseVisitorConstructorWithDefaults as d } from "../../cst/cst_visitor.js";
import { DEFAULT_PARSER_CONFIG as f } from "../parser.js";
import N from "../../../../../../../../lodash-es@4.17.21/node_modules/lodash-es/has.js";
import s from "../../../../../../../../lodash-es@4.17.21/node_modules/lodash-es/noop.js";
import a from "../../../../../../../../lodash-es@4.17.21/node_modules/lodash-es/isUndefined.js";
import r from "../../../../../../../../lodash-es@4.17.21/node_modules/lodash-es/keys.js";
class p {
  initTreeBuilder(t) {
    if (this.CST_STACK = [], this.outputCst = t.outputCst, this.nodeLocationTracking = N(t, "nodeLocationTracking") ? t.nodeLocationTracking : f.nodeLocationTracking, !this.outputCst)
      this.cstInvocationStateUpdate = s, this.cstFinallyStateUpdate = s, this.cstPostTerminal = s, this.cstPostNonTerminal = s, this.cstPostRule = s;
    else if (/full/i.test(this.nodeLocationTracking))
      this.recoveryEnabled ? (this.setNodeLocationFromToken = i, this.setNodeLocationFromNode = i, this.cstPostRule = s, this.setInitialNodeLocation = this.setInitialNodeLocationFullRecovery) : (this.setNodeLocationFromToken = s, this.setNodeLocationFromNode = s, this.cstPostRule = this.cstPostRuleFull, this.setInitialNodeLocation = this.setInitialNodeLocationFullRegular);
    else if (/onlyOffset/i.test(this.nodeLocationTracking))
      this.recoveryEnabled ? (this.setNodeLocationFromToken = n, this.setNodeLocationFromNode = n, this.cstPostRule = s, this.setInitialNodeLocation = this.setInitialNodeLocationOnlyOffsetRecovery) : (this.setNodeLocationFromToken = s, this.setNodeLocationFromNode = s, this.cstPostRule = this.cstPostRuleOnlyOffset, this.setInitialNodeLocation = this.setInitialNodeLocationOnlyOffsetRegular);
    else if (/none/i.test(this.nodeLocationTracking))
      this.setNodeLocationFromToken = s, this.setNodeLocationFromNode = s, this.cstPostRule = s, this.setInitialNodeLocation = s;
    else
      throw Error(`Invalid <nodeLocationTracking> config option: "${t.nodeLocationTracking}"`);
  }
  setInitialNodeLocationOnlyOffsetRecovery(t) {
    t.location = {
      startOffset: NaN,
      endOffset: NaN
    };
  }
  setInitialNodeLocationOnlyOffsetRegular(t) {
    t.location = {
      // without error recovery the starting Location of a new CstNode is guaranteed
      // To be the next Token's startOffset (for valid inputs).
      // For invalid inputs there won't be any CSTOutput so this potential
      // inaccuracy does not matter
      startOffset: this.LA(1).startOffset,
      endOffset: NaN
    };
  }
  setInitialNodeLocationFullRecovery(t) {
    t.location = {
      startOffset: NaN,
      startLine: NaN,
      startColumn: NaN,
      endOffset: NaN,
      endLine: NaN,
      endColumn: NaN
    };
  }
  /**
       *  @see setInitialNodeLocationOnlyOffsetRegular for explanation why this work
  
       * @param cstNode
       */
  setInitialNodeLocationFullRegular(t) {
    const o = this.LA(1);
    t.location = {
      startOffset: o.startOffset,
      startLine: o.startLine,
      startColumn: o.startColumn,
      endOffset: NaN,
      endLine: NaN,
      endColumn: NaN
    };
  }
  cstInvocationStateUpdate(t) {
    const o = {
      name: t,
      children: /* @__PURE__ */ Object.create(null)
    };
    this.setInitialNodeLocation(o), this.CST_STACK.push(o);
  }
  cstFinallyStateUpdate() {
    this.CST_STACK.pop();
  }
  cstPostRuleFull(t) {
    const o = this.LA(0), e = t.location;
    e.startOffset <= o.startOffset ? (e.endOffset = o.endOffset, e.endLine = o.endLine, e.endColumn = o.endColumn) : (e.startOffset = NaN, e.startLine = NaN, e.startColumn = NaN);
  }
  cstPostRuleOnlyOffset(t) {
    const o = this.LA(0), e = t.location;
    e.startOffset <= o.startOffset ? e.endOffset = o.endOffset : e.startOffset = NaN;
  }
  cstPostTerminal(t, o) {
    const e = this.CST_STACK[this.CST_STACK.length - 1];
    c(e, o, t), this.setNodeLocationFromToken(e.location, o);
  }
  cstPostNonTerminal(t, o) {
    const e = this.CST_STACK[this.CST_STACK.length - 1];
    l(e, o, t), this.setNodeLocationFromNode(e.location, t.location);
  }
  getBaseCstVisitorConstructor() {
    if (a(this.baseCstVisitorConstructor)) {
      const t = h(this.className, r(this.gastProductionsCache));
      return this.baseCstVisitorConstructor = t, t;
    }
    return this.baseCstVisitorConstructor;
  }
  getBaseCstVisitorConstructorWithDefaults() {
    if (a(this.baseCstVisitorWithDefaultsConstructor)) {
      const t = d(this.className, r(this.gastProductionsCache), this.getBaseCstVisitorConstructor());
      return this.baseCstVisitorWithDefaultsConstructor = t, t;
    }
    return this.baseCstVisitorWithDefaultsConstructor;
  }
  getLastExplicitRuleShortName() {
    const t = this.RULE_STACK;
    return t[t.length - 1];
  }
  getPreviousExplicitRuleShortName() {
    const t = this.RULE_STACK;
    return t[t.length - 2];
  }
  getLastExplicitRuleOccurrenceIndex() {
    const t = this.RULE_OCCURRENCE_STACK;
    return t[t.length - 1];
  }
}
export {
  p as TreeBuilder
};
