'use strict';Object.defineProperty(exports, "__esModule", { value: true });















var _eventemitter = require('eventemitter3');var _eventemitter2 = _interopRequireDefault(_eventemitter);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}

var defaults = {
  from: 0,
  to: 'latest',
  interval: 5000,
  filter: function filter() {return true;} }; // 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 subscribeToEvents = function subscribeToEvents(contract, events) {var opt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};var api = contract.api;opt = Object.assign({}, defaults, opt);var filter = null;var interval = null;var unsubscribe = function unsubscribe() {if (filter) {
      filter.
      then(function (filterId) {
        return api.eth.uninstallFilter(filterId);
      }).
      catch(function (err) {
        emitter.emit('error', err);
      });
      filter = null;
    }
    if (interval) {
      clearInterval(interval);
      interval = null;
    }
  };

  var emitter = new _eventemitter2.default();

  emitter.unsubscribe = unsubscribe;

  var fetcher = function fetcher(method, filterId) {return function () {
      api.
      eth[method](filterId).
      then(function (logs) {
        logs = contract.parseEventLogs(logs);var _iteratorNormalCompletion = true;var _didIteratorError = false;var _iteratorError = undefined;try {

          for (var _iterator = logs[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {var log = _step.value;
            if (opt.filter(log)) {
              emitter.emit('log', log);
              emitter.emit(log.event, log);
            }
          }} catch (err) {_didIteratorError = true;_iteratorError = err;} finally {try {if (!_iteratorNormalCompletion && _iterator.return) {_iterator.return();}} finally {if (_didIteratorError) {throw _iteratorError;}}}
      }).
      catch(function (err) {
        emitter.emit('error', err);
      });
    };};

  var signatures = events.
  filter(function (event) {return contract.instance[event];}).
  map(function (event) {return contract.instance[event].signature;});

  filter = api.eth.
  newFilter({
    fromBlock: opt.from,
    toBlock: opt.to,
    address: contract.address,
    topics: [signatures] }).

  then(function (filterId) {
    fetcher('getFilterLogs', filterId)(); // fetch immediately

    var fetchChanges = fetcher('getFilterChanges', filterId);

    interval = setInterval(fetchChanges, opt.interval);

    return filterId;
  }).
  catch(function (err) {
    emitter.emit('error', err);
    throw err; // reject Promise
  });

  return emitter;
};exports.default =

subscribeToEvents;