import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import { EXPERIENCE_FAILURE_REASON } from './consts';
var DEFAULT_FAILURE_RESULT = {
  status: 'failure',
  reason: 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.
 */
export var 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;
    _classCallCheck(this, ExperienceCheckTimeout);
    _defineProperty(this, "durationMs", 0);
    this.durationMs = durationMs;
    this.onTimeout = onTimeout;
  }
  return _createClass(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;
      }
    }
  }]);
}();