import { isCrossReference, isRuleCall } from "../languages/generated/ast.js";
import { getCrossReferenceTerminal, getRuleType } from "../utils/grammar-utils.js";
var DefaultValueConverter = class {
	convert(r, i) {
		let a = i.grammarSource;
		if (isCrossReference(a) && (a = getCrossReferenceTerminal(a)), isRuleCall(a)) {
			let e = a.rule.ref;
			if (!e) throw Error("This cst node was not parsed by a rule.");
			return this.runConverter(e, r, i);
		}
		return r;
	}
	runConverter(e, t, n) {
		switch (e.name.toUpperCase()) {
			case "INT": return ValueConverter.convertInt(t);
			case "STRING": return ValueConverter.convertString(t);
			case "ID": return ValueConverter.convertID(t);
		}
		switch (getRuleType(e)?.toLowerCase()) {
			case "number": return ValueConverter.convertNumber(t);
			case "boolean": return ValueConverter.convertBoolean(t);
			case "bigint": return ValueConverter.convertBigint(t);
			case "date": return ValueConverter.convertDate(t);
			default: return t;
		}
	}
}, ValueConverter;
(function(e) {
	function t(e) {
		let t = "";
		for (let r = 1; r < e.length - 1; r++) {
			let i = e.charAt(r);
			if (i === "\\") {
				let i = e.charAt(++r);
				t += n(i);
			} else t += i;
		}
		return t;
	}
	e.convertString = t;
	function n(e) {
		switch (e) {
			case "b": return "\b";
			case "f": return "\f";
			case "n": return "\n";
			case "r": return "\r";
			case "t": return "	";
			case "v": return "\v";
			case "0": return "\0";
			default: return e;
		}
	}
	function r(e) {
		return e.charAt(0) === "^" ? e.substring(1) : e;
	}
	e.convertID = r;
	function i(e) {
		return parseInt(e);
	}
	e.convertInt = i;
	function a(e) {
		return BigInt(e);
	}
	e.convertBigint = a;
	function o(e) {
		return new Date(e);
	}
	e.convertDate = o;
	function s(e) {
		return Number(e);
	}
	e.convertNumber = s;
	function c(e) {
		return e.toLowerCase() === "true";
	}
	e.convertBoolean = c;
})(ValueConverter ||= {});
export { DefaultValueConverter, ValueConverter };
