/* index.tsx generated by @compiled/babel-plugin v0.39.1 */
/* eslint-disable @atlaskit/design-system/consistent-css-prop-usage */
/* eslint-disable @atlaskit/ui-styling-standard/no-unsafe-values */
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getFlexibleUiBlock = exports.getChildrenOptions = exports.default = void 0;
require("./index.compiled.css");
var _runtime = require("@compiled/react/runtime");
var _react = _interopRequireDefault(require("react"));
var _reactMagneticDi = require("react-magnetic-di");
var _constants = require("../../../../constants");
var _flexibleUiContext = require("../../../../state/flexible-ui-context");
var _utils = require("../../../../state/flexible-ui-context/utils");
var _flexible = require("../../../../utils/flexible");
var _blocks = require("../blocks");
var _hoverCardControl = _interopRequireDefault(require("./hover-card-control"));
var _layeredLink = _interopRequireDefault(require("./layered-link"));
var getChildrenOptions = exports.getChildrenOptions = function getChildrenOptions(children, context) {
  var options = {};
  if ((0, _utils.isFlexUiPreviewPresent)(context)) {
    _react.default.Children.map(children, function (child) {
      if ( /*#__PURE__*/_react.default.isValidElement(child)) {
        if ((0, _flexible.isFlexibleUiPreviewBlock)(child)) {
          var placement = child.props.placement;
          if (placement === _constants.MediaPlacement.Left) {
            options.previewOnLeft = true;
          }
          if (placement === _constants.MediaPlacement.Right) {
            options.previewOnRight = true;
          }
        }
      }
    });
  }
  return options;
};
var filterChildren = function filterChildren(children, removeBlockRestriction) {
  if (removeBlockRestriction) {
    return children;
  }
  return _react.default.Children.map(children, function (child) {
    return /*#__PURE__*/_react.default.isValidElement(child) && (0, _flexible.isFlexibleUiBlock)(child) ? child : undefined;
  });
};

/**
 * Note: This function is only necessary for CompiledCSS within Jest tests due to the way it handles Styles.
 * CompiledCSS will inject a StyleCacheProvider around the component tree, which
 * causes the children to be wrapped in a StyleCacheProvider as well. This function recursively
 * searches for the first valid TitleBlock within the children of the StyleCacheProvider.
 */
var _getFlexibleUiBlock = exports.getFlexibleUiBlock = function getFlexibleUiBlock(node) {
  if (! /*#__PURE__*/_react.default.isValidElement(node)) {
    return undefined;
  }
  if (node.type === _blocks.TitleBlock) {
    return node;
  }
  if ((0, _flexible.isStyleCacheProvider)(node)) {
    // Component wrapped with compiled at runtime, check for children
    var isChildrenValid;
    _react.default.Children.map(node.props.children, function (child) {
      var _child$type;
      if (typeof child.type !== 'string' && ((_child$type = child.type) === null || _child$type === void 0 ? void 0 : _child$type.name) !== 'Style') {
        isChildrenValid = _getFlexibleUiBlock(child);
      }
    });
    return isChildrenValid;
  }
  return undefined;
};
var getTitleBlockProps = function getTitleBlockProps(children) {
  var block = _react.default.Children.toArray(children).map(function (child) {
    return _getFlexibleUiBlock(child);
  }).filter(function (x) {
    return x !== undefined;
  }).at(0);
  if ( /*#__PURE__*/_react.default.isValidElement(block)) {
    return block.props;
  }
};
var getLayeredLink = function getLayeredLink(testId, context, children, onClick, onAuxClick, onContextMenu, title) {
  var _ref = context || {},
    linkTitle = _ref.linkTitle,
    _ref$url = _ref.url,
    url = _ref$url === void 0 ? '' : _ref$url;
  // SSR cannot reliably extract TitleBlock props from children, so `title` is
  // provided from CardSSR as a stable server/client value to prevent hydration
  // text mismatches for the clickable container link.
  var _ref2 = getTitleBlockProps(children) || {},
    target = _ref2.anchorTarget,
    text = _ref2.text;
  return /*#__PURE__*/_react.default.createElement(_layeredLink.default, {
    onClick: onClick,
    onAuxClick: onAuxClick,
    onContextMenu: onContextMenu,
    target: target,
    testId: testId,
    text: title || text || (linkTitle === null || linkTitle === void 0 ? void 0 : linkTitle.text),
    url: url
  });
};
var baseStyleCommon = null;
var backgroundStyles = null;
var elevationStyles = null;
var clickableContainerStyles = null;
var gapStyleMap = {
  xlarge: "_zulpr2fv",
  large: "_zulpzu5n",
  medium: "_zulp1bpf",
  small: "_zulpswc3"
};

/**
 * Get container padding based on smart link size
 * Equivalent version for DS primitives space token is getPrimitivesPaddingSpaceBySize()
 * at view/FlexibleCard/components/utils.tsx
 */
