"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ExperienceCheckTimeout = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _consts = require("./consts");
var DEFAULT_FAILURE_RESULT = {
  status: 'failure',
  reason: _consts.EXPERIENCE_FAILURE_REASON.TIMEOUT
};

/**
 * Check for the completion of an experience based on a timeout
 *
 * By default, will result in failure with reason 'timeout' after the specified duration.
 *
 * Can be customized for different results on timeout via the onTimeout callback.
 */
var ExperienceCheckTimeout = exports.ExperienceCheckTimeout = /*#__PURE__*/function () {
  function ExperienceCheckTimeout(_ref) {
    var durationMs = _ref.durationMs,
      _ref$onTimeout = _ref.onTimeout,
      onTimeout = _ref$onTimeout === void 0 ? function () {
        return DEFAULT_FAILURE_RESULT;
      } : _ref$onTimeout;
    (0, _classCallCheck2.default)(this, ExperienceCheckTimeout);
    (0, _defineProperty2.default)(this, "durationMs", 0);
    this.durationMs = durationMs;
    this.onTimeout = onTimeout;
  }
  return (0, _createClass2.default)(ExperienceCheckTimeout, [{
    key: "start",
    value: function start(callback) {
      var _this = this;
      this.stop();
      this.timeoutId = setTimeout(function () {
        var result = _this.onTimeout() || DEFAULT_FAILURE_RESULT;
        callback(result);
      }, this.durationMs);
    }
  }, {
    key: "stop",
    value: function stop() {
      if (this.timeoutId) {
        clearTimeout(this.timeoutId);
        this.timeoutId = undefined;
      }
    }
  }]);
}();