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