var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _LocalDatastoreUtils = require("./LocalDatastoreUtils");
var RNStorage = require('./StorageController.react-native');
var LocalDatastoreController = {
  fromPinWithName: function () {
    var _fromPinWithName = (0, _asyncToGenerator2.default)(function* (name) {
      var values = yield RNStorage.getItemAsync(name);
      if (!values) {
        return [];
      }
      var objects = JSON.parse(values);
      return objects;
    });
    function fromPinWithName() {
      return _fromPinWithName.apply(this, arguments);
    }
    return fromPinWithName;
  }(),
  pinWithName: function () {
    var _pinWithName = (0, _asyncToGenerator2.default)(function* (name, value) {
      try {
        var values = JSON.stringify(value);
        yield RNStorage.setItemAsync(name, values);
      } catch (e) {
        console.error(e.message);
      }
    });
    function pinWithName() {
      return _pinWithName.apply(this, arguments);
    }
    return pinWithName;
  }(),
  unPinWithName: function (name) {
    return RNStorage.removeItemAsync(name);
  },
  getAllContents: function () {
    var _getAllContents = (0, _asyncToGenerator2.default)(function* () {
      var keys = yield RNStorage.getAllKeysAsync();
      var batch = [];
      for (var i = 0; i < keys.length; i += 1) {
        var key = keys[i];
        if ((0, _LocalDatastoreUtils.isLocalDatastoreKey)(key)) {
          batch.push(key);
        }
      }
      var LDS = {};
      var results = [];
      try {
        results = yield RNStorage.multiGet(batch);
      } catch (error) {
        console.error('Error getAllContents: ', error);
        return {};
      }
      results.forEach(function (pair) {
        var _pair = (0, _slicedToArray2.default)(pair, 2),
          key = _pair[0],
          value = _pair[1];
        try {
          LDS[key] = JSON.parse(value);
        } catch (error) {
          LDS[key] = null;
        }
      });
      return LDS;
    });
    function getAllContents() {
      return _getAllContents.apply(this, arguments);
    }
    return getAllContents;
  }(),
  getRawStorage: function () {
    var _getRawStorage = (0, _asyncToGenerator2.default)(function* () {
      var keys = yield RNStorage.getAllKeysAsync();
      var storage = {};
      var results = yield RNStorage.multiGet(keys);
      results.map(function (pair) {
        var _pair2 = (0, _slicedToArray2.default)(pair, 2),
          key = _pair2[0],
          value = _pair2[1];
        storage[key] = value;
      });
      return storage;
    });
    function getRawStorage() {
      return _getRawStorage.apply(this, arguments);
    }
    return getRawStorage;
  }(),
  clear: function () {
    var _clear = (0, _asyncToGenerator2.default)(function* () {
      var keys = yield RNStorage.getAllKeysAsync();
      var batch = [];
      for (var i = 0; i < keys.length; i += 1) {
        var key = keys[i];
        if ((0, _LocalDatastoreUtils.isLocalDatastoreKey)(key)) {
          batch.push(key);
        }
      }
      return RNStorage.multiRemove(batch).catch(function (error) {
        return console.error('Error clearing local datastore: ', error);
      });
    });
    function clear() {
      return _clear.apply(this, arguments);
    }
    return clear;
  }()
};
module.exports = LocalDatastoreController;