"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _parse = require("@dbml/parse");
var _database = _interopRequireDefault(require("../model_structure/database"));
var _mysqlParser = _interopRequireDefault(require("./mysqlParser"));
var _postgresParser = _interopRequireDefault(require("./postgresParser"));
var _dbmlParser = _interopRequireDefault(require("./dbmlParser"));
var _schemarbParser = _interopRequireDefault(require("./schemarbParser"));
var _mssqlParser = _interopRequireDefault(require("./mssqlParser"));
var _ASTGeneration = require("./ANTLR/ASTGeneration");
var _error = require("./error");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
var Parser = /*#__PURE__*/function () {
  function Parser(dbmlCompiler) {
    _classCallCheck(this, Parser);
    this.DBMLCompiler = dbmlCompiler || new _parse.Compiler();
  }
  _createClass(Parser, [{
    key: "parse",
    value: function parse(str, format) {
      try {
        var rawDatabase = {};
        switch (format) {
          case 'mysql':
            rawDatabase = Parser.parseMySQLToJSONv2(str);
            break;
          case 'mysqlLegacy':
            rawDatabase = Parser.parseMySQLToJSON(str);
            break;
          case 'postgres':
            rawDatabase = Parser.parsePostgresToJSONv2(str);
            break;
          case 'snowflake':
            rawDatabase = Parser.parseSnowflakeToJSON(str);
            break;
          case 'postgresLegacy':
            rawDatabase = Parser.parsePostgresToJSON(str);
            break;
          case 'dbml':
            rawDatabase = Parser.parseDBMLToJSON(str);
            break;
          case 'dbmlv2':
            rawDatabase = Parser.parseDBMLToJSONv2(str, this.DBMLCompiler);
            break;
          case 'schemarb':
            rawDatabase = Parser.parseSchemaRbToJSON(str);
            break;
          case 'mssql':
            rawDatabase = Parser.parseMSSQLToJSON(str);
            break;
          case 'json':
            if (_typeof(str) === 'object') {
              rawDatabase = str;
            } else {
              rawDatabase = JSON.parse(str);
            }
            break;
          default:
            break;
        }
        var schema = Parser.parseJSONToDatabase(rawDatabase);
        return schema;
      } catch (diags) {
        throw _error.CompilerError.create(diags);
      }
    }
  }], [{
    key: "parseJSONToDatabase",
    value: function parseJSONToDatabase(rawDatabase) {
      var database = new _database["default"](rawDatabase);
      return database;
    }
  }, {
    key: "parseMySQLToJSONv2",
    value: function parseMySQLToJSONv2(str) {
      return (0, _ASTGeneration.parse)(str, 'mysql');
    }
  }, {
    key: "parseMySQLToJSON",
    value: function parseMySQLToJSON(str) {
      return _mysqlParser["default"].parse(str);
    }
  }, {
    key: "parsePostgresToJSONv2",
    value: function parsePostgresToJSONv2(str) {
      return (0, _ASTGeneration.parse)(str, 'postgres');
    }
  }, {
    key: "parsePostgresToJSON",
    value: function parsePostgresToJSON(str) {
      return _postgresParser["default"].parse(str);
    }
  }, {
    key: "parseDBMLToJSONv2",
    value: function parseDBMLToJSONv2(str, dbmlCompiler) {
      var compiler = dbmlCompiler || new _parse.Compiler();
      compiler.setSource(str);
      var diags = compiler.parse.errors().map(function (error) {
        return {
          message: error.diagnostic,
          location: {
            start: {
              line: error.nodeOrToken.startPos.line + 1,
              column: error.nodeOrToken.startPos.column + 1
            },
            end: {
              line: error.nodeOrToken.endPos.line + 1,
              column: error.nodeOrToken.endPos.column + 1
            }
          },
          code: error.code
        };
      });
      if (diags.length > 0) throw _error.CompilerError.create(diags);
      return compiler.parse.rawDb();
    }
  }, {
    key: "parseDBMLToJSON",
    value: function parseDBMLToJSON(str) {
      return _dbmlParser["default"].parse(str);
    }
  }, {
    key: "parseSchemaRbToJSON",
    value: function parseSchemaRbToJSON(str) {
      return _schemarbParser["default"].parse(str);
    }
  }, {
    key: "parseMSSQLToJSON",
    value: function parseMSSQLToJSON(str) {
      return _mssqlParser["default"].parseWithPegError(str);
    }
  }, {
    key: "parseSnowflakeToJSON",
    value: function parseSnowflakeToJSON(str) {
      return (0, _ASTGeneration.parse)(str, 'snowflake');
    }
  }, {
    key: "parse",
    value: function parse(str, format) {
      return new Parser().parse(str, format);
    }
  }]);
  return Parser;
}();
var _default = Parser;
exports["default"] = _default;