import { Alternation as e, Alternative as t, Option as n, Repetition as r, RepetitionMandatory as i, RepetitionMandatoryWithSeparator as a, RepetitionWithSeparator as o } from "../../../../../@chevrotain/gast/lib/src/model.js";
import { GAstVisitor as s } from "../../../../../@chevrotain/gast/lib/src/visitor.js";
import { RestWalker as c } from "./rest.js";
import { tokenStructuredMatcher as l, tokenStructuredMatcherNoCategories as u } from "../../scan/tokens.js";
import { possiblePathsFrom as d } from "./interpreter.js";
//#region ../../node_modules/chevrotain/lib/src/parse/grammar/lookahead.js
var f;
(function(e) {
	e[e.OPTION = 0] = "OPTION", e[e.REPETITION = 1] = "REPETITION", e[e.REPETITION_MANDATORY = 2] = "REPETITION_MANDATORY", e[e.REPETITION_MANDATORY_WITH_SEPARATOR = 3] = "REPETITION_MANDATORY_WITH_SEPARATOR", e[e.REPETITION_WITH_SEPARATOR = 4] = "REPETITION_WITH_SEPARATOR", e[e.ALTERNATION = 5] = "ALTERNATION";
})(f ||= {});
function p(t) {
	/* istanbul ignore else */
	if (t instanceof n || t === "Option") return f.OPTION;
	if (t instanceof r || t === "Repetition") return f.REPETITION;
	if (t instanceof i || t === "RepetitionMandatory") return f.REPETITION_MANDATORY;
	if (t instanceof a || t === "RepetitionMandatoryWithSeparator") return f.REPETITION_MANDATORY_WITH_SEPARATOR;
	if (t instanceof o || t === "RepetitionWithSeparator") return f.REPETITION_WITH_SEPARATOR;
	if (t instanceof e || t === "Alternation") return f.ALTERNATION;
	throw Error("non exhaustive match");
}
function m(e) {
	let { occurrence: t, rule: n, prodType: r, maxLookahead: i } = e, a = p(r);
	return a === f.ALTERNATION ? T(t, n, i) : E(t, n, a, i);
}
function h(e, t, n, r, i, a) {
	let o = T(e, t, n);
	return a(o, r, k(o) ? u : l, i);
}
function g(e, t, n, r, i, a) {
	let o = E(e, t, i, n), s = k(o) ? u : l;
	return a(o[0], s, r);
}
function _(e, t, n, r) {
	let i = e.length, a = e.every((e) => e.every((e) => e.length === 1));
	if (t) return function(t) {
		let r = t.map((e) => e.GATE);
		for (let t = 0; t < i; t++) {
			let i = e[t], a = i.length, o = r[t];
			if (!(o !== void 0 && o.call(this) === !1)) nextPath: for (let e = 0; e < a; e++) {
				let r = i[e], a = r.length;
				for (let e = 0; e < a; e++) if (n(this.LA_FAST(e + 1), r[e]) === !1) continue nextPath;
				return t;
			}
		}
	};
	if (a && !r) {
		let t = e.map((e) => e.flat()).reduce((e, t, n) => (t.forEach((t) => {
			t.tokenTypeIdx in e || (e[t.tokenTypeIdx] = n), t.categoryMatches.forEach((t) => {
				Object.hasOwn(e, t) || (e[t] = n);
			});
		}), e), {});
		return function() {
			return t[this.LA_FAST(1).tokenTypeIdx];
		};
	} else return function() {
		for (let t = 0; t < i; t++) {
			let r = e[t], i = r.length;
			nextPath: for (let e = 0; e < i; e++) {
				let i = r[e], a = i.length;
				for (let e = 0; e < a; e++) if (n(this.LA_FAST(e + 1), i[e]) === !1) continue nextPath;
				return t;
			}
		}
	};
}
function v(e, t, n) {
	let r = e.every((e) => e.length === 1), i = e.length;
	if (r && !n) {
		let t = e.flat();
		if (t.length === 1 && t[0].categoryMatches.length === 0) {
			let e = t[0].tokenTypeIdx;
			return function() {
				return this.LA_FAST(1).tokenTypeIdx === e;
			};
		} else {
			let e = t.reduce((e, t, n) => (e[t.tokenTypeIdx] = !0, t.categoryMatches.forEach((t) => {
				e[t] = !0;
			}), e), []);
			return function() {
				return e[this.LA_FAST(1).tokenTypeIdx] === !0;
			};
		}
	} else return function() {
		nextPath: for (let n = 0; n < i; n++) {
			let r = e[n], i = r.length;
			for (let e = 0; e < i; e++) if (t(this.LA_FAST(e + 1), r[e]) === !1) continue nextPath;
			return !0;
		}
		return !1;
	};
}
var y = class extends c {
	constructor(e, t, n) {
		super(), this.topProd = e, this.targetOccurrence = t, this.targetProdType = n;
	}
	startWalking() {
		return this.walk(this.topProd), this.restDef;
	}
	checkIsTarget(e, t, n, r) {
		return e.idx === this.targetOccurrence && this.targetProdType === t ? (this.restDef = n.concat(r), !0) : !1;
	}
	walkOption(e, t, n) {
		this.checkIsTarget(e, f.OPTION, t, n) || super.walkOption(e, t, n);
	}
	walkAtLeastOne(e, t, n) {
		this.checkIsTarget(e, f.REPETITION_MANDATORY, t, n) || super.walkOption(e, t, n);
	}
	walkAtLeastOneSep(e, t, n) {
		this.checkIsTarget(e, f.REPETITION_MANDATORY_WITH_SEPARATOR, t, n) || super.walkOption(e, t, n);
	}
	walkMany(e, t, n) {
		this.checkIsTarget(e, f.REPETITION, t, n) || super.walkOption(e, t, n);
	}
	walkManySep(e, t, n) {
		this.checkIsTarget(e, f.REPETITION_WITH_SEPARATOR, t, n) || super.walkOption(e, t, n);
	}
}, b = class extends s {
	constructor(e, t, n) {
		super(), this.targetOccurrence = e, this.targetProdType = t, this.targetRef = n, this.result = [];
	}
	checkIsTarget(e, t) {
		e.idx === this.targetOccurrence && this.targetProdType === t && (this.targetRef === void 0 || e === this.targetRef) && (this.result = e.definition);
	}
	visitOption(e) {
		this.checkIsTarget(e, f.OPTION);
	}
	visitRepetition(e) {
		this.checkIsTarget(e, f.REPETITION);
	}
	visitRepetitionMandatory(e) {
		this.checkIsTarget(e, f.REPETITION_MANDATORY);
	}
	visitRepetitionMandatoryWithSeparator(e) {
		this.checkIsTarget(e, f.REPETITION_MANDATORY_WITH_SEPARATOR);
	}
	visitRepetitionWithSeparator(e) {
		this.checkIsTarget(e, f.REPETITION_WITH_SEPARATOR);
	}
	visitAlternation(e) {
		this.checkIsTarget(e, f.ALTERNATION);
	}
};
function x(e) {
	let t = Array(e);
	for (let n = 0; n < e; n++) t[n] = [];
	return t;
}
function S(e) {
	let t = [""];
	for (let n = 0; n < e.length; n++) {
		let r = e[n], i = [];
		for (let e = 0; e < t.length; e++) {
			let n = t[e];
			i.push(n + "_" + r.tokenTypeIdx);
			for (let e = 0; e < r.categoryMatches.length; e++) {
				let t = "_" + r.categoryMatches[e];
				i.push(n + t);
			}
		}
		t = i;
	}
	return t;
}
function C(e, t, n) {
	for (let r = 0; r < e.length; r++) {
		if (r === n) continue;
		let i = e[r];
		for (let e = 0; e < t.length; e++) if (i[t[e]] === !0) return !1;
	}
	return !0;
}
function w(e, t) {
	let n = e.map((e) => d([e], 1)), r = x(n.length), i = n.map((e) => {
		let t = {};
		return e.forEach((e) => {
			S(e.partialPath).forEach((e) => {
				t[e] = !0;
			});
		}), t;
	}), a = n;
	for (let e = 1; e <= t; e++) {
		let n = a;
		a = x(n.length);
		for (let o = 0; o < n.length; o++) {
			let s = n[o];
			for (let n = 0; n < s.length; n++) {
				let c = s[n].partialPath, l = s[n].suffixDef, u = S(c);
				if (C(i, u, o) || l.length === 0 || c.length === t) {
					let e = r[o];
					if (D(e, c) === !1) {
						e.push(c);
						for (let e = 0; e < u.length; e++) {
							let t = u[e];
							i[o][t] = !0;
						}
					}
				} else {
					let t = d(l, e + 1, c);
					a[o] = a[o].concat(t), t.forEach((e) => {
						S(e.partialPath).forEach((e) => {
							i[o][e] = !0;
						});
					});
				}
			}
		}
	}
	return r;
}
function T(e, t, n, r) {
	let i = new b(e, f.ALTERNATION, r);
	return t.accept(i), w(i.result, n);
}
function E(e, n, r, i) {
	let a = new b(e, r);
	n.accept(a);
	let o = a.result, s = new y(n, e, r).startWalking();
	return w([new t({ definition: o }), new t({ definition: s })], i);
}
function D(e, t) {
	compareOtherPath: for (let n = 0; n < e.length; n++) {
		let r = e[n];
		if (r.length === t.length) {
			for (let e = 0; e < r.length; e++) {
				let n = t[e], i = r[e];
				if (!(n === i || i.categoryMatchesMap[n.tokenTypeIdx] !== void 0)) continue compareOtherPath;
			}
			return !0;
		}
	}
	return !1;
}
function O(e, t) {
	return e.length < t.length && e.every((e, n) => {
		let r = t[n];
		return e === r || r.categoryMatchesMap[e.tokenTypeIdx];
	});
}
function k(e) {
	return e.every((e) => e.every((e) => e.every((e) => e.categoryMatches.length === 0)));
}
//#endregion
export { f as PROD_TYPE, _ as buildAlternativesLookAheadFunc, g as buildLookaheadFuncForOptionalProd, h as buildLookaheadFuncForOr, v as buildSingleAlternativeLookaheadFunction, D as containsPath, m as getLookaheadPaths, E as getLookaheadPathsForOptionalProd, T as getLookaheadPathsForOr, p as getProdType, O as isStrictPrefixOfPath };
