"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Database = void 0;
var _regeneratorRuntime2 = _interopRequireDefault(require("@babel/runtime/helpers/regeneratorRuntime"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var url = _interopRequireWildcard(require("node:url"));
var _require = require("util"),
  promisify = _require.promisify;
var _require2 = require("./index.node"),
  init_db = _require2.init_db,
  start_db = _require2.start_db,
  stop_db = _require2.stop_db,
  _new_db = _require2.new_db,
  _drop_db = _require2.drop_db,
  _execute_sql = _require2.execute_sql;
var Database = /*#__PURE__*/function () {
  function Database(root_dir) {
    (0, _classCallCheck2["default"])(this, Database);
    (0, _defineProperty2["default"])(this, "db", void 0);
    (0, _defineProperty2["default"])(this, "root_dir", void 0);
    (0, _defineProperty2["default"])(this, "used", false);
    this.root_dir = root_dir !== null && root_dir !== void 0 ? root_dir : ".";
  }
  (0, _createClass2["default"])(Database, [{
    key: "start",
    value: function () {
      var _start = (0, _asyncToGenerator2["default"])( /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee() {
        var db;
        return (0, _regeneratorRuntime2["default"])().wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return this._get_db();
              case 2:
                db = _context.sent;
                return _context.abrupt("return", db && start_db.call(db));
              case 4:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this);
      }));
      function start() {
        return _start.apply(this, arguments);
      }
      return start;
    }()
  }, {
    key: "stop",
    value: function () {
      var _stop = (0, _asyncToGenerator2["default"])( /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee2() {
        var res;
        return (0, _regeneratorRuntime2["default"])().wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                if (!this.db) {
                  _context2.next = 6;
                  break;
                }
                _context2.next = 3;
                return stop_db.call(this.db);
              case 3:
                res = _context2.sent;
                this.used = true;
                this.db = undefined;
              case 6:
                return _context2.abrupt("return", this);
              case 7:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2, this);
      }));
      function stop() {
        return _stop.apply(this, arguments);
      }
      return stop;
    }()
  }, {
    key: "new_db",
    value: function () {
      var _new_db2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee3(name) {
        var db;
        return (0, _regeneratorRuntime2["default"])().wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                _context3.next = 2;
                return this._get_db();
              case 2:
                db = _context3.sent;
                return _context3.abrupt("return", db && _new_db.call(db, name));
              case 4:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3, this);
      }));
      function new_db(_x) {
        return _new_db2.apply(this, arguments);
      }
      return new_db;
    }()
  }, {
    key: "execute_sql",
    value: function () {
      var _execute_sql2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee4(uri, sql) {
        var db;
        return (0, _regeneratorRuntime2["default"])().wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                _context4.next = 2;
                return this._get_db();
              case 2:
                db = _context4.sent;
                return _context4.abrupt("return", db && _execute_sql.call(db, uri, sql));
              case 4:
              case "end":
                return _context4.stop();
            }
          }
        }, _callee4, this);
      }));
      function execute_sql(_x2, _x3) {
        return _execute_sql2.apply(this, arguments);
      }
      return execute_sql;
    }()
  }, {
    key: "drop_db",
    value: function () {
      var _drop_db2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee5(uri) {
        var url_obj, db_name, parsed_uri;
        return (0, _regeneratorRuntime2["default"])().wrap(function _callee5$(_context5) {
          while (1) {
            switch (_context5.prev = _context5.next) {
              case 0:
                if (!this.db) {
                  _context5.next = 6;
                  break;
                }
                url_obj = new URL(uri);
                db_name = url_obj.pathname.split("/")[1];
                parsed_uri = url.format(url_obj, {
                  search: false
                });
                _context5.next = 6;
                return _drop_db.call(this.db, parsed_uri, db_name);
              case 6:
              case "end":
                return _context5.stop();
            }
          }
        }, _callee5, this);
      }));
      function drop_db(_x4) {
        return _drop_db2.apply(this, arguments);
      }
      return drop_db;
    }()
  }, {
    key: "_get_db",
    value: function () {
      var _get_db2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee6() {
        return (0, _regeneratorRuntime2["default"])().wrap(function _callee6$(_context6) {
          while (1) {
            switch (_context6.prev = _context6.next) {
              case 0:
                if (!this.db) {
                  this.db = init_db(this.root_dir);
                }
                return _context6.abrupt("return", this.db);
              case 2:
              case "end":
                return _context6.stop();
            }
          }
        }, _callee6, this);
      }));
      function _get_db() {
        return _get_db2.apply(this, arguments);
      }
      return _get_db;
    }()
  }]);
  return Database;
}();
exports.Database = Database;
module.exports = Database;
//# sourceMappingURL=index.js.map