"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.EmbedCardResolvedView = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireDefault(require("react"));
var _link = _interopRequireDefault(require("@atlaskit/icon/core/link"));
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _tokens = require("@atlaskit/tokens");
var _utils = require("../../../utils");
var _ExpandedFrame = require("../components/ExpandedFrame");
var _Frame = require("../components/Frame");
var _ImageIcon = require("../components/ImageIcon");
var _useEmbedResolvePostMessageListener = require("../useEmbedResolvePostMessageListener");
var EmbedCardResolvedView = exports.EmbedCardResolvedView = /*#__PURE__*/_react.default.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.default.useMemo(function () {
    return /*#__PURE__*/_react.default.createElement(_link.default, {
      label: "icon",
      testId: "embed-card-fallback-icon",
      color: "currentColor"
    });
  }, []);
  var icon = _react.default.useMemo(function () {
    if ( /*#__PURE__*/_react.default.isValidElement(iconFromContext)) {
      return iconFromContext;
    }
    return /*#__PURE__*/_react.default.createElement(_ImageIcon.ImageIcon, (0, _extends2.default)({
      src: src
    }, (0, _platformFeatureFlags.fg)('platform_lp_use_entity_icon_url_for_icon') ? {
      alt: iconLabel
    } : undefined, {
      default: linkGlyph,
      appearance: (0, _utils.isProfileType)(type) ? 'round' : 'square',
      hideLoadingSkeleton: hideIconLoadingSkeleton
    }));
  }, [iconFromContext, src, linkGlyph, type, hideIconLoadingSkeleton, iconLabel]);
  (0, _useEmbedResolvePostMessageListener.useEmbedResolvePostMessageListener)({
    url: link,
    embedIframeRef: embedIframeRef
  });
  var themeState = (0, _tokens.useThemeObserver)();
  var previewUrl = preview === null || preview === void 0 ? void 0 : preview.src;
  if (previewUrl && isSupportTheming) {
    previewUrl = (0, _utils.getPreviewUrlWithTheme)(previewUrl, themeState);
  }
  var _React$useState = _react.default.useState(false),
    _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
    isMouseOver = _React$useState2[0],
    setMouseOver = _React$useState2[1];
  return /*#__PURE__*/_react.default.createElement(_ExpandedFrame.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.default.createElement(_Frame.Frame, {
    url: previewUrl,
    isTrusted: isTrusted,
    testId: testId,
    ref: embedIframeRef,
    onIframeDwell: onIframeDwell,
    onIframeFocus: onIframeFocus,
    onIframeMouseEnter: onIframeMouseEnter,
    onIframeMouseLeave: onIframeMouseLeave,
    isMouseOver: isMouseOver,
    title: text,
    extensionKey: extensionKey
  }));
});