"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var TimeRecordingService =
/*#__PURE__*/
function () {
  function TimeRecordingService(recordQuizTime, updateTimerInfo) {
    var _this = this;

    (0, _classCallCheck2.default)(this, TimeRecordingService);

    this.__performApiCall = function () {
      _this._recordQuizTime(function () {
        var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

        if (_this._stopped) {
          return;
        }

        _this._updateTimerInfo({
          timeRecording: data.timeRecording
        });
      });
    };

    this._recordQuizTime = recordQuizTime;
    this._updateTimerInfo = updateTimerInfo;
    this._stopped = false;
    this._apiInterval = null;
    this._apiTimeIncrement = 4000;
  }

  (0, _createClass2.default)(TimeRecordingService, [{
    key: "start",
    value: function start() {
      this.__performApiCall();

      this.__startApiInterval();

      return this;
    }
  }, {
    key: "stop",
    value: function stop() {
      this._stopped = true;

      this.__stopApiInterval();

      return this;
    }
  }, {
    key: "__ensureNotStopped",
    value: function __ensureNotStopped() {
      if (this._stopped) {
        throw new Error('An attempt was made to restart a stopped timer. This is disallowed ' + 'to prevent tricky race conditions. Create a new TimeRecordingService object instead.');
      }
    }
  }, {
    key: "__startApiInterval",
    value: function __startApiInterval() {
      this.__ensureNotStopped();

      this._apiInterval = setInterval(this.__performApiCall, this._apiTimeIncrement);
    }
  }, {
    key: "__stopApiInterval",
    value: function __stopApiInterval() {
      clearInterval(this._apiInterval);
      this._apiInterval = null;
    }
  }]);
  return TimeRecordingService;
}();

exports.default = TimeRecordingService;