'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.LS_KEY_DISPLAY = 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;};}();var _desc, _value, _class, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5, _descriptor6, _descriptor7, _descriptor8, _descriptor9, _descriptor10, _descriptor11, _descriptor12, _descriptor13, _descriptor14; // 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 _eventemitter = require('eventemitter3');var _eventemitter2 = _interopRequireDefault(_eventemitter);
var _mobx = require('mobx');
var _store = require('store');var _store2 = _interopRequireDefault(_store);

var _contracts = require('../contracts');var _contracts2 = _interopRequireDefault(_contracts);
var _dapps = require('../util/dapps');function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}function _initDefineProp(target, property, descriptor, context) {if (!descriptor) return;Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 });}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}function _possibleConstructorReturn(self, call) {if (!self) {throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call && (typeof call === "object" || typeof call === "function") ? call : self;}function _inherits(subClass, superClass) {if (typeof superClass !== "function" && superClass !== null) {throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);}subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } });if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;}function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {var desc = {};Object['ke' + 'ys'](descriptor).forEach(function (key) {desc[key] = descriptor[key];});desc.enumerable = !!desc.enumerable;desc.configurable = !!desc.configurable;if ('value' in desc || desc.initializer) {desc.writable = true;}desc = decorators.slice().reverse().reduce(function (desc, decorator) {return decorator(target, property, desc) || desc;}, desc);if (context && desc.initializer !== void 0) {desc.value = desc.initializer ? desc.initializer.call(context) : void 0;desc.initializer = undefined;}if (desc.initializer === void 0) {Object['define' + 'Property'](target, property, desc);desc = null;}return desc;}function _initializerWarningHelper(descriptor, context) {throw new Error('Decorating class property failed. Please ensure that transform-class-properties is enabled.');}

var LS_KEY_DISPLAY = 'displayApps';
var LS_KEY_EXTERNAL_ACCEPT = 'acceptExternal';
var BUILTIN_APPS_KEY = 'BUILTIN_APPS_KEY';

var instance = null;var

