import isEmpty_default from "../../../../../lodash-es/isEmpty.js";
import clone_default from "../../../../../lodash-es/clone.js";
import forEach_default from "../../../../../lodash-es/forEach.js";
import map_default from "../../../../../lodash-es/map.js";
import has_default from "../../../../../lodash-es/has.js";
import values_default from "../../../../../lodash-es/values.js";
import { toFastProperties } from "../../../../../@chevrotain/utils/lib/src/to-fast-properties.js";
import { computeAllProdsFollows } from "../grammar/follow.js";
import { EOF, createTokenInstance } from "../../scan/tokens_public.js";
import { defaultGrammarValidatorErrorProvider, defaultParserErrorProvider } from "../errors_public.js";
import { validateLookahead } from "../grammar/checks.js";
import { resolveGrammar, validateGrammar } from "../grammar/gast/gast_resolver_public.js";
import { Recoverable } from "./traits/recoverable.js";
import { LooksAhead } from "./traits/looksahead.js";
import { TreeBuilder } from "./traits/tree_builder.js";
import { LexerAdapter } from "./traits/lexer_adapter.js";
import { RecognizerApi } from "./traits/recognizer_api.js";
import { RecognizerEngine } from "./traits/recognizer_engine.js";
import { ErrorHandler } from "./traits/error_handler.js";
import { ContentAssist } from "./traits/context_assist.js";
import { GastRecorder } from "./traits/gast_recorder.js";
import { PerformanceTracer } from "./traits/perf_tracer.js";
import { applyMixins } from "./utils/apply_mixins.js";
const END_OF_FILE = createTokenInstance(EOF, "", NaN, NaN, NaN, NaN, NaN, NaN);
Object.freeze(END_OF_FILE);
const DEFAULT_PARSER_CONFIG = Object.freeze({
	recoveryEnabled: !1,
	maxLookahead: 3,
	dynamicTokensEnabled: !1,
	outputCst: !0,
	errorMessageProvider: defaultParserErrorProvider,
	nodeLocationTracking: "none",
	traceInitPerf: !1,
	skipValidations: !1
}), DEFAULT_RULE_CONFIG = Object.freeze({
	recoveryValueFunc: () => void 0,
	resyncEnabled: !0
});
var ParserDefinitionErrorType;
(function(u) {
	u[u.INVALID_RULE_NAME = 0] = "INVALID_RULE_NAME", u[u.DUPLICATE_RULE_NAME = 1] = "DUPLICATE_RULE_NAME", u[u.INVALID_RULE_OVERRIDE = 2] = "INVALID_RULE_OVERRIDE", u[u.DUPLICATE_PRODUCTIONS = 3] = "DUPLICATE_PRODUCTIONS", u[u.UNRESOLVED_SUBRULE_REF = 4] = "UNRESOLVED_SUBRULE_REF", u[u.LEFT_RECURSION = 5] = "LEFT_RECURSION", u[u.NONE_LAST_EMPTY_ALT = 6] = "NONE_LAST_EMPTY_ALT", u[u.AMBIGUOUS_ALTS = 7] = "AMBIGUOUS_ALTS", u[u.CONFLICT_TOKENS_RULES_NAMESPACE = 8] = "CONFLICT_TOKENS_RULES_NAMESPACE", u[u.INVALID_TOKEN_NAME = 9] = "INVALID_TOKEN_NAME", u[u.NO_NON_EMPTY_LOOKAHEAD = 10] = "NO_NON_EMPTY_LOOKAHEAD", u[u.AMBIGUOUS_PREFIX_ALTS = 11] = "AMBIGUOUS_PREFIX_ALTS", u[u.TOO_MANY_ALTS = 12] = "TOO_MANY_ALTS", u[u.CUSTOM_LOOKAHEAD_VALIDATION = 13] = "CUSTOM_LOOKAHEAD_VALIDATION";
})(ParserDefinitionErrorType ||= {});
function EMPTY_ALT(u = void 0) {
	return function() {
		return u;
	};
}
var Parser = class D {
	static performSelfAnalysis(u) {
		throw Error("The **static** `performSelfAnalysis` method has been deprecated.	\nUse the **instance** method with the same name instead.");
	}
	performSelfAnalysis() {
		this.TRACE_INIT("performSelfAnalysis", () => {
			let A;
			this.selfAnalysisDone = !0;
			let j = this.className;
			this.TRACE_INIT("toFastProps", () => {
				toFastProperties(this);
			}), this.TRACE_INIT("Grammar Recording", () => {
				try {
					this.enableRecording(), forEach_default(this.definedRulesNames, (u) => {
						let D = this[u].originalGrammarAction, O;
						this.TRACE_INIT(`${u} Rule`, () => {
							O = this.topLevelRuleRecord(u, D);
						}), this.gastProductionsCache[u] = O;
					});
				} finally {
					this.disableRecording();
				}
			});
			let M = [];
			if (this.TRACE_INIT("Grammar Resolving", () => {
				M = resolveGrammar({ rules: values_default(this.gastProductionsCache) }), this.definitionErrors = this.definitionErrors.concat(M);
			}), this.TRACE_INIT("Grammar Validations", () => {
				if (isEmpty_default(M) && this.skipValidations === !1) {
					let u = validateGrammar({
						rules: values_default(this.gastProductionsCache),
						tokenTypes: values_default(this.tokensMap),
						errMsgProvider: defaultGrammarValidatorErrorProvider,
						grammarName: j
					}), D = validateLookahead({
						lookaheadStrategy: this.lookaheadStrategy,
						rules: values_default(this.gastProductionsCache),
						tokenTypes: values_default(this.tokensMap),
						grammarName: j
					});
					this.definitionErrors = this.definitionErrors.concat(u, D);
				}
			}), isEmpty_default(this.definitionErrors) && (this.recoveryEnabled && this.TRACE_INIT("computeAllProdsFollows", () => {
				this.resyncFollows = computeAllProdsFollows(values_default(this.gastProductionsCache));
			}), this.TRACE_INIT("ComputeLookaheadFunctions", () => {
				var u, D;
				(D = (u = this.lookaheadStrategy).initialize) == null || D.call(u, { rules: values_default(this.gastProductionsCache) }), this.preComputeLookaheadFunctions(values_default(this.gastProductionsCache));
			})), !D.DEFER_DEFINITION_ERRORS_HANDLING && !isEmpty_default(this.definitionErrors)) throw A = map_default(this.definitionErrors, (u) => u.message), Error(`Parser Definition Errors detected:\n ${A.join("\n-------------------------------\n")}`);
		});
	}
	constructor(u, D) {
		this.definitionErrors = [], this.selfAnalysisDone = !1;
		let O = this;
		if (O.initErrorHandler(D), O.initLexerAdapter(), O.initLooksAhead(D), O.initRecognizerEngine(u, D), O.initRecoverable(D), O.initTreeBuilder(D), O.initContentAssist(), O.initGastRecorder(D), O.initPerformanceTracer(D), has_default(D, "ignoredIssues")) throw Error("The <ignoredIssues> IParserConfig property has been deprecated.\n	Please use the <IGNORE_AMBIGUITIES> flag on the relevant DSL method instead.\n	See: https://chevrotain.io/docs/guide/resolving_grammar_errors.html#IGNORING_AMBIGUITIES\n	For further details.");
		this.skipValidations = has_default(D, "skipValidations") ? D.skipValidations : DEFAULT_PARSER_CONFIG.skipValidations;
	}
};
Parser.DEFER_DEFINITION_ERRORS_HANDLING = !1, applyMixins(Parser, [
	Recoverable,
	LooksAhead,
	TreeBuilder,
	LexerAdapter,
	RecognizerEngine,
	RecognizerApi,
	ErrorHandler,
	ContentAssist,
	GastRecorder,
	PerformanceTracer
]);
var EmbeddedActionsParser = class extends Parser {
	constructor(u, O = DEFAULT_PARSER_CONFIG) {
		let k = clone_default(O);
		k.outputCst = !1, super(u, k);
	}
};
export { DEFAULT_PARSER_CONFIG, DEFAULT_RULE_CONFIG, EMPTY_ALT, END_OF_FILE, EmbeddedActionsParser, ParserDefinitionErrorType };
