'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.StatsStore = 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;

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _mobx = require('mobx');

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 defaults = {
  numPassed: 0,
  numFailed: 0,
  numPending: 0,

  _startTime: null,
  _currentTime: null
};

var StatsStore = (_class = function () {
  function StatsStore() {
    _classCallCheck(this, StatsStore);

    _initDefineProp(this, 'numPassed', _descriptor, this);

    _initDefineProp(this, 'numFailed', _descriptor2, this);

    _initDefineProp(this, 'numPending', _descriptor3, this);

    _initDefineProp(this, '_startTime', _descriptor4, this);

    _initDefineProp(this, '_currentTime', _descriptor5, this);
  }

  _createClass(StatsStore, [{
    key: 'start',
    value: function start(_ref) {
      var startTime = _ref.startTime;
      var _ref$numPassed = _ref.numPassed;
      var numPassed = _ref$numPassed === undefined ? 0 : _ref$numPassed;
      var _ref$numFailed = _ref.numFailed;
      var numFailed = _ref$numFailed === undefined ? 0 : _ref$numFailed;
      var _ref$numPending = _ref.numPending;
      var numPending = _ref$numPending === undefined ? 0 : _ref$numPending;

      if (this._startTime) return;

      this.numPassed = numPassed;
      this.numFailed = numFailed;
      this.numPending = numPending;

      this._startTime = new Date(startTime).getTime();
      this._updateCurrentTime();

      this._startTimer();
    }
  }, {
    key: '_startTimer',
    value: function _startTimer() {
      this._interval = setInterval((0, _mobx.action)('duration:interval', this._updateCurrentTime.bind(this)), 100);
    }
  }, {
    key: '_stopTimer',
    value: function _stopTimer() {
      clearInterval(this._interval);
    }
  }, {
    key: '_updateCurrentTime',
    value: function _updateCurrentTime() {
      this._currentTime = Date.now();
    }
  }, {
    key: 'incrementCount',
    value: function incrementCount(type) {
      var countKey = 'num' + _lodash2.default.capitalize(type);
      this[countKey] = this[countKey] + 1;
    }
  }, {
    key: 'pause',
    value: function pause() {
      this._stopTimer();
    }
  }, {
    key: 'resume',
    value: function resume() {
      this._startTimer();
    }
  }, {
    key: 'stop',
    value: function stop() {
      this._stopTimer();
      this._updateCurrentTime();
    }
  }, {
    key: 'reset',
    value: function reset() {
      var _this = this;

      this._stopTimer();
      _lodash2.default.each(defaults, function (value, key) {
        _this[key] = value;
      });
    }
  }, {
    key: 'duration',
    get: function get() {
      if (!this._startTime) return 0;

      return this._currentTime - this._startTime;
    }
  }]);

  return StatsStore;
}(), (_descriptor = _applyDecoratedDescriptor(_class.prototype, 'numPassed', [_mobx.observable], {
  enumerable: true,
  initializer: function initializer() {
    return defaults.numPassed;
  }
}), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, 'numFailed', [_mobx.observable], {
  enumerable: true,
  initializer: function initializer() {
    return defaults.numFailed;
  }
}), _descriptor3 = _applyDecoratedDescriptor(_class.prototype, 'numPending', [_mobx.observable], {
  enumerable: true,
  initializer: function initializer() {
    return defaults.numPending;
  }
}), _descriptor4 = _applyDecoratedDescriptor(_class.prototype, '_startTime', [_mobx.observable], {
  enumerable: true,
  initializer: function initializer() {
    return defaults._startTime;
  }
}), _descriptor5 = _applyDecoratedDescriptor(_class.prototype, '_currentTime', [_mobx.observable], {
  enumerable: true,
  initializer: function initializer() {
    return defaults._startTime;
  }
}), _applyDecoratedDescriptor(_class.prototype, 'duration', [_mobx.computed], Object.getOwnPropertyDescriptor(_class.prototype, 'duration'), _class.prototype)), _class);
exports.StatsStore = StatsStore;
exports.default = new StatsStore();