import { Rule as A, Option as N, RepetitionMandatory as S, RepetitionMandatoryWithSeparator as _, Repetition as T, RepetitionWithSeparator as g, NonTerminal as C, Terminal as L, Alternation as P, Alternative as D } from "../../../../../../../../@chevrotain_gast@11.0.3/node_modules/@chevrotain/gast/lib/src/model.js";
import { Lexer as $ } from "../../../scan/lexer_public.js";
import { augmentTokenTypes as v, hasShortKeyProperty as w } from "../../../scan/tokens.js";
import { createToken as M, createTokenInstance as b } from "../../../scan/tokens_public.js";
import { END_OF_FILE as K } from "../parser.js";
import { BITS_FOR_OCCURRENCE_IDX as k } from "../../grammar/keys.js";
import d from "../../../../../../../../lodash-es@4.17.21/node_modules/lodash-es/has.js";
import l from "../../../../../../../../lodash-es@4.17.21/node_modules/lodash-es/last.js";
import f from "../../../../../../../../lodash-es@4.17.21/node_modules/lodash-es/isFunction.js";
import y from "../../../../../../../../lodash-es@4.17.21/node_modules/lodash-es/some.js";
import G from "../../../../../../../../lodash-es@4.17.21/node_modules/lodash-es/forEach.js";
import x from "../../../../../../../../lodash-es@4.17.21/node_modules/lodash-es/isArray.js";
const u = {
  description: "This Object indicates the Parser is during Recording Phase"
};
Object.freeze(u);
const E = !0, O = Math.pow(2, k) - 1, p = M({ name: "RECORDING_PHASE_TOKEN", pattern: $.NA });
v([p]);
const I = b(
  p,
  `This IToken indicates the Parser is in Recording Phase
	See: https://chevrotain.io/docs/guide/internals.html#grammar-recording for details`,
  // Using "-1" instead of NaN (as in EOF) because an actual number is less likely to
  // cause errors if the output of LA or CONSUME would be (incorrectly) used during the recording phase.
  -1,
  -1,
  -1,
  -1,
  -1,
  -1
);
Object.freeze(I);
const B = {
  name: `This CSTNode indicates the Parser is in Recording Phase
	See: https://chevrotain.io/docs/guide/internals.html#grammar-recording for details`,
  children: {}
};
class ee {
  initGastRecorder(e) {
    this.recordingProdStack = [], this.RECORDING_PHASE = !1;
  }
  enableRecording() {
    this.RECORDING_PHASE = !0, this.TRACE_INIT("Enable Recording", () => {
      for (let e = 0; e < 10; e++) {
        const t = e > 0 ? e : "";
        this[`CONSUME${t}`] = function(r, i) {
          return this.consumeInternalRecord(r, e, i);
        }, this[`SUBRULE${t}`] = function(r, i) {
          return this.subruleInternalRecord(r, e, i);
        }, this[`OPTION${t}`] = function(r) {
          return this.optionInternalRecord(r, e);
        }, this[`OR${t}`] = function(r) {
          return this.orInternalRecord(r, e);
        }, this[`MANY${t}`] = function(r) {
          this.manyInternalRecord(e, r);
        }, this[`MANY_SEP${t}`] = function(r) {
          this.manySepFirstInternalRecord(e, r);
        }, this[`AT_LEAST_ONE${t}`] = function(r) {
          this.atLeastOneInternalRecord(e, r);
        }, this[`AT_LEAST_ONE_SEP${t}`] = function(r) {
          this.atLeastOneSepFirstInternalRecord(e, r);
        };
      }
      this.consume = function(e, t, r) {
        return this.consumeInternalRecord(t, e, r);
      }, this.subrule = function(e, t, r) {
        return this.subruleInternalRecord(t, e, r);
      }, this.option = function(e, t) {
        return this.optionInternalRecord(t, e);
      }, this.or = function(e, t) {
        return this.orInternalRecord(t, e);
      }, this.many = function(e, t) {
        this.manyInternalRecord(e, t);
      }, this.atLeastOne = function(e, t) {
        this.atLeastOneInternalRecord(e, t);
      }, this.ACTION = this.ACTION_RECORD, this.BACKTRACK = this.BACKTRACK_RECORD, this.LA = this.LA_RECORD;
    });
  }
  disableRecording() {
    this.RECORDING_PHASE = !1, this.TRACE_INIT("Deleting Recording methods", () => {
      const e = this;
      for (let t = 0; t < 10; t++) {
        const r = t > 0 ? t : "";
        delete e[`CONSUME${r}`], delete e[`SUBRULE${r}`], delete e[`OPTION${r}`], delete e[`OR${r}`], delete e[`MANY${r}`], delete e[`MANY_SEP${r}`], delete e[`AT_LEAST_ONE${r}`], delete e[`AT_LEAST_ONE_SEP${r}`];
      }
      delete e.consume, delete e.subrule, delete e.option, delete e.or, delete e.many, delete e.atLeastOne, delete e.ACTION, delete e.BACKTRACK, delete e.LA;
    });
  }
  //   Parser methods are called inside an ACTION?
  //   Maybe try/catch/finally on ACTIONS while disabling the recorders state changes?
  // @ts-expect-error -- noop place holder
  ACTION_RECORD(e) {
  }
  // Executing backtracking logic will break our recording logic assumptions
  BACKTRACK_RECORD(e, t) {
    return () => !0;
  }
  // LA is part of the official API and may be used for custom lookahead logic
  // by end users who may forget to wrap it in ACTION or inside a GATE
  LA_RECORD(e) {
    return K;
  }
  topLevelRuleRecord(e, t) {
    try {
      const r = new A({ definition: [], name: e });
      return r.name = e, this.recordingProdStack.push(r), t.call(this), this.recordingProdStack.pop(), r;
    } catch (r) {
      if (r.KNOWN_RECORDER_ERROR !== !0)
        try {
          r.message = r.message + `
	 This error was thrown during the "grammar recording phase" For more info see:
	https://chevrotain.io/docs/guide/internals.html#grammar-recording`;
        } catch {
          throw r;
        }
      throw r;
    }
  }
  // Implementation of parsing DSL
  optionInternalRecord(e, t) {
    return c.call(this, N, e, t);
  }
  atLeastOneInternalRecord(e, t) {
    c.call(this, S, t, e);
  }
  atLeastOneSepFirstInternalRecord(e, t) {
    c.call(this, _, t, e, E);
  }
  manyInternalRecord(e, t) {
    c.call(this, T, t, e);
  }
  manySepFirstInternalRecord(e, t) {
    c.call(this, g, t, e, E);
  }
  orInternalRecord(e, t) {
    return U.call(this, e, t);
  }
  subruleInternalRecord(e, t, r) {
    if (R(t), !e || d(e, "ruleName") === !1) {
      const a = new Error(`<SUBRULE${m(t)}> argument is invalid expecting a Parser method reference but got: <${JSON.stringify(e)}>
 inside top level rule: <${this.recordingProdStack[0].name}>`);
      throw a.KNOWN_RECORDER_ERROR = !0, a;
    }
    const i = l(this.recordingProdStack), s = e.ruleName, o = new C({
      idx: t,
      nonTerminalName: s,
      label: r == null ? void 0 : r.LABEL,
      // The resolving of the `referencedRule` property will be done once all the Rule's GASTs have been created
      referencedRule: void 0
    });
    return i.definition.push(o), this.outputCst ? B : u;
  }
  consumeInternalRecord(e, t, r) {
    if (R(t), !w(e)) {
      const o = new Error(`<CONSUME${m(t)}> argument is invalid expecting a TokenType reference but got: <${JSON.stringify(e)}>
 inside top level rule: <${this.recordingProdStack[0].name}>`);
      throw o.KNOWN_RECORDER_ERROR = !0, o;
    }
    const i = l(this.recordingProdStack), s = new L({
      idx: t,
      terminalType: e,
      label: r == null ? void 0 : r.LABEL
    });
    return i.definition.push(s), I;
  }
}
function c(n, e, t, r = !1) {
  R(t);
  const i = l(this.recordingProdStack), s = f(e) ? e : e.DEF, o = new n({ definition: [], idx: t });
  return r && (o.separator = e.SEP), d(e, "MAX_LOOKAHEAD") && (o.maxLookahead = e.MAX_LOOKAHEAD), this.recordingProdStack.push(o), s.call(this), i.definition.push(o), this.recordingProdStack.pop(), u;
}
function U(n, e) {
  R(e);
  const t = l(this.recordingProdStack), r = x(n) === !1, i = r === !1 ? n : n.DEF, s = new P({
    definition: [],
    idx: e,
    ignoreAmbiguities: r && n.IGNORE_AMBIGUITIES === !0
  });
  d(n, "MAX_LOOKAHEAD") && (s.maxLookahead = n.MAX_LOOKAHEAD);
  const o = y(i, (a) => f(a.GATE));
  return s.hasPredicates = o, t.definition.push(s), G(i, (a) => {
    const h = new D({ definition: [] });
    s.definition.push(h), d(a, "IGNORE_AMBIGUITIES") ? h.ignoreAmbiguities = a.IGNORE_AMBIGUITIES : d(a, "GATE") && (h.ignoreAmbiguities = !0), this.recordingProdStack.push(h), a.ALT.call(this), this.recordingProdStack.pop();
  }), u;
}
function m(n) {
  return n === 0 ? "" : `${n}`;
}
function R(n) {
  if (n < 0 || n > O) {
    const e = new Error(
      // The stack trace will contain all the needed details
      `Invalid DSL Method idx value: <${n}>
	Idx value must be a none negative value smaller than ${O + 1}`
    );
    throw e.KNOWN_RECORDER_ERROR = !0, e;
  }
}
export {
  ee as GastRecorder
};
