/* index.tsx generated by @compiled/babel-plugin v0.38.1 */
import _extends from "@babel/runtime/helpers/extends";
import "./index.compiled.css";
import { ax, ix } from "@compiled/react/runtime";
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
import { di } from 'react-magnetic-di';
import { fg } from '@atlaskit/platform-feature-flags';
import { useAnalyticsEvents } from '../../../../../common/analytics/generated/use-analytics-events';
import { CardDisplay, SmartLinkPosition, SmartLinkSize } from '../../../../../constants';
import { succeedUfoExperience } from '../../../../../state/analytics';
import useAISummaryAction from '../../../../../state/hooks/use-ai-summary-action';
import useRovoConfig from '../../../../../state/hooks/use-rovo-config';
import FlexibleCard from '../../../../FlexibleCard';
import { ActionBlock, AIFooterBlock, AISummaryBlock, MetadataBlock, SnippetBlock, TitleBlock } from '../../../../FlexibleCard/components/blocks';
import { RovoSummaryBlock } from '../../../../FlexibleCard/components/blocks/ai-summary-block';
import { getMetadata } from '../../../utils';
import ImagePreview from '../../ImagePreview';
const hiddenSnippetStyles = null;
const titleBlockStyles = null;
const snippetBlockCss = null;
const actionBlockCss = null;
const HoverCardResolvedView = ({
  cardState,
  extensionKey,
  flexibleCardProps,
  isAISummaryEnabled,
  onActionClick,
  titleBlockProps,
  id,
  url
}) => {
  var _cardState$details;
  const {
    fireEvent
  } = useAnalyticsEvents();
  const rovoConfig = useRovoConfig();
  useEffect(() => {
    // Since this hover view is only rendered on resolved status,
    // there is no need to check for statuses.
    succeedUfoExperience('smart-link-rendered', id || 'NULL', {
      extensionKey,
      display: CardDisplay.HoverCardPreview
    });

    // UFO will disregard this if authentication experience has not yet been started
    succeedUfoExperience('smart-link-authenticated', id || 'NULL', {
      display: CardDisplay.HoverCardPreview
    });
    fireEvent('ui.smartLink.renderSuccess', {
      display: CardDisplay.HoverCardPreview
    });
  }, [extensionKey, fireEvent, id]);
  const data = (_cardState$details = cardState.details) === null || _cardState$details === void 0 ? void 0 : _cardState$details.data;
  const {
    primary,
    secondary
  } = useMemo(() => {
    return getMetadata(extensionKey, data);
  }, [data, extensionKey]);
  const snippetHeight = React.useRef(0);
  const snippetBlockRef = useRef(null);
  const onSnippetRender = useCallback(() => {
    var _snippetBlockRef$curr, _snippetBlockRef$curr2;
    snippetHeight.current = (_snippetBlockRef$curr = (_snippetBlockRef$curr2 = snippetBlockRef.current) === null || _snippetBlockRef$curr2 === void 0 ? void 0 : _snippetBlockRef$curr2.getBoundingClientRect().height) !== null && _snippetBlockRef$curr !== void 0 ? _snippetBlockRef$curr : 0;
  }, []);
  const imagePreview = ImagePreview({
    fallbackElementHeight: snippetHeight.current,
    response: cardState.details
  });
  const snippet = imagePreview ? null : /*#__PURE__*/React.createElement(SnippetBlock, {
    className: ax(["_ca0q1b66"])
  });
  const aiSummaryMinHeight = snippet ? snippetHeight.current : 0;
  const isRovoSummaryEnabled = (rovoConfig === null || rovoConfig === void 0 ? void 0 : rovoConfig.isRovoEnabled) && extensionKey === 'google-object-provider' && fg('platform_sl_3p_auth_rovo_action_kill_switch');
  const aiSummaryProps = fg('platform_sl_3p_auth_rovo_action_kill_switch') ?
  // eslint-disable-next-line react-hooks/rules-of-hooks
  useAISummaryAction(url) : undefined;
  const hasSummarised = useRef(false);
  useEffect(() => {
    if (fg('platform_sl_3p_auth_rovo_action_kill_switch')) {
      if ((aiSummaryProps === null || aiSummaryProps === void 0 ? void 0 : aiSummaryProps.state.status) !== 'ready' || hasSummarised.current) {
        return;
      }
      hasSummarised.current = true;
      aiSummaryProps === null || aiSummaryProps === void 0 ? void 0 : aiSummaryProps.summariseUrl();
    }
  }, [aiSummaryProps, isRovoSummaryEnabled]);
  return /*#__PURE__*/React.createElement(FlexibleCard, flexibleCardProps, imagePreview, /*#__PURE__*/React.createElement(TitleBlock, _extends({}, titleBlockProps, {
    metadataPosition: SmartLinkPosition.Top,
    className: ax(["_zulpu2gc _quenze3t _oebj1txw _13mh1pd9"])
  })), /*#__PURE__*/React.createElement(MetadataBlock, {
    primary: primary,
    secondary: secondary,
    maxLines: 1,
    size: SmartLinkSize.Medium
  }), isRovoSummaryEnabled ? /*#__PURE__*/React.createElement(RovoSummaryBlock, {
    aiSummaryMinHeight: aiSummaryMinHeight,
    placeholder: snippet,
    url: url
  }) : isAISummaryEnabled ? /*#__PURE__*/React.createElement(AISummaryBlock, {
    aiSummaryMinHeight: aiSummaryMinHeight,
    placeholder: snippet
  }) : snippet, /*#__PURE__*/React.createElement(SnippetBlock, {
    testId: "hidden-snippet",
    onRender: onSnippetRender,
    blockRef: snippetBlockRef,
    isHidden: true,
    className: ax(["_3um015vq _kqswstnw"])
  }), /*#__PURE__*/React.createElement(ActionBlock, {
    onClick: onActionClick,
    spaceInline: "space.100",
    hideAISummaryAction: isRovoSummaryEnabled,
    className: ax(["_n3td1b66"])
  }), /*#__PURE__*/React.createElement(AIFooterBlock, null));
};
export default HoverCardResolvedView;