"use strict";

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.deriveMobiusSocketMessageType = deriveMobiusSocketMessageType;
exports.isSupplementaryServiceMessageType = void 0;
var _types = require("../../common/types");
var _Logger = _interopRequireDefault(require("../../Logger"));
var _constants = require("../constants");
var _constants2 = require("./constants");
/**
 * Derives the Mobius Socket message type from the URI and HTTP method.
 * Uses both the URI path pattern and the HTTP method to disambiguate operations
 * that share the same endpoint (e.g., PATCH vs DELETE on calls/{callId}).
 *
 * @param uri - The request URI
 * @param httpMethodType - The HTTP method used for the request
 * @returns The socket message type based on the URI pattern and HTTP method
 *
 * @example
 * deriveMobiusSocketMessageType('/api/v1/calling/web/device', HTTP_METHODS.POST)
 * // returns 'register'
 *
 * deriveMobiusSocketMessageType('/api/v1/calling/web/devices/abc123/calls/xyz789', HTTP_METHODS.PATCH)
 * // returns 'call_state'
 *
 * deriveMobiusSocketMessageType('/api/v1/calling/web/devices/abc123/calls/xyz789', HTTP_METHODS.DELETE)
 * // returns 'call_delete'
 */
// eslint-disable-next-line import/prefer-default-export
function deriveMobiusSocketMessageType(uri, httpMethodType) {
  var logContext = {
    file: _constants.MOBIUS_SOCKET_MAPPER_FILE,
    method: _constants.METHODS.DERIVE_MOBIUS_SOCKET_MESSAGE_TYPE
  };
  if (!uri) {
    _Logger.default.warn('Cannot derive Mobius socket message type: uri is empty', logContext);
    return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.UNKNOWN;
  }

  // --- Supplementary services: .../services/{service}/{action} ---
  if (uri.includes('/services')) {
    if (uri.includes('/callhold/hold')) {
      return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.CALL_HOLD;
    }
    if (uri.includes('/callhold/resume')) {
      return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.CALL_RESUME;
    }
    if (uri.includes('/calltransfer/commit')) {
      return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.CALL_TRANSFER;
    }
    _Logger.default.warn("Unrecognized supplementary service uri - uri: ".concat(uri, ", httpMethod: ").concat(httpMethodType), logContext);
    return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.UNKNOWN;
  }

  // --- Call sub-resource actions (checked before bare calls/{callId}) ---

  // Call media: .../calls/{callId}/media
  if (uri.includes('/calls/') && uri.includes('/media')) {
    return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.CALL_MEDIA;
  }

  // Call status: .../calls/{callId}/status
  if (uri.includes('/calls/') && uri.includes('/status')) {
    return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.CALL_STATUS;
  }

  // Call state or delete: .../calls/{callId}  (PATCH → state, DELETE → delete)
  if (uri.match(/\/calls\/[^/]+$/)) {
    if (httpMethodType === _types.HTTP_METHODS.PATCH) {
      return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.CALL_STATE;
    }
    if (httpMethodType === _types.HTTP_METHODS.DELETE) {
      return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.CALL_DELETE;
    }
    _Logger.default.warn("Unrecognized httpMethod for calls/{callId} - uri: ".concat(uri, ", httpMethod: ").concat(httpMethodType), logContext);
    return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.UNKNOWN;
  }

  // Call setup: .../devices/{deviceId}/call  (singular)
  if (uri.endsWith('/call')) {
    return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.CALL_SETUP;
  }

  // --- Device-level operations ---

  // Device keepalive: .../devices/{deviceId}/status  (no /calls/ in path)
  if (!uri.includes('/calls/') && uri.endsWith('/status')) {
    return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.DEVICE_STATUS;
  }

  // Device registration: .../calling/web/device  (singular)
  if (uri.endsWith('/device')) {
    return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.REGISTER;
  }

  // Device unregister or get: .../devices/{deviceId}  (DELETE → unregister, GET → device_get)
  if (uri.match(/\/devices\/[^/?]+$/) && !uri.includes('/calls')) {
    if (httpMethodType === _types.HTTP_METHODS.DELETE) {
      return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.UNREGISTER;
    }
    if (httpMethodType === _types.HTTP_METHODS.GET) {
      return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.DEVICE_GET;
    }
    _Logger.default.warn("Unrecognized httpMethod for devices/{deviceId} - uri: ".concat(uri, ", httpMethod: ").concat(httpMethodType), logContext);
    return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.UNKNOWN;
  }

  // Device list: .../devices  or  .../devices?userid=...
  if (uri.includes('/devices') && !uri.match(/\/devices\/[^/?]+/)) {
    return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.DEVICE_LIST;
  }
  _Logger.default.warn("Unrecognized uri pattern for Mobius socket - uri: ".concat(uri, ", httpMethod: ").concat(httpMethodType), logContext);
  return _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.UNKNOWN;
}
var isSupplementaryServiceMessageType = exports.isSupplementaryServiceMessageType = function isSupplementaryServiceMessageType(type) {
  return [_constants2.MOBIUS_SOCKET_MESSAGE_TYPE.CALL_HOLD, _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.CALL_RESUME, _constants2.MOBIUS_SOCKET_MESSAGE_TYPE.CALL_TRANSFER].includes(type);
};
//# sourceMappingURL=mobiusSocketMapper.js.map
