import flatten_default from "../../../../../lodash-es/flatten.js";
import map_default from "../../../../../lodash-es/map.js";
import uniq_default from "../../../../../lodash-es/uniq.js";
import { NonTerminal, Terminal } from "../../../../../@chevrotain/gast/lib/src/model.js";
import { isBranchingProd, isOptionalProd, isSequenceProd } from "../../../../../@chevrotain/gast/lib/src/helpers.js";
function first(e) {
	/* istanbul ignore else */
	if (e instanceof NonTerminal) return first(e.referencedRule);
	if (e instanceof Terminal) return firstForTerminal(e);
	if (isSequenceProd(e)) return firstForSequence(e);
	if (isBranchingProd(e)) return firstForBranching(e);
	throw Error("non exhaustive match");
}
function firstForSequence(e) {
	let o = [], s = e.definition, c = 0, l = s.length > c, u, d = !0;
	for (; l && d;) u = s[c], d = isOptionalProd(u), o = o.concat(first(u)), c += 1, l = s.length > c;
	return uniq_default(o);
}
function firstForBranching(s) {
	return uniq_default(flatten_default(map_default(s.definition, (e) => first(e))));
}
function firstForTerminal(e) {
	return [e.terminalType];
}
export { first };
