'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.default = undefined;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 _personalActions = require('./personalActions');function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}

var instance = void 0;var

Personal = function () {
  function Personal(store, api) {_classCallCheck(this, Personal);
    this._api = api;
    this._store = store;
  }_createClass(Personal, [{ key: '_subscribeAccountsInfo', value: function _subscribeAccountsInfo()









































    {var _this = this;
      var resolved = false;

      // The Promise will be resolved when the first
      // accounts are loaded
      return new Promise(function (resolve, reject) {
        _this._api.
        subscribe('parity_allAccountsInfo', function (error, accountsInfo) {
          if (error) {
            console.error('parity_allAccountsInfo', error);

            if (!resolved) {
              resolved = true;
              return reject(error);
            }

            return;
          }

          // Add the address to each accounts
          Object.keys(accountsInfo).
          forEach(function (address) {
            accountsInfo[address].address = address;
          });var _store =

          _this._store,dispatch = _store.dispatch,getState = _store.getState;

          (0, _personalActions.personalAccountsInfo)(accountsInfo)(dispatch, getState).
          then(function () {
            if (!resolved) {
              resolved = true;
              return resolve();
            }
          }).
          catch(function (error) {
            if (!resolved) {
              resolved = true;
              return reject(error);
            }
          });
        }).
        then(function (subId) {
          _this.subscriptionId = subId;
        });
      });
    } }, { key: '_unsubscribeAccountsInfo', value: function _unsubscribeAccountsInfo()

    {var _this2 = this;
      // Unsubscribe to any previous
      // subscriptions
      if (this.subscriptionId) {
        return this._api.
        unsubscribe(this.subscriptionId).
        then(function () {
          _this2.subscriptionId = null;
        });
      }

      return Promise.resolve();
    } }, { key: '_removeDeleted', value: function _removeDeleted()

    {var _this3 = this;
      return this._api.parity.
      allAccountsInfo().
      then(function (accountsInfo) {
        return Promise.all(
        Object.
        keys(accountsInfo).
        filter(function (address) {
          var account = accountsInfo[address];

          return !account.uuid && account.meta && account.meta.deleted;
        }).
        map(function (address) {return _this3._api.parity.removeAddress(address);}));

      }).
      then(function (results) {
        if (results.length) {
          console.log('Removed ' + results.length + ' previously marked addresses');
        }
      }).
      catch(function (error) {
        console.warn('removeDeleted', error);
      });
    } }], [{ key: 'get', value: function get(store) {if (!instance && store) {return Personal.init(store);}return instance;} }, { key: 'init', value: function init(store) {var _store$getState = store.getState(),api = _store$getState.api;if (!instance) {instance = new Personal(store, api);} else if (!instance) {throw new Error('The Personal Provider has not been initialized yet');}return instance;} }, { key: 'start', value: function start() {var self = instance;return Personal.stop().then(function () {return Promise.all([self._removeDeleted(), self._subscribeAccountsInfo()]);});} }, { key: 'stop', value: function stop() {if (!instance) {return Promise.resolve();}var self = instance;return self._unsubscribeAccountsInfo();} }]);return Personal;}();exports.default = Personal;