'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;};}();var _desc, _value, _class, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5, _descriptor6, _descriptor7, _descriptor8; // 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/>.

/* global chrome */

var _mobx = require('mobx');

var _store = require('store');var _store2 = _interopRequireDefault(_store);
var _browser = require('useragent.js/lib/browser');var _browser2 = _interopRequireDefault(_browser);

var _constants = require('../util/constants');function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: 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 _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 A_DAY = 24 * 60 * 60 * 1000;
var NEXT_DISPLAY = '_parity::extensionWarning::nextDisplay';

// 'https://chrome.google.com/webstore/detail/parity-ethereum-integrati/himekenlppkgeaoeddcliojfddemadig';
var EXTENSION_PAGE = 'https://chrome.google.com/webstore/detail/himekenlppkgeaoeddcliojfddemadig';

var instance = void 0;var

Store = (_class = function () {





  function Store() {var _this = this;_classCallCheck(this, Store);_initDefineProp(this, 'hasExtension', _descriptor, this);_initDefineProp(this, 'isInstalling', _descriptor2, this);_initDefineProp(this, 'nextDisplay', _descriptor3, this);_initDefineProp(this, 'shouldInstall', _descriptor4, this);_initDefineProp(this, 'setExtensionActive', _descriptor5, this);_initDefineProp(this, 'setInstalling', _descriptor6, this);_initDefineProp(this, 'snoozeWarning', _descriptor7, this);_initDefineProp(this, 'setShouldInstall', _descriptor8, this);this.
























    testInstall = function () {
      _this.readStatus().then(_this.setShouldInstall);
    };this.

    readStatus = function () {
      return new Promise(function (resolve, reject) {
        // Defer checking for the extension since it may not have loaded yet.
        setTimeout(function () {
          var hasExtension = Symbol.for('parity.extension') in window;
          var userAgent = navigator.userAgent || '';
          var isChrome = (_browser2.default.analyze(userAgent) || {}).name.toLowerCase() === 'chrome';
          var isElectron = userAgent.indexOf('Electron') !== -1;

          if (hasExtension) {
            _this.setExtensionActive();
            return resolve(false);
          }

          return resolve(isChrome && !isElectron);
        }, 5000);
      });
    };this.

    installExtension = function () {
      _this.setInstalling(true);

      if (window.location.hostname.toString().endsWith(_constants.DOMAIN)) {
        return _this.inlineInstall().
        catch(function (error) {
          console.warn('Unable to perform direct install', error);
          window.open(EXTENSION_PAGE, '_blank');
        });
      }

      window.open(EXTENSION_PAGE, '_blank');
      return Promise.resolve(true);
    };this.

    inlineInstall = function () {
      return new Promise(function (resolve, reject) {
        var link = document.createElement('link');

        link.setAttribute('rel', 'chrome-webstore-item');
        link.setAttribute('href', EXTENSION_PAGE);
        document.querySelector('head').appendChild(link);

        if (chrome && chrome.webstore && chrome.webstore.install) {
          chrome.webstore.install(EXTENSION_PAGE, resolve, reject);
        } else {
          reject(new Error('Direct installation failed.'));
        }
      });
    };this.nextDisplay = _store2.default.get(NEXT_DISPLAY) || 0;}_createClass(Store, [{ key: 'showWarning', get: function get() {return !this.isInstalling && this.shouldInstall && Date.now() > this.nextDisplay;} }], [{ key: 'get', value: function get()

    {
      if (!instance) {
        instance = new Store();
      }

      return instance;
    } }]);return Store;}(), (_descriptor = _applyDecoratedDescriptor(_class.prototype, 'hasExtension', [_mobx.observable], { enumerable: true, initializer: function initializer() {return false;} }), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, 'isInstalling', [_mobx.observable], { enumerable: true, initializer: function initializer() {return false;} }), _descriptor3 = _applyDecoratedDescriptor(_class.prototype, 'nextDisplay', [_mobx.observable], { enumerable: true, initializer: function initializer() {return 0;} }), _descriptor4 = _applyDecoratedDescriptor(_class.prototype, 'shouldInstall', [_mobx.observable], { enumerable: true, initializer: function initializer() {return false;} }), _applyDecoratedDescriptor(_class.prototype, 'showWarning', [_mobx.computed], Object.getOwnPropertyDescriptor(_class.prototype, 'showWarning'), _class.prototype), _descriptor5 = _applyDecoratedDescriptor(_class.prototype, 'setExtensionActive', [_mobx.action], { enumerable: true, initializer: function initializer() {var _this2 = this;return function () {_this2.hasExtension = true;};} }), _descriptor6 = _applyDecoratedDescriptor(_class.prototype, 'setInstalling', [_mobx.action], { enumerable: true, initializer: function initializer() {var _this3 = this;return function (isInstalling) {_this3.isInstalling = isInstalling;};} }), _descriptor7 = _applyDecoratedDescriptor(_class.prototype, 'snoozeWarning', [_mobx.action], { enumerable: true, initializer: function initializer() {var _this4 = this;return function () {var sleep = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : A_DAY;_this4.nextDisplay = Date.now() + sleep;_store2.default.set(NEXT_DISPLAY, _this4.nextDisplay);};} }), _descriptor8 = _applyDecoratedDescriptor(_class.prototype, 'setShouldInstall', [_mobx.action], { enumerable: true, initializer: function initializer() {var _this5 = this;return function (status) {_this5.shouldInstall = status;};} })), _class);exports.default = Store;