import { isRecognitionException as u, EarlyExitException as m, NoViableAltException as d } from "../../exceptions_public.js";
import { getLookaheadPathsForOptionalProd as E, getLookaheadPathsForOr as A } from "../../grammar/lookahead.js";
import { DEFAULT_PARSER_CONFIG as p } from "../parser.js";
import P from "../../../../../../../../lodash-es@4.17.21/node_modules/lodash-es/has.js";
import c from "../../../../../../../../lodash-es@4.17.21/node_modules/lodash-es/clone.js";
class N {
  initErrorHandler(e) {
    this._errors = [], this.errorMessageProvider = P(e, "errorMessageProvider") ? e.errorMessageProvider : p.errorMessageProvider;
  }
  SAVE_ERROR(e) {
    if (u(e))
      return e.context = {
        ruleStack: this.getHumanReadableRuleStack(),
        ruleOccurrenceStack: c(this.RULE_OCCURRENCE_STACK)
      }, this._errors.push(e), e;
    throw Error("Trying to save an Error which is not a RecognitionException");
  }
  get errors() {
    return c(this._errors);
  }
  set errors(e) {
    this._errors = e;
  }
  // TODO: consider caching the error message computed information
  raiseEarlyExitException(e, i, a) {
    const t = this.getCurrRuleFullName(), h = this.getGAstProductions()[t], o = E(e, h, i, this.maxLookahead)[0], s = [];
    for (let n = 1; n <= this.maxLookahead; n++)
      s.push(this.LA(n));
    const r = this.errorMessageProvider.buildEarlyExitMessage({
      expectedIterationPaths: o,
      actual: s,
      previous: this.LA(0),
      customUserDescription: a,
      ruleName: t
    });
    throw this.SAVE_ERROR(new m(r, this.LA(1), this.LA(0)));
  }
  // TODO: consider caching the error message computed information
  raiseNoAltException(e, i) {
    const a = this.getCurrRuleFullName(), t = this.getGAstProductions()[a], h = A(e, t, this.maxLookahead), l = [];
    for (let r = 1; r <= this.maxLookahead; r++)
      l.push(this.LA(r));
    const o = this.LA(0), s = this.errorMessageProvider.buildNoViableAltMessage({
      expectedPathsPerAlt: h,
      actual: l,
      previous: o,
      customUserDescription: i,
      ruleName: this.getCurrRuleFullName()
    });
    throw this.SAVE_ERROR(new d(s, this.LA(1), o));
  }
}
export {
  N as ErrorHandler
};
