"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.WEBRTC_CALLING_OVER_WS_FEATURE_KEY = void 0;
exports.isMobiusWssEnabled = isMobiusWssEnabled;
var _Logger = _interopRequireDefault(require("../../Logger"));
var _constants = require("../constants");
/** WDM device-settings key for routing Mobius traffic over WebSocket. */
var WEBRTC_CALLING_OVER_WS_FEATURE_KEY = exports.WEBRTC_CALLING_OVER_WS_FEATURE_KEY = 'webrtc-calling-over-ws-CALL-219562';

/** Allowed origins for samples page localStorage override */
var ALLOWED_ORIGINS = ['localhost', '127.0.0.1', 'web-sdk.webex.com'];

/**
 * Returns tri-state localStorage override: true (force-enable), false (force-disable), or null (defer to backend).
 */
function samplesPageToggleValue() {
  if (typeof window === 'undefined' || !window.localStorage) {
    return null;
  }
  var hostname = window.location.hostname;
  var isAllowedOrigin = ALLOWED_ORIGINS.some(function (origin) {
    return hostname === origin || hostname.endsWith(".".concat(origin));
  });
  if (!isAllowedOrigin) {
    return null;
  }
  var localStorageValue = localStorage.getItem('mobius-wss-enabled');
  if (localStorageValue === 'true') {
    _Logger.default.trace("Mobius WSS force-enabled via samples page localStorage override on ".concat(hostname), {
      file: _constants.WS_FEATURE_FLAG_FILE,
      method: _constants.METHODS.IS_MOBIUS_WSS_ENABLED
    });
    return true;
  }
  if (localStorageValue === 'false') {
    _Logger.default.trace("Mobius WSS force-disabled via samples page localStorage override on ".concat(hostname), {
      file: _constants.WS_FEATURE_FLAG_FILE,
      method: _constants.METHODS.IS_MOBIUS_WSS_ENABLED
    });
    return false;
  }
  return null;
}

/**
 * Whether Webex Calling should use the Mobius WebSocket transport for API requests.
 * Reads WDM `webex.internal.device.settings['webrtc-calling-over-ws'].value`; must be
 * strictly `true` to enable WebSocket—otherwise use HTTP.
 *
 * Additionally checks browser localStorage for 'mobius-wss-enabled' flag on allowed origins
 * (localhost, 127.0.0.1, web-sdk.webex.com) to enable testing via samples page.
 * localStorage can force-enable ('true'), force-disable ('false'), or defer to backend (null/unset).
 */
function isMobiusWssEnabled(webex) {
  var _webex$internal, _webex$internal$devic, _webex$internal$devic2, _webex$internal$devic3, _webex$internal$devic4;
  var enabled = ((_webex$internal = webex.internal) === null || _webex$internal === void 0 ? void 0 : (_webex$internal$devic = _webex$internal.device) === null || _webex$internal$devic === void 0 ? void 0 : (_webex$internal$devic2 = _webex$internal$devic.features) === null || _webex$internal$devic2 === void 0 ? void 0 : (_webex$internal$devic3 = _webex$internal$devic2.developer) === null || _webex$internal$devic3 === void 0 ? void 0 : (_webex$internal$devic4 = _webex$internal$devic3.get(WEBRTC_CALLING_OVER_WS_FEATURE_KEY)) === null || _webex$internal$devic4 === void 0 ? void 0 : _webex$internal$devic4.value) === true;
  var localStorageOverride = samplesPageToggleValue();
  var finalValue = localStorageOverride !== null ? localStorageOverride : enabled;
  _Logger.default.trace("Mobius WSS feature flag '".concat(WEBRTC_CALLING_OVER_WS_FEATURE_KEY, "' resolved to: ").concat(finalValue, " (backend: ").concat(enabled, ", localStorage: ").concat(localStorageOverride, ")"), {
    file: _constants.WS_FEATURE_FLAG_FILE,
    method: _constants.METHODS.IS_MOBIUS_WSS_ENABLED
  });
  return finalValue;
}
//# sourceMappingURL=wsFeatureFlag.js.map
