"use strict";

var _Reflect$construct = require("@babel/runtime-corejs2/core-js/reflect/construct");
var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");
var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
_Object$defineProperty(exports, "__esModule", {
  value: true
});
exports.UnknownResponse = exports.NotAuthorized = exports.Forbidden = exports.ConnectionError = exports.BadRequest = void 0;
exports.createTimeoutError = createTimeoutError;
exports.createWssResponseError = createWssResponseError;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/defineProperty"));
var _common = require("@webex/common");
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? _Reflect$construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(_Reflect$construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } /*!
 * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
 */ // @ts-expect-error `@webex/common` is still JS-only and does not ship declarations.
/**
 * Exception thrown when a websocket gets closed.
 */
var ConnectionError = exports.ConnectionError = /*#__PURE__*/function (_Exception) {
  function ConnectionError() {
    var _this;
    var event = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    (0, _classCallCheck2.default)(this, ConnectionError);
    _this = _callSuper(this, ConnectionError, [event]);
    (0, _defineProperty2.default)(_this, "code", void 0);
    (0, _defineProperty2.default)(_this, "reason", void 0);
    _this.code = event.code;
    _this.reason = event.reason;
    return _this;
  }

  /**
   * Parses a close event and sets the code and reason properties.
   * @param event - Socket close event to parse
   * @returns The reason string from the event
   */
  (0, _inherits2.default)(ConnectionError, _Exception);
  return (0, _createClass2.default)(ConnectionError, [{
    key: "parse",
    value: function parse() {
      var event = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      return event.reason;
    }
  }]);
}(_common.Exception);
/**
 * Thrown for CloseCode 1005.
 * UnknownResponse is produced by IE when we receive a 4XXX close code.
 * This should typically be treated like a NotFound error.
 */
(0, _defineProperty2.default)(ConnectionError, "defaultMessage", 'Failed to connect to socket');
var UnknownResponse = exports.UnknownResponse = /*#__PURE__*/function (_ConnectionError2) {
  // eslint-disable-next-line no-useless-constructor
  function UnknownResponse(event) {
    (0, _classCallCheck2.default)(this, UnknownResponse);
    return _callSuper(this, UnknownResponse, [event]);
  }
  (0, _inherits2.default)(UnknownResponse, _ConnectionError2);
  return (0, _createClass2.default)(UnknownResponse);
}(ConnectionError);
/**
 * Thrown for CloseCode 4400.
 * BadRequest usually implies an attempt to use service account credentials.
 */
(0, _defineProperty2.default)(UnknownResponse, "defaultMessage", 'UnknownResponse is produced by IE when we receive a 4XXX. You probably want to treat this like a NotFound');
var BadRequest = exports.BadRequest = /*#__PURE__*/function (_ConnectionError3) {
  // eslint-disable-next-line no-useless-constructor
  function BadRequest(event) {
    (0, _classCallCheck2.default)(this, BadRequest);
    return _callSuper(this, BadRequest, [event]);
  }
  (0, _inherits2.default)(BadRequest, _ConnectionError3);
  return (0, _createClass2.default)(BadRequest);
}(ConnectionError);
/**
 * Thrown for CloseCode 4401.
 * Indicates an authorization failure requiring a token refresh.
 */
(0, _defineProperty2.default)(BadRequest, "defaultMessage", 'BadRequest usually implies an attempt to use service account credentials');
var NotAuthorized = exports.NotAuthorized = /*#__PURE__*/function (_ConnectionError4) {
  // eslint-disable-next-line no-useless-constructor
  function NotAuthorized(event) {
    (0, _classCallCheck2.default)(this, NotAuthorized);
    return _callSuper(this, NotAuthorized, [event]);
  }
  (0, _inherits2.default)(NotAuthorized, _ConnectionError4);
  return (0, _createClass2.default)(NotAuthorized);
}(ConnectionError);
/**
 * Thrown for CloseCode 4403.
 * Forbidden usually implies the credentials are not entitled for Webex.
 */
(0, _defineProperty2.default)(NotAuthorized, "defaultMessage", 'Please refresh your access token');
var Forbidden = exports.Forbidden = /*#__PURE__*/function (_ConnectionError5) {
  // eslint-disable-next-line no-useless-constructor
  function Forbidden(event) {
    (0, _classCallCheck2.default)(this, Forbidden);
    return _callSuper(this, Forbidden, [event]);
  }
  (0, _inherits2.default)(Forbidden, _ConnectionError5);
  return (0, _createClass2.default)(Forbidden);
}(ConnectionError);
/**
 * Creates a MobiusSocketResponseError from a socket response.
 *
 * @param response - The socket response that triggered the error
 * @param statusCode - HTTP-style status code for the error
 * @param statusMessage - Human-readable status message
 * @returns A formatted error object with response details
 */
(0, _defineProperty2.default)(Forbidden, "defaultMessage", 'Forbidden usually implies these credentials are not entitled for Webex');
function createWssResponseError(response, statusCode, statusMessage) {
  var error = new Error(statusMessage || "Mobius websocket request failed with status ".concat(statusCode || 'unknown'));
  error.name = 'MobiusSocketResponseError';
  error.statusCode = statusCode;
  error.statusMessage = statusMessage;
  error.response = response;
  error.trackingId = response === null || response === void 0 ? void 0 : response.trackingId;
  return error;
}

/**
 * Creates a timeout error for a socket request that didn't receive a response.
 *
 * @param request - The socket request that timed out
 * @returns A timeout error with status code 408
 */
function createTimeoutError(request) {
  var errorPayload = {
    type: 'response_event',
    subtype: request.type,
    trackingId: request.trackingId
  };
  return createWssResponseError(errorPayload, 408, 'Mobius websocket response timed out');
}
//# sourceMappingURL=errors.js.map
