import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _extends from "@babel/runtime/helpers/extends";
import React from 'react';
import LinkGlyph from '@atlaskit/icon/core/link';
import { fg } from '@atlaskit/platform-feature-flags';
import { useThemeObserver } from '@atlaskit/tokens';
import { getPreviewUrlWithTheme, isProfileType } from '../../../utils';
import { ExpandedFrame } from '../components/ExpandedFrame';
import { Frame } from '../components/Frame';
import { ImageIcon } from '../components/ImageIcon';
import { useEmbedResolvePostMessageListener } from '../useEmbedResolvePostMessageListener';
export var EmbedCardResolvedView = /*#__PURE__*/React.forwardRef(function (_ref, embedIframeRef) {
  var link = _ref.link,
    context = _ref.context,
    onClick = _ref.onClick,
    onAuxClick = _ref.onAuxClick,
    onContextMenu = _ref.onContextMenu,
    isSelected = _ref.isSelected,
    frameStyle = _ref.frameStyle,
    preview = _ref.preview,
    title = _ref.title,
    isTrusted = _ref.isTrusted,
    _ref$testId = _ref.testId,
    testId = _ref$testId === void 0 ? 'embed-card-resolved-view' : _ref$testId,
    inheritDimensions = _ref.inheritDimensions,
    onIframeDwell = _ref.onIframeDwell,
    onIframeFocus = _ref.onIframeFocus,
    onIframeMouseEnter = _ref.onIframeMouseEnter,
    onIframeMouseLeave = _ref.onIframeMouseLeave,
    isSupportTheming = _ref.isSupportTheming,
    type = _ref.type,
    CompetitorPrompt = _ref.CompetitorPrompt,
    hideIconLoadingSkeleton = _ref.hideIconLoadingSkeleton,
    extensionKey = _ref.extensionKey;
  var iconFromContext = context === null || context === void 0 ? void 0 : context.icon;
  var iconLabel = context === null || context === void 0 ? void 0 : context.iconLabel;
  var src = typeof iconFromContext === 'string' ? iconFromContext : undefined;
  var text = title || (context === null || context === void 0 ? void 0 : context.text);
  var linkGlyph = React.useMemo(function () {
    return /*#__PURE__*/React.createElement(LinkGlyph, {
      label: "icon",
      testId: "embed-card-fallback-icon",
      color: "currentColor"
    });
  }, []);
  var icon = React.useMemo(function () {
    if ( /*#__PURE__*/React.isValidElement(iconFromContext)) {
      return iconFromContext;
    }
    return /*#__PURE__*/React.createElement(ImageIcon, _extends({
      src: src
    }, fg('platform_lp_use_entity_icon_url_for_icon') ? {
      alt: iconLabel
    } : undefined, {
      default: linkGlyph,
      appearance: isProfileType(type) ? 'round' : 'square',
      hideLoadingSkeleton: hideIconLoadingSkeleton
    }));
  }, [iconFromContext, src, linkGlyph, type, hideIconLoadingSkeleton, iconLabel]);
  useEmbedResolvePostMessageListener({
    url: link,
    embedIframeRef: embedIframeRef
  });
  var themeState = useThemeObserver();
  var previewUrl = preview === null || preview === void 0 ? void 0 : preview.src;
  if (previewUrl && isSupportTheming) {
    previewUrl = getPreviewUrlWithTheme(previewUrl, themeState);
  }
  var _React$useState = React.useState(false),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    isMouseOver = _React$useState2[0],
    setMouseOver = _React$useState2[1];
  return /*#__PURE__*/React.createElement(ExpandedFrame, {
    isSelected: isSelected,
    frameStyle: frameStyle,
    href: link,
    testId: testId,
    icon: icon,
    text: text,
    onClick: onClick,
    onAuxClick: onAuxClick,
    onContextMenu: onContextMenu,
    inheritDimensions: inheritDimensions,
    setOverflow: false,
    CompetitorPrompt: CompetitorPrompt,
    onContentMouseEnter: function onContentMouseEnter() {
      setMouseOver(true);
      onIframeMouseEnter === null || onIframeMouseEnter === void 0 || onIframeMouseEnter();
    },
    onContentMouseLeave: function onContentMouseLeave() {
      setMouseOver(false);
      onIframeMouseLeave === null || onIframeMouseLeave === void 0 || onIframeMouseLeave();
    }
  }, /*#__PURE__*/React.createElement(Frame, {
    url: previewUrl,
    isTrusted: isTrusted,
    testId: testId,
    ref: embedIframeRef,
    onIframeDwell: onIframeDwell,
    onIframeFocus: onIframeFocus,
    onIframeMouseEnter: onIframeMouseEnter,
    onIframeMouseLeave: onIframeMouseLeave,
    isMouseOver: isMouseOver,
    title: text,
    extensionKey: extensionKey
  }));
});