var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _LocalDatastoreUtils = require("./LocalDatastoreUtils");
var _Storage = _interopRequireDefault(require("./Storage"));
var LocalDatastoreController = {
  fromPinWithName: function () {
    var _fromPinWithName = (0, _asyncToGenerator2.default)(function* (name) {
      var values = yield _Storage.default.getItemAsync(name);
      if (!values) {
        return [];
      }
      var objects = JSON.parse(values);
      return objects;
    });
    function fromPinWithName() {
      return _fromPinWithName.apply(this, arguments);
    }
    return fromPinWithName;
  }(),
  pinWithName: function (name, value) {
    var values = JSON.stringify(value);
    return _Storage.default.setItemAsync(name, values);
  },
  unPinWithName: function (name) {
    return _Storage.default.removeItemAsync(name);
  },
  getAllContents: function () {
    var _getAllContents = (0, _asyncToGenerator2.default)(function* () {
      var keys = yield _Storage.default.getAllKeysAsync();
      return keys.reduce(function () {
        var _ref = (0, _asyncToGenerator2.default)(function* (previousPromise, key) {
          var LDS = yield previousPromise;
          if ((0, _LocalDatastoreUtils.isLocalDatastoreKey)(key)) {
            var value = yield _Storage.default.getItemAsync(key);
            try {
              LDS[key] = JSON.parse(value);
            } catch (error) {
              console.error('Error getAllContents: ', error);
            }
          }
          return LDS;
        });
        return function () {
          return _ref.apply(this, arguments);
        };
      }(), Promise.resolve({}));
    });
    function getAllContents() {
      return _getAllContents.apply(this, arguments);
    }
    return getAllContents;
  }(),
  getRawStorage: function () {
    var _getRawStorage = (0, _asyncToGenerator2.default)(function* () {
      var keys = yield _Storage.default.getAllKeysAsync();
      return keys.reduce(function () {
        var _ref2 = (0, _asyncToGenerator2.default)(function* (previousPromise, key) {
          var LDS = yield previousPromise;
          var value = yield _Storage.default.getItemAsync(key);
          LDS[key] = value;
          return LDS;
        });
        return function () {
          return _ref2.apply(this, arguments);
        };
      }(), Promise.resolve({}));
    });
    function getRawStorage() {
      return _getRawStorage.apply(this, arguments);
    }
    return getRawStorage;
  }(),
  clear: function () {
    var _clear = (0, _asyncToGenerator2.default)(function* () {
      var keys = yield _Storage.default.getAllKeysAsync();
      var toRemove = [];
      for (var key of keys) {
        if ((0, _LocalDatastoreUtils.isLocalDatastoreKey)(key)) {
          toRemove.push(key);
        }
      }
      var promises = toRemove.map(this.unPinWithName);
      return Promise.all(promises);
    });
    function clear() {
      return _clear.apply(this, arguments);
    }
    return clear;
  }()
};
module.exports = LocalDatastoreController;