import { DEFAULT_PARSER_CONFIG as c } from "../parser.js";
import { getKeyForAutomaticLookahead as r, OR_IDX as S, MANY_IDX as F, OPTION_IDX as T, AT_LEAST_ONE_IDX as A, AT_LEAST_ONE_SEP_IDX as E, MANY_SEP_IDX as _ } from "../../grammar/keys.js";
import { GAstVisitor as u } from "../../../../../../../../@chevrotain_gast@11.0.3/node_modules/@chevrotain/gast/lib/src/visitor.js";
import { getProductionDslName as i } from "../../../../../../../../@chevrotain_gast@11.0.3/node_modules/@chevrotain/gast/lib/src/helpers.js";
import { LLkLookaheadStrategy as I } from "../../grammar/llk_lookahead.js";
import L from "../../../../../../../../lodash-es@4.17.21/node_modules/lodash-es/has.js";
import e from "../../../../../../../../lodash-es@4.17.21/node_modules/lodash-es/forEach.js";
class v {
  initLooksAhead(t) {
    this.dynamicTokensEnabled = L(t, "dynamicTokensEnabled") ? t.dynamicTokensEnabled : c.dynamicTokensEnabled, this.maxLookahead = L(t, "maxLookahead") ? t.maxLookahead : c.maxLookahead, this.lookaheadStrategy = L(t, "lookaheadStrategy") ? t.lookaheadStrategy : new I({ maxLookahead: this.maxLookahead }), this.lookAheadFuncsCache = /* @__PURE__ */ new Map();
  }
  preComputeLookaheadFunctions(t) {
    e(t, (o) => {
      this.TRACE_INIT(`${o.name} Rule Lookahead`, () => {
        const { alternation: h, repetition: d, option: p, repetitionMandatory: m, repetitionMandatoryWithSeparator: k, repetitionWithSeparator: l } = C(o);
        e(h, (a) => {
          const y = a.idx === 0 ? "" : a.idx;
          this.TRACE_INIT(`${i(a)}${y}`, () => {
            const x = this.lookaheadStrategy.buildLookaheadForAlternation({
              prodOccurrence: a.idx,
              rule: o,
              maxLookahead: a.maxLookahead || this.maxLookahead,
              hasPredicates: a.hasPredicates,
              dynamicTokensEnabled: this.dynamicTokensEnabled
            }), M = r(this.fullRuleNameToShort[o.name], S, a.idx);
            this.setLaFuncCache(M, x);
          });
        }), e(d, (a) => {
          this.computeLookaheadFunc(o, a.idx, F, "Repetition", a.maxLookahead, i(a));
        }), e(p, (a) => {
          this.computeLookaheadFunc(o, a.idx, T, "Option", a.maxLookahead, i(a));
        }), e(m, (a) => {
          this.computeLookaheadFunc(o, a.idx, A, "RepetitionMandatory", a.maxLookahead, i(a));
        }), e(k, (a) => {
          this.computeLookaheadFunc(o, a.idx, E, "RepetitionMandatoryWithSeparator", a.maxLookahead, i(a));
        }), e(l, (a) => {
          this.computeLookaheadFunc(o, a.idx, _, "RepetitionWithSeparator", a.maxLookahead, i(a));
        });
      });
    });
  }
  computeLookaheadFunc(t, o, h, d, p, m) {
    this.TRACE_INIT(`${m}${o === 0 ? "" : o}`, () => {
      const k = this.lookaheadStrategy.buildLookaheadForOptional({
        prodOccurrence: o,
        rule: t,
        maxLookahead: p || this.maxLookahead,
        dynamicTokensEnabled: this.dynamicTokensEnabled,
        prodType: d
      }), l = r(this.fullRuleNameToShort[t.name], h, o);
      this.setLaFuncCache(l, k);
    });
  }
  // this actually returns a number, but it is always used as a string (object prop key)
  getKeyForAutomaticLookahead(t, o) {
    const h = this.getLastExplicitRuleShortName();
    return r(h, t, o);
  }
  getLaFuncFromCache(t) {
    return this.lookAheadFuncsCache.get(t);
  }
  /* istanbul ignore next */
  setLaFuncCache(t, o) {
    this.lookAheadFuncsCache.set(t, o);
  }
}
class N extends u {
  constructor() {
    super(...arguments), this.dslMethods = {
      option: [],
      alternation: [],
      repetition: [],
      repetitionWithSeparator: [],
      repetitionMandatory: [],
      repetitionMandatoryWithSeparator: []
    };
  }
  reset() {
    this.dslMethods = {
      option: [],
      alternation: [],
      repetition: [],
      repetitionWithSeparator: [],
      repetitionMandatory: [],
      repetitionMandatoryWithSeparator: []
    };
  }
  visitOption(t) {
    this.dslMethods.option.push(t);
  }
  visitRepetitionWithSeparator(t) {
    this.dslMethods.repetitionWithSeparator.push(t);
  }
  visitRepetitionMandatory(t) {
    this.dslMethods.repetitionMandatory.push(t);
  }
  visitRepetitionMandatoryWithSeparator(t) {
    this.dslMethods.repetitionMandatoryWithSeparator.push(t);
  }
  visitRepetition(t) {
    this.dslMethods.repetition.push(t);
  }
  visitAlternation(t) {
    this.dslMethods.alternation.push(t);
  }
}
const s = new N();
function C(n) {
  s.reset(), n.accept(s);
  const t = s.dslMethods;
  return s.reset(), t;
}
export {
  v as LooksAhead,
  C as collectMethods
};
