import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
var {
  promisify
} = require("util");
import * as url from "node:url";
var {
  init_db,
  start_db,
  stop_db,
  new_db,
  drop_db,
  execute_sql
} = require("./index.node");
export class Database {
  constructor(root_dir) {
    this.db = void 0;
    this.root_dir = void 0;
    this.used = false;
    this.root_dir = root_dir != null ? root_dir : ".";
  }
  start() {
    var _this = this;
    return _asyncToGenerator(function* () {
      var db = yield _this._get_db();
      return db && start_db.call(db);
    })();
  }
  stop() {
    var _this2 = this;
    return _asyncToGenerator(function* () {
      if (_this2.db) {
        var res = yield stop_db.call(_this2.db);
        _this2.used = true;
        _this2.db = undefined;
      }
      return _this2;
    })();
  }
  new_db(name) {
    var _this3 = this;
    return _asyncToGenerator(function* () {
      var db = yield _this3._get_db();
      return db && new_db.call(db, name);
    })();
  }
  execute_sql(uri, sql) {
    var _this4 = this;
    return _asyncToGenerator(function* () {
      var db = yield _this4._get_db();
      return db && execute_sql.call(db, uri, sql);
    })();
  }
  drop_db(uri) {
    var _this5 = this;
    return _asyncToGenerator(function* () {
      if (_this5.db) {
        var url_obj = new URL(uri);
        var db_name = url_obj.pathname.split("/")[1];
        var parsed_uri = url.format(url_obj, {
          search: false
        });
        yield drop_db.call(_this5.db, parsed_uri, db_name);
      }
    })();
  }
  _get_db() {
    var _this6 = this;
    return _asyncToGenerator(function* () {
      if (!_this6.db) {
        _this6.db = init_db(_this6.root_dir);
      }
      return _this6.db;
    })();
  }
}
module.exports = Database;
//# sourceMappingURL=index.js.map