import clone_default from "../../../../../../lodash-es/clone.js";
import has_default from "../../../../../../lodash-es/has.js";
import { getLookaheadPathsForOptionalProd, getLookaheadPathsForOr } from "../../grammar/lookahead.js";
import { EarlyExitException, NoViableAltException, isRecognitionException } from "../../exceptions_public.js";
import { DEFAULT_PARSER_CONFIG } from "../parser.js";
var ErrorHandler = class {
	initErrorHandler(e) {
		this._errors = [], this.errorMessageProvider = has_default(e, "errorMessageProvider") ? e.errorMessageProvider : DEFAULT_PARSER_CONFIG.errorMessageProvider;
	}
	SAVE_ERROR(t) {
		if (isRecognitionException(t)) return t.context = {
			ruleStack: this.getHumanReadableRuleStack(),
			ruleOccurrenceStack: clone_default(this.RULE_OCCURRENCE_STACK)
		}, this._errors.push(t), t;
		throw Error("Trying to save an Error which is not a RecognitionException");
	}
	get errors() {
		return clone_default(this._errors);
	}
	set errors(e) {
		this._errors = e;
	}
	raiseEarlyExitException(e, t, r) {
		let a = this.getCurrRuleFullName(), o = this.getGAstProductions()[a], s = getLookaheadPathsForOptionalProd(e, o, t, this.maxLookahead)[0], c = [];
		for (let e = 1; e <= this.maxLookahead; e++) c.push(this.LA(e));
		let l = this.errorMessageProvider.buildEarlyExitMessage({
			expectedIterationPaths: s,
			actual: c,
			previous: this.LA(0),
			customUserDescription: r,
			ruleName: a
		});
		throw this.SAVE_ERROR(new EarlyExitException(l, this.LA(1), this.LA(0)));
	}
	raiseNoAltException(e, t) {
		let n = this.getCurrRuleFullName(), i = this.getGAstProductions()[n], o = getLookaheadPathsForOr(e, i, this.maxLookahead), s = [];
		for (let e = 1; e <= this.maxLookahead; e++) s.push(this.LA(e));
		let c = this.LA(0), l = this.errorMessageProvider.buildNoViableAltMessage({
			expectedPathsPerAlt: o,
			actual: s,
			previous: c,
			customUserDescription: t,
			ruleName: this.getCurrRuleFullName()
		});
		throw this.SAVE_ERROR(new NoViableAltException(l, this.LA(1), c));
	}
};
export { ErrorHandler };