DappsStore = (_class = function (_EventEmitter) {_inherits(DappsStore, _EventEmitter);











  function DappsStore(api) {_classCallCheck(this, DappsStore);var _this = _possibleConstructorReturn(this, (DappsStore.__proto__ || Object.getPrototypeOf(DappsStore)).call(this));_initDefineProp(_this, 'apps', _descriptor, _this);_initDefineProp(_this, 'displayApps', _descriptor2, _this);_initDefineProp(_this, 'modalOpen', _descriptor3, _this);_initDefineProp(_this, 'externalOverlayVisible', _descriptor4, _this);_this._api = null;_this._subscriptions = {};_this._cachedApps = {};_this._manifests = {};_this._registryAppsIds = null;_initDefineProp(_this, 'refreshDapps', _descriptor5, _this);_initDefineProp(_this, 'openModal', _descriptor6, _this);_initDefineProp(_this, 'closeModal', _descriptor7, _this);_initDefineProp(_this, 'closeExternalOverlay', _descriptor8, _this);_initDefineProp(_this, 'hideApp', _descriptor9, _this);_initDefineProp(_this, 'showApp', _descriptor10, _this);_initDefineProp(_this, 'readDisplayApps', _descriptor11, _this);_initDefineProp(_this, 'writeDisplayApps', _descriptor12, _this);_initDefineProp(_this, 'setDisplayApps', _descriptor13, _this);_initDefineProp(_this, 'addApps', _descriptor14, _this);_this.















































































































































































































































































    getAppById = function (id) {
      return _this.apps.find(function (app) {return app.id === id;});
    };_this._api = api;_this.readDisplayApps();_this.loadExternalOverlay();_this.subscribeToChanges();return _this;}_createClass(DappsStore, [{ key: 'loadApp', /**
                                                                                                                                                                 * Try to find the app from the local (local or builtin)
                                                                                                                                                                 * apps, else fetch from the node
                                                                                                                                                                 */value: function loadApp(id) {var _this2 = this;var _Contracts$get = _contracts2.default.get(this._api),dappReg = _Contracts$get.dappReg;return this.loadLocalApps().then(function () {var app = _this2.apps.find(function (app) {return app.id === id;});if (app) {return app;}return _this2.fetchRegistryApp(dappReg, id, true);}).then(function (app) {_this2.emit('loaded', app);return app;});} }, { key: 'loadLocalApps', value: function loadLocalApps() {var _this3 = this;return Promise.all([this.fetchBuiltinApps().then(function (apps) {return _this3.addApps(apps);}), this.fetchLocalApps().then(function (apps) {return _this3.addApps(apps, true);})]);} }, { key: 'loadAllApps', value: function loadAllApps() {var _this4 = this;var _Contracts$get2 = _contracts2.default.get(this._api),dappReg = _Contracts$get2.dappReg;return Promise.all([this.loadLocalApps(), this.fetchRegistryApps(dappReg).then(function (apps) {return _this4.addApps(apps);})]).then(this.writeDisplayApps);} }, { key: 'subscribeToChanges', value: function subscribeToChanges() {var _this5 = this;var _Contracts$get3 = _contracts2.default.get(this._api),dappReg = _Contracts$get3.dappReg; // Unsubscribe from previous subscriptions, if any
      if (this._subscriptions.block) {this._api.unsubscribe(this._subscriptions.block);}if (this._subscriptions.filter) {this._api.eth.uninstallFilter(this._subscriptions.filter);} // Subscribe to dapps reg changes
      (0, _dapps.subscribeToChanges)(this._api, dappReg, function (appIds) {var updates = appIds.map(function (appId) {return _this5.fetchRegistryApp(dappReg, appId, true);});Promise.all(updates).then(function (apps) {_this5.addApps(apps);});}).then(function (subscriptions) {_this5._subscriptions = subscriptions;});} }, { key: 'fetchBuiltinApps', value: function fetchBuiltinApps() {var _this6 = this;var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;if (!force && this._cachedApps[BUILTIN_APPS_KEY] !== undefined) {return Promise.resolve(this._cachedApps[BUILTIN_APPS_KEY]);}this._cachedApps[BUILTIN_APPS_KEY] = (0, _dapps.fetchBuiltinApps)(this._api).then(function (apps) {_this6._cachedApps[BUILTIN_APPS_KEY] = apps;return apps;});return Promise.resolve(this._cachedApps[BUILTIN_APPS_KEY]);} }, { key: 'fetchLocalApps', value: function fetchLocalApps() {return (0, _dapps.fetchLocalApps)(this._api);} }, { key: 'fetchRegistryAppIds', value: function fetchRegistryAppIds() {var _this7 = this;var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;if (!force && this._registryAppsIds) {return Promise.resolve(this._registryAppsIds);}this._registryAppsIds = (0, _dapps.fetchRegistryAppIds)(this._api).then(function (appIds) {_this7._registryAppsIds = appIds;return _this7._registryAppsIds;});return Promise.resolve(this._registryAppsIds);} }, { key: 'fetchRegistryApp', value: function fetchRegistryApp(dappReg, appId) {var _this8 = this;var force = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;if (!force && this._cachedApps[appId] !== undefined) {return Promise.resolve(this._cachedApps[appId]);}this._cachedApps[appId] = (0, _dapps.fetchRegistryApp)(this._api, dappReg, appId).then(function (dapp) {_this8._cachedApps[appId] = dapp;return dapp;});return Promise.resolve(this._cachedApps[appId]);} }, { key: 'fetchRegistryApps', value: function fetchRegistryApps(dappReg) {var _this9 = this;return this.fetchRegistryAppIds().then(function (appIds) {var promises = appIds.map(function (appId) {// Fetch the Dapp and display it ASAP
          return _this9.fetchRegistryApp(dappReg, appId).then(function (app) {if (app) {_this9.addApps([app]);}return app;});});return Promise.all(promises);});} }, { key: 'loadExternalOverlay', value: function loadExternalOverlay() {this.externalOverlayVisible = !(_store2.default.get(LS_KEY_EXTERNAL_ACCEPT) || false);} }, { key: 'allApps', get: function get() {return this.apps;} }, { key: 'sortedBuiltin', get: function get() {return this.apps.filter(function (app) {return app.type === 'builtin';});} }, { key: 'sortedLocal', get: function get() {return this.apps.filter(function (app) {return app.type === 'local';});} }, { key: 'sortedNetwork', get: function get() {return this.apps.filter(function (app) {return app.type === 'network';});} }, { key: 'visibleApps', get: function get() {var _this10 = this;return this.apps.filter(function (app) {return _this10.displayApps[app.id] && _this10.displayApps[app.id].visible;});} }, { key: 'visibleBuiltin', get: function get() {return this.visibleApps.filter(function (app) {return !app.noselect && app.type === 'builtin';});} }, { key: 'visibleLocal', get: function get() {return this.visibleApps.filter(function (app) {return app.type === 'local';});} }, { key: 'visibleNetwork', get: function get() {return this.visibleApps.filter(function (app) {return app.type === 'network';});} }, { key: 'visibleViews', get: function get() {return this.visibleApps.filter(function (app) {return !app.noselect && app.type === 'view';});} }], [{ key: 'get', value: function get(api) {if (!instance) {instance = new DappsStore(api);}return instance;} }]);return DappsStore;}(_eventemitter2.default), (_descriptor = _applyDecoratedDescriptor(_class.prototype, 'apps', [_mobx.observable], { enumerable: true, initializer: function initializer() {return [];} }), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, 'displayApps', [_mobx.observable], { enumerable: true, initializer: function initializer() {return {};} }), _descriptor3 = _applyDecoratedDescriptor(_class.prototype, 'modalOpen', [_mobx.observable], { enumerable: true, initializer: function initializer() {return false;} }), _descriptor4 = _applyDecoratedDescriptor(_class.prototype, 'externalOverlayVisible', [_mobx.observable], { enumerable: true, initializer: function initializer() {return true;} }), _applyDecoratedDescriptor(_class.prototype, 'allApps', [_mobx.computed], Object.getOwnPropertyDescriptor(_class.prototype, 'allApps'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'sortedBuiltin', [_mobx.computed], Object.getOwnPropertyDescriptor(_class.prototype, 'sortedBuiltin'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'sortedLocal', [_mobx.computed], Object.getOwnPropertyDescriptor(_class.prototype, 'sortedLocal'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'sortedNetwork', [_mobx.computed], Object.getOwnPropertyDescriptor(_class.prototype, 'sortedNetwork'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'visibleApps', [_mobx.computed], Object.getOwnPropertyDescriptor(_class.prototype, 'visibleApps'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'visibleBuiltin', [_mobx.computed], Object.getOwnPropertyDescriptor(_class.prototype, 'visibleBuiltin'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'visibleLocal', [_mobx.computed], Object.getOwnPropertyDescriptor(_class.prototype, 'visibleLocal'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'visibleNetwork', [_mobx.computed], Object.getOwnPropertyDescriptor(_class.prototype, 'visibleNetwork'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'visibleViews', [_mobx.computed], Object.getOwnPropertyDescriptor(_class.prototype, 'visibleViews'), _class.prototype), _descriptor5 = _applyDecoratedDescriptor(_class.prototype, 'refreshDapps', [_mobx.action], { enumerable: true, initializer: function initializer() {var _this11 = this;return function () {var self = _this11;self._api.parity.dappsRefresh().then(function (res) {if (res === true) {self.loadAllApps();}}).catch(function (err) {console.log(err);});};} }), _descriptor6 = _applyDecoratedDescriptor(_class.prototype, 'openModal', [_mobx.action], { enumerable: true, initializer: function initializer() {var _this12 = this;return function () {_this12.modalOpen = true;};} }), _descriptor7 = _applyDecoratedDescriptor(_class.prototype, 'closeModal', [_mobx.action], { enumerable: true, initializer: function initializer() {var _this13 = this;return function () {_this13.modalOpen = false;};} }), _descriptor8 = _applyDecoratedDescriptor(_class.prototype, 'closeExternalOverlay', [_mobx.action], { enumerable: true, initializer: function initializer() {var _this14 = this;return function () {_this14.externalOverlayVisible = false;_store2.default.set(LS_KEY_EXTERNAL_ACCEPT, true);};} }), _applyDecoratedDescriptor(_class.prototype, 'loadExternalOverlay', [_mobx.action], Object.getOwnPropertyDescriptor(_class.prototype, 'loadExternalOverlay'), _class.prototype), _descriptor9 = _applyDecoratedDescriptor(_class.prototype, 'hideApp', [_mobx.action], { enumerable: true, initializer: function initializer() {var _this15 = this;return function (id) {_this15.setDisplayApps(_defineProperty({}, id, { visible: false }));_this15.writeDisplayApps();};} }), _descriptor10 = _applyDecoratedDescriptor(_class.prototype, 'showApp', [_mobx.action], { enumerable: true, initializer: function initializer() {var _this16 = this;return function (id) {_this16.setDisplayApps(_defineProperty({}, id, { visible: true }));_this16.writeDisplayApps();};} }), _descriptor11 = _applyDecoratedDescriptor(_class.prototype, 'readDisplayApps', [_mobx.action], { enumerable: true, initializer: function initializer() {var _this17 = this;return function () {_this17.displayApps = _store2.default.get(LS_KEY_DISPLAY) || {};};} }), _descriptor12 = _applyDecoratedDescriptor(_class.prototype, 'writeDisplayApps', [_mobx.action], { enumerable: true, initializer: function initializer() {var _this18 = this;return function () {_store2.default.set(LS_KEY_DISPLAY, _this18.displayApps);};} }), _descriptor13 = _applyDecoratedDescriptor(_class.prototype, 'setDisplayApps', [_mobx.action], { enumerable: true, initializer: function initializer() {var _this19 = this;return function (displayApps) {_this19.displayApps = Object.assign({}, _this19.displayApps, displayApps);};} }), _descriptor14 = _applyDecoratedDescriptor(_class.prototype, 'addApps', [_mobx.action], { enumerable: true, initializer: function initializer() {var _this20 = this;return function () {var _apps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];var _local = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;(0, _mobx.transaction)(function () {var apps = _apps.filter(function (app) {return app;}); // Get new apps IDs if available
        var newAppsIds = apps.map(function (app) {return app.id;}).filter(function (id) {return id;});_this20.apps = _this20.apps.filter(function (app) {return !app.id || !newAppsIds.includes(app.id);}).filter(function (app) {return !(app.type === 'local' && _local && apps.indexOf(app) === -1);}).concat(apps || []).sort(function (a, b) {return a.name.localeCompare(b.name);});var visibility = {};apps.forEach(function (app) {if (!_this20.displayApps[app.id]) {visibility[app.id] = { visible: app.visible };}});_this20.setDisplayApps(visibility);});};} })), _class);exports.default = DappsStore;exports.LS_KEY_DISPLAY = LS_KEY_DISPLAY;