"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 _react = _interopRequireDefault(require("react"));
var _link = _interopRequireDefault(require("@atlaskit/icon/core/migration/link"));
var _platformFeatureFlagsReact = require("@atlaskit/platform-feature-flags-react");
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 FrameComponent = (0, _platformFeatureFlagsReact.componentWithFG)('rovo_chat_embed_card_dwell_and_hover_metrics', _Frame.FrameUpdated, _Frame.Frame);
var EmbedCardResolvedViewOld = /*#__PURE__*/_react.default.forwardRef(function (_ref, embedIframeRef) {
  var link = _ref.link,
    context = _ref.context,
    onClick = _ref.onClick,
    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,
    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 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",
      LEGACY_size: "small",
      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, {
      src: src,
      default: linkGlyph,
      appearance: (0, _utils.isProfileType)(type) ? 'round' : 'square',
      hideLoadingSkeleton: hideIconLoadingSkeleton
    });
  }, [iconFromContext, src, linkGlyph, type, hideIconLoadingSkeleton]);
  (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);
  }
  return /*#__PURE__*/_react.default.createElement(_ExpandedFrame.ExpandedFrame, {
    isSelected: isSelected,
    frameStyle: frameStyle,
    href: link,
    testId: testId,
    icon: icon,
    text: text,
    onClick: onClick,
    inheritDimensions: inheritDimensions,
    setOverflow: false,
    CompetitorPrompt: CompetitorPrompt
  }, /*#__PURE__*/_react.default.createElement(_Frame.Frame, {
    url: previewUrl,
    isTrusted: isTrusted,
    testId: testId,
    ref: embedIframeRef,
    onIframeDwell: onIframeDwell,
    onIframeFocus: onIframeFocus,
    title: text,
    extensionKey: extensionKey
  }));
});
var EmbedCardResolvedViewUpdated = /*#__PURE__*/_react.default.forwardRef(function (_ref2, embedIframeRef) {
  var link = _ref2.link,
    context = _ref2.context,
    onClick = _ref2.onClick,
    isSelected = _ref2.isSelected,
    frameStyle = _ref2.frameStyle,
    preview = _ref2.preview,
    title = _ref2.title,
    isTrusted = _ref2.isTrusted,
    _ref2$testId = _ref2.testId,
    testId = _ref2$testId === void 0 ? 'embed-card-resolved-view' : _ref2$testId,
    inheritDimensions = _ref2.inheritDimensions,
    onIframeDwell = _ref2.onIframeDwell,
    onIframeFocus = _ref2.onIframeFocus,
    onIframeMouseEnter = _ref2.onIframeMouseEnter,
    onIframeMouseLeave = _ref2.onIframeMouseLeave,
    isSupportTheming = _ref2.isSupportTheming,
    type = _ref2.type,
    CompetitorPrompt = _ref2.CompetitorPrompt,
    hideIconLoadingSkeleton = _ref2.hideIconLoadingSkeleton,
    extensionKey = _ref2.extensionKey;
  var iconFromContext = context === null || context === void 0 ? void 0 : context.icon;
  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",
      LEGACY_size: "small",
      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, {
      src: src,
      default: linkGlyph,
      appearance: (0, _utils.isProfileType)(type) ? 'round' : 'square',
      hideLoadingSkeleton: hideIconLoadingSkeleton
    });
  }, [iconFromContext, src, linkGlyph, type, hideIconLoadingSkeleton]);
  (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,
    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(FrameComponent, {
    url: previewUrl,
    isTrusted: isTrusted,
    testId: testId,
    ref: embedIframeRef,
    onIframeDwell: onIframeDwell,
    onIframeFocus: onIframeFocus,
    onIframeMouseEnter: onIframeMouseEnter,
    onIframeMouseLeave: onIframeMouseLeave,
    isMouseOver: isMouseOver,
    title: text,
    extensionKey: extensionKey
  }));
});
var EmbedCardResolvedViewWithFG = exports.EmbedCardResolvedView = (0, _platformFeatureFlagsReact.componentWithFG)('rovo_chat_embed_card_dwell_and_hover_metrics', EmbedCardResolvedViewUpdated, EmbedCardResolvedViewOld);