'use strict';

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

var _events = require('events');

var _mobx = require('mobx');

var localBus = new _events.EventEmitter();

exports.default = {
  init: function init(_ref) {
    var appState = _ref.appState;
    var runnablesStore = _ref.runnablesStore;
    var statsStore = _ref.statsStore;
    var scroller = _ref.scroller;

    this.appState = appState;
    this.runnablesStore = runnablesStore;
    this.statsStore = statsStore;
    this.scroller = scroller;
  },
  listen: function listen(runner) {
    var appState = this.appState;
    var runnablesStore = this.runnablesStore;
    var scroller = this.scroller;
    var statsStore = this.statsStore;


    runner.on('runnables:ready', (0, _mobx.action)('runnables:ready', function () {
      var rootRunnable = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

      runnablesStore.setRunnables(rootRunnable);
    }));

    runner.on('reporter:log:add', (0, _mobx.action)('log:add', function (log) {
      runnablesStore.addLog(log);
    }));

    runner.on('reporter:log:state:changed', (0, _mobx.action)('log:update', function (log) {
      runnablesStore.updateLog(log);
    }));

    runner.on('reporter:restart:test:run', (0, _mobx.action)('restart:test:run', function () {
      appState.reset();
      runnablesStore.reset();
      statsStore.reset();
      runner.emit('reporter:restarted');
    }));

    runner.on('run:start', (0, _mobx.action)('run:start', function () {
      if (runnablesStore.hasTests) {
        appState.startRunning();
      }
    }));

    runner.on('reporter:start', (0, _mobx.action)('start', function (startInfo) {
      appState.setAutoScrolling(startInfo.autoScrollingEnabled);
      runnablesStore.setInitialScrollTop(startInfo.scrollTop);
      if (runnablesStore.hasTests) {
        statsStore.start(startInfo);
      }
    }));

    runner.on('test:before:run', (0, _mobx.action)('test:before:run', function (runnable) {
      runnablesStore.runnableStarted(runnable);
    }));

    runner.on('test:after:run', (0, _mobx.action)('test:after:run', function (runnable) {
      runnablesStore.runnableFinished(runnable);
      statsStore.incrementCount(runnable.state);
    }));

    runner.on('paused', (0, _mobx.action)('paused', function (nextCommandName) {
      appState.pause(nextCommandName);
      statsStore.pause();
    }));

    runner.on('run:end', (0, _mobx.action)('run:end', function () {
      appState.stop();
      statsStore.stop();
    }));

    runner.on('reporter:collect:run:state', function (cb) {
      cb({
        autoScrollingEnabled: appState.autoScrollingEnabled,
        scrollTop: scroller.getScrollTop()
      });
    });

    localBus.on('resume', (0, _mobx.action)('resume', function () {
      appState.resume();
      statsStore.resume();
      runner.emit('runner:resume');
    }));

    localBus.on('next', (0, _mobx.action)('next', function () {
      runner.emit('runner:next');
    }));

    localBus.on('stop', (0, _mobx.action)('stop', function () {
      runner.emit('runner:abort');
    }));

    localBus.on('restart', (0, _mobx.action)('restart', function () {
      runner.emit('runner:restart');
    }));

    localBus.on('show:command', function (commandId) {
      runner.emit('runner:console:log', commandId);
    });

    localBus.on('show:error', function (testId) {
      var test = runnablesStore.testById(testId);
      if (test.err.isCommandErr) {
        var command = test.commandMatchingErr();
        if (!command) return;
        runner.emit('runner:console:log', command.id);
      } else {
        runner.emit('runner:console:error', testId);
      }
    });

    localBus.on('show:snapshot', function (commandId) {
      runner.emit('runner:show:snapshot', commandId);
    });

    localBus.on('hide:snapshot', function (commandId) {
      runner.emit('runner:hide:snapshot', commandId);
    });

    localBus.on('focus:tests', function () {
      runner.emit('focus:tests');
    });

    localBus.on('persist:state', function () {
      runner.emit('persist:state', {
        autoScrollingEnabled: appState.autoScrollingEnabled
      });
    });
  },
  emit: function emit(event) {
    for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
      args[_key - 1] = arguments[_key];
    }

    localBus.emit.apply(localBus, [event].concat(args));
  },


  // for testing purposes
  __off: function __off() {
    localBus.removeAllListeners();
  }
};
module.exports = exports['default'];