import { Alternation as e, NonTerminal as t, Option as n, Repetition as r, RepetitionMandatory as i, RepetitionMandatoryWithSeparator as a, RepetitionWithSeparator as o, Terminal as s } from "../../@chevrotain/gast/lib/src/model.js";
import "../../chevrotain/lib/src/api.js";
import c from "../../lodash-es/map.js";
import l from "../../lodash-es/filter.js";
//#region ../../node_modules/chevrotain-allstar/lib/atn.js
function u(e, t, n) {
	return `${e.name}_${t}_${n}`;
}
var d = class {
	constructor(e) {
		this.target = e;
	}
	isEpsilon() {
		return !1;
	}
}, f = class extends d {
	constructor(e, t) {
		super(e), this.tokenType = t;
	}
}, p = class extends d {
	constructor(e) {
		super(e);
	}
	isEpsilon() {
		return !0;
	}
}, m = class extends d {
	constructor(e, t, n) {
		super(e), this.rule = t, this.followState = n;
	}
	isEpsilon() {
		return !0;
	}
};
function h(e) {
	let t = {
		decisionMap: {},
		decisionStates: [],
		ruleToStartState: /* @__PURE__ */ new Map(),
		ruleToStopState: /* @__PURE__ */ new Map(),
		states: []
	};
	g(t, e);
	let n = e.length;
	for (let r = 0; r < n; r++) {
		let n = e[r], i = w(t, n, n);
		i !== void 0 && P(t, n, i);
	}
	return t;
}
function g(e, t) {
	let n = t.length;
	for (let r = 0; r < n; r++) {
		let n = t[r], i = I(e, n, void 0, { type: 2 }), a = I(e, n, void 0, { type: 7 });
		i.stop = a, e.ruleToStartState.set(n, i), e.ruleToStopState.set(n, a);
	}
}
function _(c, l, u) {
	return u instanceof s ? M(c, l, u.terminalType, u) : u instanceof t ? N(c, l, u) : u instanceof e ? S(c, l, u) : u instanceof n ? C(c, l, u) : u instanceof r ? v(c, l, u) : u instanceof o ? y(c, l, u) : u instanceof i ? b(c, l, u) : u instanceof a ? x(c, l, u) : w(c, l, u);
}
function v(e, t, n) {
	let r = I(e, t, n, { type: 5 });
	return O(e, r), E(e, t, n, k(e, t, r, n, w(e, t, n)));
}
function y(e, t, n) {
	let r = I(e, t, n, { type: 5 });
	return O(e, r), E(e, t, n, k(e, t, r, n, w(e, t, n)), M(e, t, n.separator, n));
}
function b(e, t, n) {
	let r = I(e, t, n, { type: 4 });
	return O(e, r), T(e, t, n, k(e, t, r, n, w(e, t, n)));
}
function x(e, t, n) {
	let r = I(e, t, n, { type: 4 });
	return O(e, r), T(e, t, n, k(e, t, r, n, w(e, t, n)), M(e, t, n.separator, n));
}
function S(e, t, n) {
	let r = I(e, t, n, { type: 1 });
	return O(e, r), k(e, t, r, n, ...c(n.definition, (n) => _(e, t, n)));
}
function C(e, t, n) {
	let r = I(e, t, n, { type: 1 });
	return O(e, r), D(e, t, n, k(e, t, r, n, w(e, t, n)));
}
function w(e, t, n) {
	let r = l(c(n.definition, (n) => _(e, t, n)), (e) => e !== void 0);
	return r.length === 1 ? r[0] : r.length === 0 ? void 0 : j(e, r);
}
function T(e, t, n, r, i) {
	let a = r.left, o = r.right, s = I(e, t, n, { type: 11 });
	O(e, s);
	let c = I(e, t, n, { type: 12 });
	return a.loopback = s, c.loopback = s, e.decisionMap[u(t, i ? "RepetitionMandatoryWithSeparator" : "RepetitionMandatory", n.idx)] = s, F(o, s), i === void 0 ? (F(s, a), F(s, c)) : (F(s, c), F(s, i.left), F(i.right, a)), {
		left: a,
		right: c
	};
}
function E(e, t, n, r, i) {
	let a = r.left, o = r.right, s = I(e, t, n, { type: 10 });
	O(e, s);
	let c = I(e, t, n, { type: 12 }), l = I(e, t, n, { type: 9 });
	return s.loopback = l, c.loopback = l, F(s, a), F(s, c), F(o, l), i === void 0 ? F(l, s) : (F(l, c), F(l, i.left), F(i.right, a)), e.decisionMap[u(t, i ? "RepetitionWithSeparator" : "Repetition", n.idx)] = s, {
		left: s,
		right: c
	};
}
function D(e, t, n, r) {
	let i = r.left, a = r.right;
	return F(i, a), e.decisionMap[u(t, "Option", n.idx)] = i, r;
}
function O(e, t) {
	return e.decisionStates.push(t), t.decision = e.decisionStates.length - 1, t.decision;
}
function k(e, t, n, r, ...i) {
	let a = I(e, t, r, {
		type: 8,
		start: n
	});
	n.end = a;
	for (let e of i) e === void 0 ? F(n, a) : (F(n, e.left), F(e.right, a));
	let o = {
		left: n,
		right: a
	};
	return e.decisionMap[u(t, A(r), r.idx)] = n, o;
}
function A(t) {
	if (t instanceof e) return "Alternation";
	if (t instanceof n) return "Option";
	if (t instanceof r) return "Repetition";
	if (t instanceof o) return "RepetitionWithSeparator";
	if (t instanceof i) return "RepetitionMandatory";
	if (t instanceof a) return "RepetitionMandatoryWithSeparator";
	throw Error("Invalid production type encountered");
}
function j(e, t) {
	let n = t.length;
	for (let r = 0; r < n - 1; r++) {
		let n = t[r], i;
		n.left.transitions.length === 1 && (i = n.left.transitions[0]);
		let a = i instanceof m, o = i, s = t[r + 1].left;
		n.left.type === 1 && n.right.type === 1 && i !== void 0 && (a && o.followState === n.right || i.target === n.right) ? (a ? o.followState = s : i.target = s, R(e, n.right)) : F(n.right, s);
	}
	let r = t[0], i = t[n - 1];
	return {
		left: r.left,
		right: i.right
	};
}
function M(e, t, n, r) {
	let i = I(e, t, r, { type: 1 }), a = I(e, t, r, { type: 1 });
	return L(i, new f(a, n)), {
		left: i,
		right: a
	};
}
function N(e, t, n) {
	let r = n.referencedRule, i = e.ruleToStartState.get(r), a = I(e, t, n, { type: 1 }), o = I(e, t, n, { type: 1 });
	return L(a, new m(i, r, o)), {
		left: a,
		right: o
	};
}
function P(e, t, n) {
	let r = e.ruleToStartState.get(t);
	F(r, n.left);
	let i = e.ruleToStopState.get(t);
	return F(n.right, i), {
		left: r,
		right: i
	};
}
function F(e, t) {
	L(e, new p(t));
}
function I(e, t, n, r) {
	let i = Object.assign({
		atn: e,
		production: n,
		epsilonOnlyTransitions: !1,
		rule: t,
		transitions: [],
		nextTokenWithinRule: [],
		stateNumber: e.states.length
	}, r);
	return e.states.push(i), i;
}
function L(e, t) {
	e.transitions.length === 0 && (e.epsilonOnlyTransitions = t.isEpsilon()), e.transitions.push(t);
}
function R(e, t) {
	e.states.splice(e.states.indexOf(t), 1);
}
//#endregion
export { f as AtomTransition, p as EpsilonTransition, m as RuleTransition, u as buildATNKey, h as createATN };
