'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.default = exports.LS_REQUESTS_KEY = undefined;var _extends = Object.assign || function (target) {for (var i = 1; i < arguments.length; i++) {var source = arguments[i];for (var key in source) {if (Object.prototype.hasOwnProperty.call(source, key)) {target[key] = source[key];}}}return target;};var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}(); // Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.

var _store = require('store');var _store2 = _interopRequireDefault(_store);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}

var LS_REQUESTS_KEY = exports.LS_REQUESTS_KEY = '_parity::requests';var

SavedRequests = function () {function SavedRequests() {_classCallCheck(this, SavedRequests);}_createClass(SavedRequests, [{ key: 'load', value: function load(
    api) {
      return this.loadRequests(api).
      catch(function (error) {
        console.error(error);
        return [];
      });
    }

    /**
       * Load the requests of the current network
       */ }, { key: 'loadRequests', value: function loadRequests(
    api) {var _this = this;
      var requests = this._get();

      return api.parity.localTransactions().
      then(function (localTransactions) {
        var promises = Object.values(requests).map(function (request) {var
          requestId = request.requestId,transactionHash = request.transactionHash;

          if (transactionHash) {
            // The transaction might be from an other
            // chain
            if (!localTransactions[transactionHash]) {
              _this.remove(requestId);
              return null;
            }

            return request;
          }

          // The request hasn't been signed yet
          return _this._requestExists(api, requestId).
          then(function (exists) {
            if (!exists) {
              _this.remove(requestId);
              return null;
            }

            return request;
          });
        });

        return Promise.all(promises);
      }).
      then(function (requests) {return requests.filter(function (request) {return request;});});
    } }, { key: 'save', value: function save(

    requestId, requestData) {
      var requests = this._get();

      requests[requestId] = _extends({},
      requests[requestId] || {},
      requestData);


      this._set(requests);
    } }, { key: 'remove', value: function remove(

    requestId) {
      var requests = this._get();

      delete requests[requestId];
      this._set(requests);
    } }, { key: '_get', value: function _get()

    {
      return _store2.default.get(LS_REQUESTS_KEY) || {};
    } }, { key: '_set', value: function _set()

    {var requests = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      if (Object.keys(requests).length > 0) {
        return _store2.default.set(LS_REQUESTS_KEY, requests);
      }

      return _store2.default.remove(LS_REQUESTS_KEY);
    } }, { key: '_requestExists', value: function _requestExists(

    api, requestId) {
      return api.parity.
      checkRequest(requestId).
      then(function () {return true;}).
      catch(function () {
        return false;
      });
    } }]);return SavedRequests;}();exports.default = SavedRequests;