import { hasTokenLabel as x, tokenLabel as g } from "../scan/tokens_public.js";
import { Rule as b, Terminal as $, NonTerminal as T } from "../../../../../../@chevrotain_gast@11.0.3/node_modules/@chevrotain/gast/lib/src/model.js";
import { getProductionDslName as p } from "../../../../../../@chevrotain_gast@11.0.3/node_modules/@chevrotain/gast/lib/src/helpers.js";
import f from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/head.js";
import l from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/map.js";
import R from "../../../../../../lodash-es@4.17.21/node_modules/lodash-es/reduce.js";
const E = {
  buildMismatchTokenMessage({ expected: e, actual: r, previous: t, ruleName: n }) {
    return `Expecting ${x(e) ? `--> ${g(e)} <--` : `token of type --> ${e.name} <--`} but found --> '${r.image}' <--`;
  },
  buildNotAllInputParsedMessage({ firstRedundant: e, ruleName: r }) {
    return "Redundant input, expecting EOF but found: " + e.image;
  },
  buildNoViableAltMessage({ expectedPathsPerAlt: e, actual: r, previous: t, customUserDescription: n, ruleName: a }) {
    const i = "Expecting: ", c = `
but found: '` + f(r).image + "'";
    if (n)
      return i + n + c;
    {
      const m = R(e, (d, h) => d.concat(h), []), o = l(m, (d) => `[${l(d, (h) => g(h)).join(", ")}]`), v = `one of these possible Token sequences:
${l(o, (d, h) => `  ${h + 1}. ${d}`).join(`
`)}`;
      return i + v + c;
    }
  },
  buildEarlyExitMessage({ expectedIterationPaths: e, actual: r, customUserDescription: t, ruleName: n }) {
    const a = "Expecting: ", u = `
but found: '` + f(r).image + "'";
    if (t)
      return a + t + u;
    {
      const m = `expecting at least one iteration which starts with one of these possible Token sequences::
  <${l(e, (o) => `[${l(o, (s) => g(s)).join(",")}]`).join(" ,")}>`;
      return a + m + u;
    }
  }
};
Object.freeze(E);
const P = {
  buildRuleNotFoundError(e, r) {
    return "Invalid grammar, reference to a rule which is not defined: ->" + r.nonTerminalName + `<-
inside top level rule: ->` + e.name + "<-";
  }
}, I = {
  buildDuplicateFoundError(e, r) {
    function t(s) {
      return s instanceof $ ? s.terminalType.name : s instanceof T ? s.nonTerminalName : "";
    }
    const n = e.name, a = f(r), i = a.idx, u = p(a), c = t(a), m = i > 0;
    let o = `->${u}${m ? i : ""}<- ${c ? `with argument: ->${c}<-` : ""}
                  appears more than once (${r.length} times) in the top level rule: ->${n}<-.                  
                  For further details see: https://chevrotain.io/docs/FAQ.html#NUMERICAL_SUFFIXES 
                  `;
    return o = o.replace(/[ \t]+/g, " "), o = o.replace(/\s\s+/g, `
`), o;
  },
  buildNamespaceConflictError(e) {
    return `Namespace conflict found in grammar.
The grammar has both a Terminal(Token) and a Non-Terminal(Rule) named: <${e.name}>.
To resolve this make sure each Terminal and Non-Terminal names are unique
This is easy to accomplish by using the convention that Terminal names start with an uppercase letter
and Non-Terminal names start with a lower case letter.`;
  },
  buildAlternationPrefixAmbiguityError(e) {
    const r = l(e.prefixPath, (a) => g(a)).join(", "), t = e.alternation.idx === 0 ? "" : e.alternation.idx;
    return `Ambiguous alternatives: <${e.ambiguityIndices.join(" ,")}> due to common lookahead prefix
in <OR${t}> inside <${e.topLevelRule.name}> Rule,
<${r}> may appears as a prefix path in all these alternatives.
See: https://chevrotain.io/docs/guide/resolving_grammar_errors.html#COMMON_PREFIX
For Further details.`;
  },
  buildAlternationAmbiguityError(e) {
    const r = l(e.prefixPath, (a) => g(a)).join(", "), t = e.alternation.idx === 0 ? "" : e.alternation.idx;
    let n = `Ambiguous Alternatives Detected: <${e.ambiguityIndices.join(" ,")}> in <OR${t}> inside <${e.topLevelRule.name}> Rule,
<${r}> may appears as a prefix path in all these alternatives.
`;
    return n = n + `See: https://chevrotain.io/docs/guide/resolving_grammar_errors.html#AMBIGUOUS_ALTERNATIVES
For Further details.`, n;
  },
  buildEmptyRepetitionError(e) {
    let r = p(e.repetition);
    return e.repetition.idx !== 0 && (r += e.repetition.idx), `The repetition <${r}> within Rule <${e.topLevelRule.name}> can never consume any tokens.
This could lead to an infinite loop.`;
  },
  // TODO: remove - `errors_public` from nyc.config.js exclude
  //       once this method is fully removed from this file
  buildTokenNameError(e) {
    return "deprecated";
  },
  buildEmptyAlternationError(e) {
    return `Ambiguous empty alternative: <${e.emptyChoiceIdx + 1}> in <OR${e.alternation.idx}> inside <${e.topLevelRule.name}> Rule.
Only the last alternative may be an empty alternative.`;
  },
  buildTooManyAlternativesError(e) {
    return `An Alternation cannot have more than 256 alternatives:
<OR${e.alternation.idx}> inside <${e.topLevelRule.name}> Rule.
 has ${e.alternation.definition.length + 1} alternatives.`;
  },
  buildLeftRecursionError(e) {
    const r = e.topLevelRule.name, t = l(e.leftRecursionPath, (i) => i.name), n = `${r} --> ${t.concat([r]).join(" --> ")}`;
    return `Left Recursion found in grammar.
rule: <${r}> can be invoked from itself (directly or indirectly)
without consuming any Tokens. The grammar path that causes this is: 
 ${n}
 To fix this refactor your grammar to remove the left recursion.
see: https://en.wikipedia.org/wiki/LL_parser#Left_factoring.`;
  },
  // TODO: remove - `errors_public` from nyc.config.js exclude
  //       once this method is fully removed from this file
  buildInvalidRuleNameError(e) {
    return "deprecated";
  },
  buildDuplicateRuleNameError(e) {
    let r;
    return e.topLevelRule instanceof b ? r = e.topLevelRule.name : r = e.topLevelRule, `Duplicate definition, rule: ->${r}<- is already defined in the grammar: ->${e.grammarName}<-`;
  }
};
export {
  P as defaultGrammarResolverErrorProvider,
  I as defaultGrammarValidatorErrorProvider,
  E as defaultParserErrorProvider
};
