import "../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/parse/parser/parser.js";
import "../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/scan/lexer_public.js";
import { tokenMatcher as C, tokenLabel as F } from "../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/scan/tokens_public.js";
import { getLookaheadPaths as v } from "../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/parse/grammar/lookahead.js";
import { LLkLookaheadStrategy as D } from "../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/parse/grammar/llk_lookahead.js";
import "../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/parse/errors_public.js";
import "../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/parse/exceptions_public.js";
import { NonTerminal as U, Option as q, Alternation as j, RepetitionMandatory as z, RepetitionMandatoryWithSeparator as $, RepetitionWithSeparator as K, Repetition as B, Terminal as G } from "../../../../@chevrotain_gast@11.0.3/node_modules/@chevrotain/gast/lib/src/model.js";
import { createATN as W, buildATNKey as b, ATN_RULE_STOP as k, AtomTransition as w, EpsilonTransition as Y, RuleTransition as V } from "./atn.js";
import { ATNConfigSet as E, DFA_ERROR as y, getATNConfigKey as H } from "./dfa.js";
import J from "../../../../lodash-es@4.17.22/node_modules/lodash-es/min.js";
import Q from "../../../../lodash-es@4.17.22/node_modules/lodash-es/flatMap.js";
import X from "../../../../lodash-es@4.17.22/node_modules/lodash-es/uniqBy.js";
import T from "../../../../lodash-es@4.17.22/node_modules/lodash-es/map.js";
import Z from "../../../../lodash-es@4.17.22/node_modules/lodash-es/flatten.js";
import L from "../../../../lodash-es@4.17.22/node_modules/lodash-es/forEach.js";
import tt from "../../../../lodash-es@4.17.22/node_modules/lodash-es/isEmpty.js";
import I from "../../../../lodash-es@4.17.22/node_modules/lodash-es/reduce.js";
function et(e, t) {
  const n = {};
  return (i) => {
    const o = i.toString();
    let r = n[o];
    return r !== void 0 || (r = {
      atnStartState: e,
      decision: t,
      states: {}
    }, n[o] = r), r;
  };
}
class P {
  constructor() {
    this.predicates = [];
  }
  is(t) {
    return t >= this.predicates.length || this.predicates[t];
  }
  set(t, n) {
    this.predicates[t] = n;
  }
  toString() {
    let t = "";
    const n = this.predicates.length;
    for (let i = 0; i < n; i++)
      t += this.predicates[i] === !0 ? "1" : "0";
    return t;
  }
}
const R = new P();
class qt extends D {
  constructor(t) {
    var n;
    super(), this.logging = (n = t == null ? void 0 : t.logging) !== null && n !== void 0 ? n : ((i) => console.log(i));
  }
  initialize(t) {
    this.atn = W(t.rules), this.dfas = nt(this.atn);
  }
  validateAmbiguousAlternationAlternatives() {
    return [];
  }
  validateEmptyOrAlternatives() {
    return [];
  }
  buildLookaheadForAlternation(t) {
    const { prodOccurrence: n, rule: i, hasPredicates: o, dynamicTokensEnabled: r } = t, s = this.dfas, a = this.logging, c = b(i, "Alternation", n), m = this.atn.decisionMap[c].decision, S = T(v({
      maxLookahead: 1,
      occurrence: n,
      prodType: "Alternation",
      rule: i
    }), (u) => T(u, (l) => l[0]));
    if (O(S, !1) && !r) {
      const u = I(S, (l, h, d) => (L(h, (f) => {
        f && (l[f.tokenTypeIdx] = d, L(f.categoryMatches, (g) => {
          l[g] = d;
        }));
      }), l), {});
      return o ? function(l) {
        var h;
        const d = this.LA(1), f = u[d.tokenTypeIdx];
        if (l !== void 0 && f !== void 0) {
          const g = (h = l[f]) === null || h === void 0 ? void 0 : h.GATE;
          if (g !== void 0 && g.call(this) === !1)
            return;
        }
        return f;
      } : function() {
        const l = this.LA(1);
        return u[l.tokenTypeIdx];
      };
    } else return o ? function(u) {
      const l = new P(), h = u === void 0 ? 0 : u.length;
      for (let f = 0; f < h; f++) {
        const g = u == null ? void 0 : u[f].GATE;
        l.set(f, g === void 0 || g.call(this));
      }
      const d = x.call(this, s, m, l, a);
      return typeof d == "number" ? d : void 0;
    } : function() {
      const u = x.call(this, s, m, R, a);
      return typeof u == "number" ? u : void 0;
    };
  }
  buildLookaheadForOptional(t) {
    const { prodOccurrence: n, rule: i, prodType: o, dynamicTokensEnabled: r } = t, s = this.dfas, a = this.logging, c = b(i, o, n), m = this.atn.decisionMap[c].decision, S = T(v({
      maxLookahead: 1,
      occurrence: n,
      prodType: o,
      rule: i
    }), (u) => T(u, (l) => l[0]));
    if (O(S) && S[0][0] && !r) {
      const u = S[0], l = Z(u);
      if (l.length === 1 && tt(l[0].categoryMatches)) {
        const d = l[0].tokenTypeIdx;
        return function() {
          return this.LA(1).tokenTypeIdx === d;
        };
      } else {
        const h = I(l, (d, f) => (f !== void 0 && (d[f.tokenTypeIdx] = !0, L(f.categoryMatches, (g) => {
          d[g] = !0;
        })), d), {});
        return function() {
          const d = this.LA(1);
          return h[d.tokenTypeIdx] === !0;
        };
      }
    }
    return function() {
      const u = x.call(this, s, m, R, a);
      return typeof u == "object" ? !1 : u === 0;
    };
  }
}
function O(e, t = !0) {
  const n = /* @__PURE__ */ new Set();
  for (const i of e) {
    const o = /* @__PURE__ */ new Set();
    for (const r of i) {
      if (r === void 0) {
        if (t)
          break;
        return !1;
      }
      const s = [r.tokenTypeIdx].concat(r.categoryMatches);
      for (const a of s)
        if (n.has(a)) {
          if (!o.has(a))
            return !1;
        } else
          n.add(a), o.add(a);
    }
  }
  return !0;
}
function nt(e) {
  const t = e.decisionStates.length, n = Array(t);
  for (let i = 0; i < t; i++)
    n[i] = et(e.decisionStates[i], i);
  return n;
}
function x(e, t, n, i) {
  const o = e[t](n);
  let r = o.start;
  if (r === void 0) {
    const a = pt(o.atnStartState);
    r = _(o, N(a)), o.start = r;
  }
  return it.apply(this, [o, r, n, i]);
}
function it(e, t, n, i) {
  let o = t, r = 1;
  const s = [];
  let a = this.LA(r++);
  for (; ; ) {
    let c = ut(o, a);
    if (c === void 0 && (c = ot.apply(this, [e, o, a, r, n, i])), c === y)
      return ct(s, o, a);
    if (c.isAcceptState === !0)
      return c.prediction;
    o = c, s.push(a), a = this.LA(r++);
  }
}
function ot(e, t, n, i, o, r) {
  const s = lt(t.configs, n, o);
  if (s.size === 0)
    return M(e, t, n, y), y;
  let a = N(s);
  const c = dt(s, o);
  if (c !== void 0)
    a.isAcceptState = !0, a.prediction = c, a.configs.uniqueAlt = c;
  else if (St(s)) {
    const p = J(s.alts);
    a.isAcceptState = !0, a.prediction = p, a.configs.uniqueAlt = p, rt.apply(this, [e, i, s.alts, r]);
  }
  return a = M(e, t, n, a), a;
}
function rt(e, t, n, i) {
  const o = [];
  for (let p = 1; p <= t; p++)
    o.push(this.LA(p).tokenType);
  const r = e.atnStartState, s = r.rule, a = r.production, c = st({
    topLevelRule: s,
    ambiguityIndices: n,
    production: a,
    prefixPath: o
  });
  i(c);
}
function st(e) {
  const t = T(e.prefixPath, (o) => F(o)).join(", "), n = e.production.idx === 0 ? "" : e.production.idx;
  let i = `Ambiguous Alternatives Detected: <${e.ambiguityIndices.join(", ")}> in <${at(e.production)}${n}> inside <${e.topLevelRule.name}> Rule,
<${t}> may appears as a prefix path in all these alternatives.
`;
  return i = i + `See: https://chevrotain.io/docs/guide/resolving_grammar_errors.html#AMBIGUOUS_ALTERNATIVES
For Further details.`, i;
}
function at(e) {
  if (e instanceof U)
    return "SUBRULE";
  if (e instanceof q)
    return "OPTION";
  if (e instanceof j)
    return "OR";
  if (e instanceof z)
    return "AT_LEAST_ONE";
  if (e instanceof $)
    return "AT_LEAST_ONE_SEP";
  if (e instanceof K)
    return "MANY_SEP";
  if (e instanceof B)
    return "MANY";
  if (e instanceof G)
    return "CONSUME";
  throw Error("non exhaustive match");
}
function ct(e, t, n) {
  const i = Q(t.configs.elements, (r) => r.state.transitions), o = X(i.filter((r) => r instanceof w).map((r) => r.tokenType), (r) => r.tokenTypeIdx);
  return {
    actualToken: n,
    possibleTokenTypes: o,
    tokenPath: e
  };
}
function ut(e, t) {
  return e.edges[t.tokenTypeIdx];
}
function lt(e, t, n) {
  const i = new E(), o = [];
  for (const s of e.elements) {
    if (n.is(s.alt) === !1)
      continue;
    if (s.state.type === k) {
      o.push(s);
      continue;
    }
    const a = s.state.transitions.length;
    for (let c = 0; c < a; c++) {
      const p = s.state.transitions[c], m = ft(p, t);
      m !== void 0 && i.add({
        state: m,
        alt: s.alt,
        stack: s.stack
      });
    }
  }
  let r;
  if (o.length === 0 && i.size === 1 && (r = i), r === void 0) {
    r = new E();
    for (const s of i.elements)
      A(s, r);
  }
  if (o.length > 0 && !gt(r))
    for (const s of o)
      r.add(s);
  return r;
}
function ft(e, t) {
  if (e instanceof w && C(t, e.tokenType))
    return e.target;
}
function dt(e, t) {
  let n;
  for (const i of e.elements)
    if (t.is(i.alt) === !0) {
      if (n === void 0)
        n = i.alt;
      else if (n !== i.alt)
        return;
    }
  return n;
}
function N(e) {
  return {
    configs: e,
    edges: {},
    isAcceptState: !1,
    prediction: -1
  };
}
function M(e, t, n, i) {
  return i = _(e, i), t.edges[n.tokenTypeIdx] = i, i;
}
function _(e, t) {
  if (t === y)
    return t;
  const n = t.configs.key, i = e.states[n];
  return i !== void 0 ? i : (t.configs.finalize(), e.states[n] = t, t);
}
function pt(e) {
  const t = new E(), n = e.transitions.length;
  for (let i = 0; i < n; i++) {
    const r = {
      state: e.transitions[i].target,
      alt: i,
      stack: []
    };
    A(r, t);
  }
  return t;
}
function A(e, t) {
  const n = e.state;
  if (n.type === k) {
    if (e.stack.length > 0) {
      const o = [...e.stack], s = {
        state: o.pop(),
        alt: e.alt,
        stack: o
      };
      A(s, t);
    } else
      t.add(e);
    return;
  }
  n.epsilonOnlyTransitions || t.add(e);
  const i = n.transitions.length;
  for (let o = 0; o < i; o++) {
    const r = n.transitions[o], s = ht(e, r);
    s !== void 0 && A(s, t);
  }
}
function ht(e, t) {
  if (t instanceof Y)
    return {
      state: t.target,
      alt: e.alt,
      stack: e.stack
    };
  if (t instanceof V) {
    const n = [...e.stack, t.followState];
    return {
      state: t.target,
      alt: e.alt,
      stack: n
    };
  }
}
function gt(e) {
  for (const t of e.elements)
    if (t.state.type === k)
      return !0;
  return !1;
}
function mt(e) {
  for (const t of e.elements)
    if (t.state.type !== k)
      return !1;
  return !0;
}
function St(e) {
  if (mt(e))
    return !0;
  const t = Tt(e.elements);
  return yt(t) && !At(t);
}
function Tt(e) {
  const t = /* @__PURE__ */ new Map();
  for (const n of e) {
    const i = H(n, !1);
    let o = t.get(i);
    o === void 0 && (o = {}, t.set(i, o)), o[n.alt] = !0;
  }
  return t;
}
function yt(e) {
  for (const t of Array.from(e.values()))
    if (Object.keys(t).length > 1)
      return !0;
  return !1;
}
function At(e) {
  for (const t of Array.from(e.values()))
    if (Object.keys(t).length === 1)
      return !0;
  return !1;
}
export {
  qt as LLStarLookaheadStrategy
};
