/* 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 */
import "./index.compiled.css";
import { ax, ix } from "@compiled/react/runtime";
import React from 'react';
import { di } from 'react-magnetic-di';
import { MediaPlacement, SmartLinkSize } from '../../../../constants';
import { useFlexibleUiContext } from '../../../../state/flexible-ui-context';
import { isFlexUiPreviewPresent } from '../../../../state/flexible-ui-context/utils';
import { isFlexibleUiBlock, isFlexibleUiPreviewBlock, isStyleCacheProvider } from '../../../../utils/flexible';
import { TitleBlock } from '../blocks';
import HoverCardControl from './hover-card-control';
import LayeredLink from './layered-link';
export var getChildrenOptions = function getChildrenOptions(children, context) {
  var options = {};
  if (isFlexUiPreviewPresent(context)) {
    React.Children.map(children, function (child) {
      if ( /*#__PURE__*/React.isValidElement(child)) {
        if (isFlexibleUiPreviewBlock(child)) {
          var placement = child.props.placement;
          if (placement === MediaPlacement.Left) {
            options.previewOnLeft = true;
          }
          if (placement === MediaPlacement.Right) {
            options.previewOnRight = true;
          }
        }
      }
    });
  }
  return options;
};
var filterChildren = function filterChildren(children, removeBlockRestriction) {
  if (removeBlockRestriction) {
    return children;
  }
  return React.Children.map(children, function (child) {
    return /*#__PURE__*/React.isValidElement(child) && 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 = function getFlexibleUiBlock(node) {
  if (! /*#__PURE__*/React.isValidElement(node)) {
    return undefined;
  }
  if (node.type === TitleBlock) {
    return node;
  }
  if (isStyleCacheProvider(node)) {
    // Component wrapped with compiled at runtime, check for children
    var isChildrenValid;
    React.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;
};
export { _getFlexibleUiBlock as getFlexibleUiBlock };
var getTitleBlockProps = function getTitleBlockProps(children) {
  var block = React.Children.toArray(children).map(function (child) {
    return _getFlexibleUiBlock(child);
  }).filter(function (x) {
    return x !== undefined;
  }).at(0);
  if ( /*#__PURE__*/React.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.createElement(LayeredLink, {
    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 SmartLinkSize.XLarge:
      return "var(--ds-space-300, 24px)";
    case SmartLinkSize.Large:
      return "var(--ds-space-250, 20px)";
    case SmartLinkSize.Medium:
      return "var(--ds-space-200, 16px)";
    case SmartLinkSize.Small:
    default:
      return "var(--ds-space-100, 8px)";
  }
};
var getGap = function getGap(size) {
  switch (size) {
    case SmartLinkSize.XLarge:
      return "var(--ds-space-250, 20px)";
    case SmartLinkSize.Large:
      return "var(--ds-space-200, 16px)";
    case SmartLinkSize.Medium:
      return "var(--ds-space-100, 8px)";
    case 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 ? 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 = 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.createElement("div", {
    "data-smart-link-container": true,
    "data-testid": testId,
    className: 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": ix(padding),
      "--_73mooq": ix(previewOnLeft ? gap : padding),
      "--_12k13bg": 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.createElement(HoverCardControl, {
      isHoverPreview: isResolved || hasHoverCardOverride,
      isAuthTooltip: isUnresolvedWithAuthFlow,
      actionOptions: actionOptions,
      testId: testId,
      url: context.url,
      hoverPreviewOptions: hoverPreviewOptions
    }, container);
  }
  return container;
};
export default Container;