text: 'use strict';
  
  Object.defineProperty(exports, '__esModule', { value: true });
  
  function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  
  var Utils = require('web3-utils');
  var web3CoreHelpers = require('web3-core-helpers');
  var cloneDeep = _interopDefault(require('lodash/cloneDeep'));
  var _get = _interopDefault(require('@babel/runtime/helpers/get'));
  var isFunction = _interopDefault(require('lodash/isFunction'));
  var EventEmitter = _interopDefault(require('eventemitter3'));
  var _createClass = _interopDefault(require('@babel/runtime/helpers/createClass'));
  var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallCheck'));
  var _possibleConstructorReturn = _interopDefault(require('@babel/runtime/helpers/possibleConstructorReturn'));
  var _getPrototypeOf = _interopDefault(require('@babel/runtime/helpers/getPrototypeOf'));
  var _inherits = _interopDefault(require('@babel/runtime/helpers/inherits'));
  
  var AbstractSubscription =
  function (_EventEmitter) {
    _inherits(AbstractSubscription, _EventEmitter);
    function AbstractSubscription(type, method, options, utils, formatters, moduleInstance) {
      var _this;
      _classCallCheck(this, AbstractSubscription);
      _this = _possibleConstructorReturn(this, _getPrototypeOf(AbstractSubscription).call(this));
      _this.type = type;
      _this.method = method;
      _this.options = options || {};
      _this.utils = utils;
      _this.formatters = formatters;
      _this.moduleInstance = moduleInstance;
      _this.id = null;
      return _this;
    }
    _createClass(AbstractSubscription, [{
      key: "beforeSubscription",
      value: function beforeSubscription(moduleInstance) {}
    }, {
      key: "onNewSubscriptionItem",
      value: function onNewSubscriptionItem(subscriptionItem) {
        return subscriptionItem;
      }
    }, {
      key: "subscribe",
      value: function subscribe(callback) {
        var _this2 = this;
        this.beforeSubscription(this.moduleInstance);
        this.moduleInstance.currentProvider.subscribe(this.type, this.method, [this.options]).then(function (subscriptionId) {
          _this2.id = subscriptionId;
          _this2.moduleInstance.currentProvider.on(_this2.id, function (response) {
            var formattedOutput = _this2.onNewSubscriptionItem(response.result);
            _this2.emit('data', formattedOutput);
            if (isFunction(callback)) {
              callback(false, formattedOutput);
            }
          });
        }).catch(function (error) {
          _this2.emit('error', error);
          if (isFunction(callback)) {
            callback(error, null);
          }
        });
        return this;
      }
    }, {
      key: "unsubscribe",
      value: function unsubscribe(callback) {
        var _this3 = this;
        return this.moduleInstance.currentProvider.unsubscribe(this.id, this.type.slice(0, 3) + '_unsubscribe').then(function (response) {
          if (!response) {
            var error = new Error('Error on unsubscribe!');
            if (isFunction(callback)) {
              callback(error, null);
            }
            throw error;
          }
          _this3.id = null;
          _this3.removeAllListeners('data');
          if (isFunction(callback)) {
            callback(false, true);
          }
          return true;
        });
      }
    }]);
    return AbstractSubscription;
  }(EventEmitter);
  
  var LogSubscription =
  function (_AbstractSubscription) {
    _inherits(LogSubscription, _AbstractSubscription);
    function LogSubscription(options, utils, formatters, moduleInstance, getPastLogsMethod) {
      var _this;
      _classCallCheck(this, LogSubscription);
      _this = _possibleConstructorReturn(this, _getPrototypeOf(LogSubscription).call(this, 'eth_subscribe', 'logs', options, utils, formatters, moduleInstance));
      _this.getPastLogsMethod = getPastLogsMethod;
      return _this;
    }
    _createClass(LogSubscription, [{
      key: "subscribe",
      value: function subscribe(callback) {
        var _this2 = this;
        if (this.options.fromBlock && this.options.fromBlock !== 'latest' || this.options.fromBlock === 0) {
          this.getPastLogsMethod.parameters = [this.formatters.inputLogFormatter(this.options)];
          this.getPastLogsMethod.execute(this.moduleInstance).then(function (logs) {
            logs.forEach(function (log) {
              callback(false, log);
              _this2.emit('data', log);
            });
            delete _this2.options.fromBlock;
            _get(_getPrototypeOf(LogSubscription.prototype), "subscribe", _this2).call(_this2, callback);
          }).catch(function (error) {
            _this2.emit('error', error);
            callback(error, null);
          });
          return this;
        }
        _get(_getPrototypeOf(LogSubscription.prototype), "subscribe", this).call(this, callback);
        return this;
      }
    }, {
      key: "onNewSubscriptionItem",
      value: function onNewSubscriptionItem(subscriptionItem) {
        return this.formatters.outputLogFormatter(subscriptionItem);
      }
    }]);
    return LogSubscription;
  }(AbstractSubscription);
  
  var NewHeadsSubscription =
  function (_AbstractSubscription) {
    _inherits(NewHeadsSubscription, _AbstractSubscription);
    function NewHeadsSubscription(utils, formatters, moduleInstance) {
      _classCallCheck(this, NewHeadsSubscription);
      return _possibleConstructorReturn(this, _getPrototypeOf(NewHeadsSubscription).call(this, 'eth_subscribe', 'newHeads', null, utils, formatters, moduleInstance));
    }
    _createClass(NewHeadsSubscription, [{
      key: "onNewSubscriptionItem",
      value: function onNewSubscriptionItem(subscriptionItem) {
        return this.formatters.outputBlockFormatter(subscriptionItem);
      }
    }]);
    return NewHeadsSubscription;
  }(AbstractSubscription);
  
  var NewPendingTransactionsSubscription =
  function (_AbstractSubscription) {
    _inherits(NewPendingTransactionsSubscription, _AbstractSubscription);
    function NewPendingTransactionsSubscription(utils, formatters, moduleInstance) {
      _classCallCheck(this, NewPendingTransactionsSubscription);
      return _possibleConstructorReturn(this, _getPrototypeOf(NewPendingTransactionsSubscription).call(this, 'eth_subscribe', 'newPendingTransactions', null, utils, formatters, moduleInstance));
    }
    return NewPendingTransactionsSubscription;
  }(AbstractSubscription);
  
  var SyncingSubscription =
  function (_AbstractSubscription) {
    _inherits(SyncingSubscription, _AbstractSubscription);
    function SyncingSubscription(utils, formatters, moduleInstance) {
      var _this;
      _classCallCheck(this, SyncingSubscription);
      _this = _possibleConstructorReturn(this, _getPrototypeOf(SyncingSubscription).call(this, 'eth_subscribe', 'syncing', null, utils, formatters, moduleInstance));
      _this.isSyncing = null;
      return _this;
    }
    _createClass(SyncingSubscription, [{
      key: "onNewSubscriptionItem",
      value: function onNewSubscriptionItem(subscriptionItem) {
        var isSyncing = subscriptionItem.result.syncing;
        if (this.isSyncing === null) {
          this.isSyncing = isSyncing;
          this.emit('changed', this.isSyncing);
        }
        if (this.isSyncing === true && isSyncing === false) {
          this.isSyncing = isSyncing;
          this.emit('changed', this.isSyncing);
        }
        if (this.isSyncing === false && isSyncing === true) {
          this.isSyncing = isSyncing;
          this.emit('changed', this.isSyncing);
        }
        return this.formatters.outputSyncingFormatter(subscriptionItem);
      }
    }]);
    return SyncingSubscription;
  }(AbstractSubscription);
  
  var MessagesSubscription =
  function (_AbstractSubscription) {
    _inherits(MessagesSubscription, _AbstractSubscription);
    function MessagesSubscription(options, utils, formatters, moduleInstance) {
      _classCallCheck(this, MessagesSubscription);
      return _possibleConstructorReturn(this, _getPrototypeOf(MessagesSubscription).call(this, 'shh_subscribe', 'messages', options, utils, formatters, moduleInstance));
    }
    return MessagesSubscription;
  }(AbstractSubscription);
  
  var SubscriptionsFactory =
  function () {
    function SubscriptionsFactory(utils, formatters) {
      _classCallCheck(this, SubscriptionsFactory);
      this.utils = utils;
      this.formatters = formatters;
    }
    _createClass(SubscriptionsFactory, [{
      key: "createLogSubscription",
      value: function createLogSubscription(options, moduleInstance, getPastLogsMethod) {
        return new LogSubscription(cloneDeep(options), this.utils, this.formatters, moduleInstance, getPastLogsMethod);
      }
    }, {
      key: "createNewHeadsSubscription",
      value: function createNewHeadsSubscription(moduleInstance) {
        return new NewHeadsSubscription(this.utils, this.formatters, moduleInstance);
      }
    }, {
      key: "createNewPendingTransactionsSubscription",
      value: function createNewPendingTransactionsSubscription(moduleInstance) {
        return new NewPendingTransactionsSubscription(this.utils, this.formatters, moduleInstance);
      }
    }, {
      key: "createSyncingSubscription",
      value: function createSyncingSubscription(moduleInstance) {
        return new SyncingSubscription(this.utils, this.formatters, moduleInstance);
      }
    }, {
      key: "createShhMessagesSubscription",
      value: function createShhMessagesSubscription(options, moduleInstance) {
        return new MessagesSubscription(options, this.utils, this.formatters, moduleInstance);
      }
    }]);
    return SubscriptionsFactory;
  }();
  
  var SubscriptionsModuleFactory =
  function () {
    function SubscriptionsModuleFactory() {
      _classCallCheck(this, SubscriptionsModuleFactory);
    }
    _createClass(SubscriptionsModuleFactory, [{
      key: "createSubscriptionsFactory",
      value: function createSubscriptionsFactory(utils, formatters) {
        return new SubscriptionsFactory(utils, formatters);
      }
    }]);
    return SubscriptionsModuleFactory;
  }();
  
  var SubscriptionsFactory$1 = function SubscriptionsFactory() {
    return new SubscriptionsModuleFactory().createSubscriptionsFactory(Utils, web3CoreHelpers.formatters);
  };
  
  exports.SubscriptionsFactory = SubscriptionsFactory$1;
  exports.LogSubscription = LogSubscription;
  exports.AbstractSubscription = AbstractSubscription;
  