import "../../../../../../@chevrotain_regexp-to-ast@11.0.3/node_modules/@chevrotain/regexp-to-ast/lib/src/character-classes.js";
import { BaseRegExpVisitor as S } from "../../../../../../@chevrotain_regexp-to-ast@11.0.3/node_modules/@chevrotain/regexp-to-ast/lib/src/base-regexp-visitor.js";
import { PRINT_WARNING as w, PRINT_ERROR as O } from "../../../../../../@chevrotain_utils@11.0.3/node_modules/@chevrotain/utils/lib/src/print.js";
import { getRegExpAst as b } from "./reg_exp_parser.js";
import { minOptimizationVal as u, charCodeToOptimizedIndex as c } from "./lexer.js";
import m from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/find.js";
import C from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/includes.js";
import N from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/forEach.js";
import R from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/values.js";
import A from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/every.js";
import L from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/isArray.js";
const y = "Complement Sets are not supported for first char optimization", g = `Unable to use "first char" lexer optimizations:
`;
function D(r, e = !1) {
  try {
    const t = b(r);
    return p(t.value, {}, t.flags.ignoreCase);
  } catch (t) {
    if (t.message === y)
      e && w(`${g}	Unable to optimize: < ${r.toString()} >
	Complement Sets cannot be automatically optimized.
	This will disable the lexer's first char optimizations.
	See: https://chevrotain.io/docs/guide/resolving_lexer_errors.html#COMPLEMENT for details.`);
    else {
      let i = "";
      e && (i = `
	This will disable the lexer's first char optimizations.
	See: https://chevrotain.io/docs/guide/resolving_lexer_errors.html#REGEXP_PARSING for details.`), O(`${g}
	Failed parsing: < ${r.toString()} >
	Using the @chevrotain/regexp-to-ast library
	Please open an issue at: https://github.com/chevrotain/chevrotain/issues` + i);
    }
  }
  return [];
}
function p(r, e, t) {
  switch (r.type) {
    case "Disjunction":
      for (let o = 0; o < r.value.length; o++)
        p(r.value[o], e, t);
      break;
    case "Alternative":
      const i = r.value;
      for (let o = 0; o < i.length; o++) {
        const f = i[o];
        switch (f.type) {
          case "EndAnchor":
          // A group back reference cannot affect potential starting char.
          // because if a back reference is the first production than automatically
          // the group being referenced has had to come BEFORE so its codes have already been added
          case "GroupBackReference":
          // assertions do not affect potential starting codes
          case "Lookahead":
          case "NegativeLookahead":
          case "StartAnchor":
          case "WordBoundary":
          case "NonWordBoundary":
            continue;
        }
        const a = f;
        switch (a.type) {
          case "Character":
            d(a.value, e, t);
            break;
          case "Set":
            if (a.complement === !0)
              throw Error(y);
            N(a.value, (l) => {
              if (typeof l == "number")
                d(l, e, t);
              else {
                const s = l;
                if (t === !0)
                  for (let n = s.from; n <= s.to; n++)
                    d(n, e, t);
                else {
                  for (let n = s.from; n <= s.to && n < u; n++)
                    d(n, e, t);
                  if (s.to >= u) {
                    const n = s.from >= u ? s.from : u, z = s.to, k = c(n), E = c(z);
                    for (let h = k; h <= E; h++)
                      e[h] = h;
                  }
                }
              }
            });
            break;
          case "Group":
            p(a.value, e, t);
            break;
          /* istanbul ignore next */
          default:
            throw Error("Non Exhaustive Match");
        }
        const I = a.quantifier !== void 0 && a.quantifier.atLeast === 0;
        if (
          // A group may be optional due to empty contents /(?:)/
          // or if everything inside it is optional /((a)?)/
          a.type === "Group" && v(a) === !1 || // If this term is not a group it may only be optional if it has an optional quantifier
          a.type !== "Group" && I === !1
        )
          break;
      }
      break;
    /* istanbul ignore next */
    default:
      throw Error("non exhaustive match!");
  }
  return R(e);
}
function d(r, e, t) {
  const i = c(r);
  e[i] = i, t === !0 && G(r, e);
}
function G(r, e) {
  const t = String.fromCharCode(r), i = t.toUpperCase();
  if (i !== t) {
    const o = c(i.charCodeAt(0));
    e[o] = o;
  } else {
    const o = t.toLowerCase();
    if (o !== t) {
      const f = c(o.charCodeAt(0));
      e[f] = f;
    }
  }
}
function x(r, e) {
  return m(r.value, (t) => {
    if (typeof t == "number")
      return C(e, t);
    {
      const i = t;
      return m(e, (o) => i.from <= o && o <= i.to) !== void 0;
    }
  });
}
function v(r) {
  const e = r.quantifier;
  return e && e.atLeast === 0 ? !0 : r.value ? L(r.value) ? A(r.value, v) : v(r.value) : !1;
}
class P extends S {
  constructor(e) {
    super(), this.targetCharCodes = e, this.found = !1;
  }
  visitChildren(e) {
    if (this.found !== !0) {
      switch (e.type) {
        case "Lookahead":
          this.visitLookahead(e);
          return;
        case "NegativeLookahead":
          this.visitNegativeLookahead(e);
          return;
      }
      super.visitChildren(e);
    }
  }
  visitCharacter(e) {
    C(this.targetCharCodes, e.value) && (this.found = !0);
  }
  visitSet(e) {
    e.complement ? x(e, this.targetCharCodes) === void 0 && (this.found = !0) : x(e, this.targetCharCodes) !== void 0 && (this.found = !0);
  }
}
function Q(r, e) {
  if (e instanceof RegExp) {
    const t = b(e), i = new P(r);
    return i.visit(t), i.found;
  } else
    return m(e, (t) => C(r, t.charCodeAt(0))) !== void 0;
}
export {
  Q as canMatchCharCode,
  g as failedOptimizationPrefixMsg,
  p as firstCharOptimizedIndices,
  D as getOptimizedStartCodesIndices
};
