import forEach_default from "../../../../../lodash-es/forEach.js";
import values_default from "../../../../../lodash-es/values.js";
import { GAstVisitor } from "../../../../../@chevrotain/gast/lib/src/visitor.js";
import { ParserDefinitionErrorType } from "../parser/parser.js";
function resolveGrammar(e, i) {
	let a = new GastRefResolverVisitor(e, i);
	return a.resolveRefs(), a.errors;
}
var GastRefResolverVisitor = class extends GAstVisitor {
	constructor(e, i) {
		super(), this.nameToTopRule = e, this.errMsgProvider = i, this.errors = [];
	}
	resolveRefs() {
		forEach_default(values_default(this.nameToTopRule), (e) => {
			this.currTopLevel = e, e.accept(this);
		});
	}
	visitNonTerminal(e) {
		let i = this.nameToTopRule[e.nonTerminalName];
		if (i) e.referencedRule = i;
		else {
			let i = this.errMsgProvider.buildRuleNotFoundError(this.currTopLevel, e);
			this.errors.push({
				message: i,
				type: ParserDefinitionErrorType.UNRESOLVED_SUBRULE_REF,
				ruleName: this.currTopLevel.name,
				unresolvedRefName: e.nonTerminalName
			});
		}
	}
};
export { resolveGrammar };
