"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useSmartLinkCrossProductUrlWrapperGated = exports.getIsFirstPartyLink = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = require("react");
var _useCrossProductUrlWrapper = require("@atlaskit/analytics-cross-product/useCrossProductUrlWrapper");
var _linkProvider = require("@atlaskit/link-provider");
var _platformFeatureFlagsReact = require("@atlaskit/platform-feature-flags-react");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var SMART_LINKS_XPC_BRIDGE = 'smartLinks';
var XPC_QUERY_PARAM = 'xpis';
var getIsFirstPartyLink = exports.getIsFirstPartyLink = function getIsFirstPartyLink(details) {
  var _is1PLink, _details$meta;
  return ((_is1PLink = details === null || details === void 0 || (_details$meta = details.meta) === null || _details$meta === void 0 ? void 0 : _details$meta.is1PLink) !== null && _is1PLink !== void 0 ? _is1PLink : false) === true;
};
var identityUrlWrapper = function identityUrlWrapper(url) {
  return url;
};
var useSmartLinkCrossProductUrlWrapperFallback = function useSmartLinkCrossProductUrlWrapperFallback(_args) {
  return identityUrlWrapper;
};
var useSmartLinkCrossProductUrlWrapper = function useSmartLinkCrossProductUrlWrapper(_ref) {
  var _effectiveProduct$toL;
  var details = _ref.details;
  var _useSmartLinkContext = (0, _linkProvider.useSmartLinkContext)(),
    product = _useSmartLinkContext.product,
    bridgeProduct = _useSmartLinkContext.bridgeProduct,
    xpcProduct = _useSmartLinkContext.xpcProduct,
    xpcSubProduct = _useSmartLinkContext.xpcSubProduct;
  // xpcProduct takes precedence over product — it identifies the host product for XPC analytics
  // without affecting link resolution (which uses the `product` prop separately).
  var effectiveProduct = xpcProduct !== null && xpcProduct !== void 0 ? xpcProduct : product;
  var wrapUrl = (0, _useCrossProductUrlWrapper.useCrossProductUrlWrapper)(_objectSpread({
    bridge: bridgeProduct !== null && bridgeProduct !== void 0 ? bridgeProduct : SMART_LINKS_XPC_BRIDGE,
    product: (_effectiveProduct$toL = effectiveProduct === null || effectiveProduct === void 0 ? void 0 : effectiveProduct.toLowerCase()) !== null && _effectiveProduct$toL !== void 0 ? _effectiveProduct$toL : 'unknown'
  }, xpcSubProduct ? {
    subProduct: xpcSubProduct
  } : {}));
  return (0, _react.useCallback)(function (url) {
    if (!getIsFirstPartyLink(details) || !effectiveProduct) {
      return url;
    }
    var parsedUrl;
    try {
      parsedUrl = new URL(url);
    } catch (_unused) {
      return url;
    }
    if (parsedUrl.searchParams.has(XPC_QUERY_PARAM)) {
      return url;
    }
    return wrapUrl(url);
  }, [details, effectiveProduct, wrapUrl]);
};
var useSmartLinkCrossProductUrlWrapperGated = exports.useSmartLinkCrossProductUrlWrapperGated = (0, _platformFeatureFlagsReact.functionWithFG)('platform_smartlink_xpc_url_wrapping', useSmartLinkCrossProductUrlWrapper, useSmartLinkCrossProductUrlWrapperFallback);