'use strict';Object.defineProperty(exports, "__esModule", { value: true });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;}; // 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/>.
exports.














personalAccountsInfo = personalAccountsInfo;exports.




























































































































_setVisibleAccounts = _setVisibleAccounts;exports.






setVisibleAccounts = setVisibleAccounts;var _lodash = require('lodash');var _contract = require('@parity/api/lib/contract');var _contract2 = _interopRequireDefault(_contract);var _abi = require('../../contracts/abi');var _wallets2 = require('../../util/wallets');var _wallets3 = _interopRequireDefault(_wallets2);var _balances = require('./balances');var _balances2 = _interopRequireDefault(_balances);var _tokens = require('./tokens');var _tokens2 = _interopRequireDefault(_tokens);var _balancesActions = require('./balancesActions');var _walletActions = require('./walletActions');var _methodDecodingStore = require('@parity/ui/lib/MethodDecoding/methodDecodingStore');var _methodDecodingStore2 = _interopRequireDefault(_methodDecodingStore);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function personalAccountsInfo(accountsInfo) {var accounts = {};var contacts = {};var contracts = {};var hardware = {};var wallets = {};Object.keys(accountsInfo || {}).map(function (address) {return Object.assign({}, accountsInfo[address], { address: address });}).filter(function (account) {return account.meta && (account.uuid || !account.meta.deleted);}).forEach(function (account) {if (account.uuid) {accounts[account.address] = account;} else if (account.meta.wallet) {account.wallet = true;wallets[account.address] = account;} else if (account.meta.contract) {account.contract = true;contracts[account.address] = account;} else if (account.meta.hardware) {account.hardware = true;hardware[account.address] = account;accounts[account.address] = account;} else if (account.meta.external) {account.external = true;accounts[account.address] = account;} else {contacts[account.address] = account;}}); // Load user contracts for Method Decoding
  _methodDecodingStore2.default.loadContracts(contracts);return function (dispatch, getState) {var _getState = getState(),api = _getState.api;var _fetchOwners = Object.values(wallets).map(function (wallet) {var walletContract = new _contract2.default(api, _abi.foundationWallet);return _wallets3.default.fetchOwners(walletContract.at(wallet.address));});return Promise.all(_fetchOwners).then(function (walletsOwners) {return Object.values(wallets).map(function (wallet, index) {wallet.owners = walletsOwners[index].map(function (owner) {return { address: owner, name: accountsInfo[owner] && accountsInfo[owner].name || owner };});return wallet;});}).catch(function () {return [];}).then(function (_wallets) {// We want to separate owned wallets and other wallets
      // However, wallets can be owned by wallets, that can
      // be owned by an account...
      var otherWallets = [].concat(_wallets);var prevLength = void 0;var nextLength = void 0; // If no more other wallets, or if the size decreased, continue...
      do {prevLength = otherWallets.length;otherWallets = otherWallets.map(function (wallet) {var addresses = Object.keys(accounts);var owners = wallet.owners.map(function (o) {return o.address;}); // Owners ∩ Addresses not null : Wallet is owned
          // by one of the accounts
          if ((0, _lodash.intersection)(owners, addresses).length > 0) {accounts[wallet.address] = wallet;return false;}return wallet;}).filter(function (wallet) {return wallet;});nextLength = otherWallets.length;} while (nextLength < prevLength); // And other wallets to contacts...
      otherWallets.forEach(function (wallet) {contacts[wallet.address] = wallet;}); // Cache the _real_ accounts for
      // WalletsUtils (used for sending transactions)
      _wallets3.default.cacheAccounts(accounts);dispatch(_personalAccountsInfo({ accountsInfo: accountsInfo, accounts: accounts, contacts: contacts, contracts: contracts, hardware: hardware }));dispatch((0, _walletActions.attachWallets)(wallets));}).catch(function (error) {console.warn('personalAccountsInfo', error);throw error;});};}function _personalAccountsInfo(data) {return _extends({ type: 'personalAccountsInfo' }, data);}function _setVisibleAccounts(addresses) {return { type: 'setVisibleAccounts', addresses: addresses };}function setVisibleAccounts(addresses) {return function (dispatch, getState) {var visibleAccounts = getState().personal.visibleAccounts;if ((0, _lodash.isEqual)(addresses.sort(), visibleAccounts.sort())) {return;}dispatch(_setVisibleAccounts(addresses)); // Don't update the balances if no new addresses displayed
    if (addresses.length === 0) {
      return;
    }

    var promises = [];

    // Update the Tokens filter to take into account the new
    // addresses if it is not loading (it fetches the
    // balances automatically after loading)
    if (!_tokens2.default.get().loading) {
      promises.push((0, _balancesActions.updateTokensFilter)()(dispatch, getState));
    }

    promises.push(_balances2.default.get().fetchEthBalances({ force: true }));

    return Promise.all(promises);
  };
}