'use strict';
'use warning';

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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 _path = _interopRequireDefault(require("path"));

var _lodash = _interopRequireDefault(require("lodash"));

var _fs = _interopRequireDefault(require("fs"));

var JSONDB = /*#__PURE__*/function () {
  function JSONDB(options) {
    (0, _classCallCheck2["default"])(this, JSONDB);

    /** Init This Options In The Class  **/
    this.options = _lodash["default"].extend({
      jsonfile: _path["default"].resolve(__dirname, "DB.json")
    }, options);
    /** End Of Init FUnction In The Class**/

    this.instanceDB = _fs["default"].readFileSync(this.options.jsonfile, {
      encoding: "utf8"
    });
  }
  /**
   *
   * @param {String} name
   */


  (0, _createClass2["default"])(JSONDB, [{
    key: "addComponent",
    value: function () {
      var _addComponent = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(name) {
        var _this = this;

        return _regenerator["default"].wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.next = 2;
                return _fs["default"].open(this.options.jsonfile, 'r+', /*#__PURE__*/function () {
                  var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(error, fd) {
                    var mReadFile, DB, DBEncode;
                    return _regenerator["default"].wrap(function _callee$(_context) {
                      while (1) {
                        switch (_context.prev = _context.next) {
                          case 0:
                            _context.next = 2;
                            return _fs["default"].readFileSync(_this.options.jsonfile, "utf8");

                          case 2:
                            mReadFile = _context.sent;
                            DB = JSON.parse(mReadFile);
                            DB[name] = {};
                            /** Decoder json data to Shrinkfy **/

                            /** Decoder json data to Shrinkfy **/
                            DBEncode = JSON.stringify(DB, null, 2);
                            _context.next = 8;
                            return _fs["default"].writeFileSync(_this.options.jsonfile, DBEncode);

                          case 8:
                            _context.next = 10;
                            return _fs["default"].close(fd, function (error) {
                              if (error) {
                                console.error("Stream Error");
                              }
                            });

                          case 10:
                          case "end":
                            return _context.stop();
                        }
                      }
                    }, _callee);
                  }));

                  return function (_x2, _x3) {
                    return _ref.apply(this, arguments);
                  };
                }());

              case 2:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2, this);
      }));

      function addComponent(_x) {
        return _addComponent.apply(this, arguments);
      }

      return addComponent;
    }()
    /**
     *
     * @param {String} name
     */

  }, {
    key: "removeComponent",
    value: function () {
      var _removeComponent = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(name) {
        var _this2 = this;

        return _regenerator["default"].wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                _context4.next = 2;
                return _fs["default"].open(this.options.jsonfile, 'r+', /*#__PURE__*/function () {
                  var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(error, fd) {
                    var mReadFile, DB, DBEncode;
                    return _regenerator["default"].wrap(function _callee3$(_context3) {
                      while (1) {
                        switch (_context3.prev = _context3.next) {
                          case 0:
                            _context3.next = 2;
                            return _fs["default"].readFileSync(_this2.options.jsonfile, "utf8");

                          case 2:
                            mReadFile = _context3.sent;
                            DB = JSON.parse(mReadFile);
                            delete DB[name];
                            /** Decoder json data to Shrinkfy **/

                            /** Decoder json data to Shrinkfy **/
                            DBEncode = JSON.stringify(DB, null, 2);
                            _context3.next = 8;
                            return _fs["default"].writeFileSync(_this2.options.jsonfile, DBEncode);

                          case 8:
                            _context3.next = 10;
                            return _fs["default"].close(fd, function (error) {
                              if (error) {
                                console.error("Stream Error");
                              }
                            });

                          case 10:
                          case "end":
                            return _context3.stop();
                        }
                      }
                    }, _callee3);
                  }));

                  return function (_x5, _x6) {
                    return _ref2.apply(this, arguments);
                  };
                }());

              case 2:
              case "end":
                return _context4.stop();
            }
          }
        }, _callee4, this);
      }));

      function removeComponent(_x4) {
        return _removeComponent.apply(this, arguments);
      }

      return removeComponent;
    }()
  }, {
    key: "ReadAll",
    value: function ReadAll() {
      return JSON.parse(this.instanceDB);
    }
    /**
     *
     * @param {JSON} jsonStructur
     * @constructor
     */

  }, {
    key: "Insert",
    value: function Insert() {
      var jsonStructur = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

      var data = _fs["default"].readFileSync(this.options.jsonfile, "utf8");

      var DB = JSON.parse(data);
      DB.push(jsonStructur);
      /** Decoder json data to Shrinkfy **/

      var DBEncode = JSON.stringify(DB);

      _fs["default"].writeFileSync(this.options.jsonfile, DBEncode);
    }
  }]);
  return JSONDB;
}();

var _default = JSONDB;
exports["default"] = _default;
//# sourceMappingURL=index.js.map