"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const ast = require("./lib-languages-generated-ast.cjs");
const grammarUtils = require("./lib-utils-grammar-utils.cjs");
class DefaultValueConverter {
  convert(input, cstNode) {
    let feature = cstNode.grammarSource;
    if (ast.isCrossReference(feature)) {
      feature = grammarUtils.getCrossReferenceTerminal(feature);
    }
    if (ast.isRuleCall(feature)) {
      const rule = feature.rule.ref;
      if (!rule) {
        throw new Error("This cst node was not parsed by a rule.");
      }
      return this.runConverter(rule, input, cstNode);
    }
    return input;
  }
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  runConverter(rule, input, cstNode) {
    var _a;
    switch (rule.name.toUpperCase()) {
      case "INT":
        return exports.ValueConverter.convertInt(input);
      case "STRING":
        return exports.ValueConverter.convertString(input);
      case "ID":
        return exports.ValueConverter.convertID(input);
    }
    switch ((_a = grammarUtils.getRuleType(rule)) === null || _a === void 0 ? void 0 : _a.toLowerCase()) {
      case "number":
        return exports.ValueConverter.convertNumber(input);
      case "boolean":
        return exports.ValueConverter.convertBoolean(input);
      case "bigint":
        return exports.ValueConverter.convertBigint(input);
      case "date":
        return exports.ValueConverter.convertDate(input);
      default:
        return input;
    }
  }
}
exports.ValueConverter = void 0;
(function(ValueConverter) {
  function convertString(input) {
    let result = "";
    for (let i = 1; i < input.length - 1; i++) {
      const c = input.charAt(i);
      if (c === "\\") {
        const c1 = input.charAt(++i);
        result += convertEscapeCharacter(c1);
      } else {
        result += c;
      }
    }
    return result;
  }
  ValueConverter.convertString = convertString;
  function convertEscapeCharacter(char) {
    switch (char) {
      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 char;
    }
  }
  function convertID(input) {
    if (input.charAt(0) === "^") {
      return input.substring(1);
    } else {
      return input;
    }
  }
  ValueConverter.convertID = convertID;
  function convertInt(input) {
    return parseInt(input);
  }
  ValueConverter.convertInt = convertInt;
  function convertBigint(input) {
    return BigInt(input);
  }
  ValueConverter.convertBigint = convertBigint;
  function convertDate(input) {
    return new Date(input);
  }
  ValueConverter.convertDate = convertDate;
  function convertNumber(input) {
    return Number(input);
  }
  ValueConverter.convertNumber = convertNumber;
  function convertBoolean(input) {
    return input.toLowerCase() === "true";
  }
  ValueConverter.convertBoolean = convertBoolean;
})(exports.ValueConverter || (exports.ValueConverter = {}));
exports.DefaultValueConverter = DefaultValueConverter;
//# sourceMappingURL=lib-parser-value-converter.cjs.map