var getPadding = function getPadding(size) {
  switch (size) {
    case _constants.SmartLinkSize.XLarge:
      return "var(--ds-space-300, 24px)";
    case _constants.SmartLinkSize.Large:
      return "var(--ds-space-250, 20px)";
    case _constants.SmartLinkSize.Medium:
      return "var(--ds-space-200, 16px)";
    case _constants.SmartLinkSize.Small:
    default:
      return "var(--ds-space-100, 8px)";
  }
};
var getGap = function getGap(size) {
  switch (size) {
    case _constants.SmartLinkSize.XLarge:
      return "var(--ds-space-250, 20px)";
    case _constants.SmartLinkSize.Large:
      return "var(--ds-space-200, 16px)";
    case _constants.SmartLinkSize.Medium:
      return "var(--ds-space-100, 8px)";
    case _constants.SmartLinkSize.Small:
    default:
      return "var(--ds-space-050, 4px)";
  }
};
var previewOnLeftStyleMap = {
  xlarge: "_19bver0k _y5vk1ejb",
  large: "_19bv16ru _y5vkv47k",
  medium: "_19bv1g7w _y5vk1eak",
  small: "_19bvlbes _y5vku2gc"
};
var previewOnRightStyleMap = {
  xlarge: "_u5f3er0k _1yob1ejb",
  large: "_u5f316ru _1yobv47k",
  medium: "_u5f31g7w _1yob1eak",
  small: "_u5f3lbes _1yobu2gc"
};

/**
 * A container is a hidden component that build the Flexible Smart Link.
 * All the Flexible UI components are wrapped inside the container.
 * It inherits the ui props from Card component and applies the custom styling
 * accordingly.
 * @internal
 * @see Block
 */
var Container = function Container(_ref3) {
  var children = _ref3.children,
    _ref3$clickableContai = _ref3.clickableContainer,
    clickableContainer = _ref3$clickableContai === void 0 ? false : _ref3$clickableContai,
    _ref3$hideBackground = _ref3.hideBackground,
    hideBackground = _ref3$hideBackground === void 0 ? false : _ref3$hideBackground,
    _ref3$hideElevation = _ref3.hideElevation,
    hideElevation = _ref3$hideElevation === void 0 ? false : _ref3$hideElevation,
    _ref3$hidePadding = _ref3.hidePadding,
    hidePadding = _ref3$hidePadding === void 0 ? false : _ref3$hidePadding,
    onClick = _ref3.onClick,
    onAuxClick = _ref3.onAuxClick,
    onContextMenu = _ref3.onContextMenu,
    retry = _ref3.retry,
    _ref3$showHoverPrevie = _ref3.showHoverPreview,
    showHoverPreview = _ref3$showHoverPrevie === void 0 ? false : _ref3$showHoverPrevie,
    hoverPreviewOptions = _ref3.hoverPreviewOptions,
    actionOptions = _ref3.actionOptions,
    _ref3$removeBlockRest = _ref3.removeBlockRestriction,
    removeBlockRestriction = _ref3$removeBlockRest === void 0 ? false : _ref3$removeBlockRest,
    _ref3$size = _ref3.size,
    size = _ref3$size === void 0 ? _constants.SmartLinkSize.Medium : _ref3$size,
    status = _ref3.status,
    _ref3$testId = _ref3.testId,
    testId = _ref3$testId === void 0 ? 'smart-links-container' : _ref3$testId,
    title = _ref3.title;
  var padding = hidePadding ? '0px' : getPadding(size);
  var gap = getGap(size);
  var context = (0, _flexibleUiContext.useFlexibleUiContext)();
  var _getChildrenOptions = getChildrenOptions(children, context),
    previewOnLeft = _getChildrenOptions.previewOnLeft,
    previewOnRight = _getChildrenOptions.previewOnRight;
  var isResolved = status === 'resolved';
  var isUnresolvedWithAuthFlow = status !== 'resolved' && retry !== undefined;
  var hasHoverCardOverride = (hoverPreviewOptions === null || hoverPreviewOptions === void 0 ? void 0 : hoverPreviewOptions.render) !== undefined;
  var canShowHoverCard = showHoverPreview && (isResolved || isUnresolvedWithAuthFlow || hasHoverCardOverride);
  var containerPaddingStyles = null;
  var container = /*#__PURE__*/_react.default.createElement("div", {
    "data-smart-link-container": true,
    "data-testid": testId,
    className: (0, _runtime.ax)(["_1e0c1txw _2lx21bp4 _1ul9idpf _1reo15vq _kqswh2mm _1pcmkb7n _1v7y1i6y _ufn21i6y", gapStyleMap[size], !hideBackground && "_bfhkhp5a", "_1yt418y6 _jb7v18y6 _y5vk167t _1yob1wow _r37x1r5k", previewOnLeft && previewOnLeftStyleMap[size], previewOnRight && previewOnRightStyleMap[size], !hideElevation && "_19itia51 _2rkopb1k _19pkv77o _2hwxv77o _otyrv77o _18u0v77o", clickableContainer && "_nqwih2mm _18ywh2mm _1nq3h2mm _1t4ckb7n _1nxdkb7n _xnbykb7n"]),
    style: {
      "--_1xumd0e": (0, _runtime.ix)(padding),
      "--_73mooq": (0, _runtime.ix)(previewOnLeft ? gap : padding),
      "--_12k13bg": (0, _runtime.ix)(previewOnRight ? gap : padding)
    }
  }, clickableContainer ? getLayeredLink(testId, context, children, onClick, onAuxClick, onContextMenu, title) : null, filterChildren(children, removeBlockRestriction));
  if (context !== null && context !== void 0 && context.url && canShowHoverCard) {
    return /*#__PURE__*/_react.default.createElement(_hoverCardControl.default, {
      isHoverPreview: isResolved || hasHoverCardOverride,
      isAuthTooltip: isUnresolvedWithAuthFlow,
      actionOptions: actionOptions,
      testId: testId,
      url: context.url,
      hoverPreviewOptions: hoverPreviewOptions
    }, container);
  }
  return container;
};
var _default = exports.default = Container;