/* ResolvedView.tsx generated by @compiled/babel-plugin v0.39.1 */
import _extends from "@babel/runtime/helpers/extends";
import "./ResolvedView.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
import { useEffect, useMemo, useState } from 'react';
import { browser } from '@atlaskit/linking-common/user-agent';
import { fg } from '@atlaskit/platform-feature-flags';
import { ActionName, CardDisplay, ElementName, SmartLinkPosition } from '../../../constants';
import extractRovoChatAction from '../../../extractors/flexible/actions/extract-rovo-chat-action';
import { getExtensionKey } from '../../../state/helpers';
import useRovoConfig from '../../../state/hooks/use-rovo-config';
import FlexibleCard from '../../FlexibleCard';
import { RovoChatPromptKey } from '../../FlexibleCard/components/actions/rovo-chat-action';
import { FooterBlock, MetadataBlock, PreviewBlock, SnippetBlock, TitleBlock } from '../../FlexibleCard/components/blocks';
import { FlexibleCardUiOptions, getSimulatedBetterMetadata, PreviewBlockOptions, titleBlockOptions } from './utils';
import { withFlexibleUIBlockCardStyle } from './utils/withFlexibleUIBlockCardStyle';
const titleBlockCss = null;
const footerBlockCss = null;
const footerBlockSafariStyles = null;

/**
 * This view represents a Block card that has an 'Resolved' status.
 * @see SmartLinkStatus
 * @see FlexibleCardProps
 */
const ResolvedView = ({
  cardState,
  onClick,
  onError,
  onResolve,
  actionOptions,
  testId = 'smart-block-resolved-view',
  url,
  CompetitorPrompt,
  hideIconLoadingSkeleton
}) => {
  var _cardState$details2;
  const [isPreviewBlockErrored, setIsPreviewBlockErrored] = useState(false);
  const extensionKey = getExtensionKey(cardState.details);
  const rovoConfig = fg('platform_sl_3p_auth_rovo_block_card_kill_switch') ?
  // eslint-disable-next-line react-hooks/rules-of-hooks
  useRovoConfig() : undefined;
  const showRovoResolvedView = fg('platform_sl_3p_auth_rovo_block_card_kill_switch') ?
  // eslint-disable-next-line react-hooks/rules-of-hooks
  useMemo(() => (cardState === null || cardState === void 0 ? void 0 : cardState.status) === 'resolved' && cardState.details && extractRovoChatAction({
    response: cardState.details,
    rovoConfig,
    actionOptions: actionOptions
  }) !== undefined, [actionOptions, cardState === null || cardState === void 0 ? void 0 : cardState.details, cardState === null || cardState === void 0 ? void 0 : cardState.status, rovoConfig]) : undefined;

  // eslint-disable-next-line react-hooks/rules-of-hooks
  const {
    safari = false
  } = useMemo(() => browser(), []);
  useEffect(() => {
    setIsPreviewBlockErrored(false);
  }, [url, cardState]);
  const {
    titleMetadata,
    topMetadata,
    bottomMetadata
  } = getSimulatedBetterMetadata(cardState.details);
  const prompts = useMemo(() => {
    if (fg('platform_sl_3p_auth_rovo_block_card_kill_switch')) {
      var _cardState$details, _cardState$details$da;
      const defaultPrompts = [RovoChatPromptKey.KEY_HIGHLIGHTS];
      const linkType = (_cardState$details = cardState.details) === null || _cardState$details === void 0 ? void 0 : (_cardState$details$da = _cardState$details.data) === null || _cardState$details$da === void 0 ? void 0 : _cardState$details$da['@type'];
      if (extensionKey === 'slack-object-provider') {
        return [RovoChatPromptKey.FIND_OPEN_QUESTIONS, ...defaultPrompts];
      }
      if (extensionKey === 'google-object-provider' && linkType !== null && linkType !== void 0 && linkType.includes('schema:PresentationDigitalDocument')) {
        return [RovoChatPromptKey.IDENTIFY_KEY_POINTS, ...defaultPrompts];
      }
      if (extensionKey === 'google-object-provider' && linkType !== null && linkType !== void 0 && linkType.includes('schema:SpreadsheetDigitalDocument')) {
        return [RovoChatPromptKey.IDENTIFY_KEY_TRENDS, ...defaultPrompts];
      }
      return [RovoChatPromptKey.SUMMARIZE_LINK, ...defaultPrompts];
    }
    return [];
  }, [cardState === null || cardState === void 0 ? void 0 : (_cardState$details2 = cardState.details) === null || _cardState$details2 === void 0 ? void 0 : _cardState$details2.data, extensionKey]);
  const footerActions = useMemo(() => {
    if (showRovoResolvedView && fg('platform_sl_3p_auth_rovo_block_card_kill_switch')) {
      return [{
        name: ActionName.RovoChatAction,
        prompts: prompts,
        iconSize: 'small',
        cardAppearance: CardDisplay.Block
      }, {
        name: ActionName.FollowAction,
        iconSize: 'small'
      }, {
        name: ActionName.DownloadAction,
        iconSize: 'small'
      }];
    }
    return [{
      name: ActionName.FollowAction,
      hideIcon: true
    }, {
      name: ActionName.PreviewAction,
      hideIcon: true
    }, {
      name: ActionName.DownloadAction,
      hideIcon: true
    }];
  }, [showRovoResolvedView, prompts]);
  const uiOptions = FlexibleCardUiOptions;
  uiOptions.enableSnippetRenderer = true;
  uiOptions.hideLoadingSkeleton = hideIconLoadingSkeleton;
  return /*#__PURE__*/React.createElement(FlexibleCard, {
    appearance: "block",
    cardState: cardState,
    onClick: onClick,
    onError: onError,
    onResolve: onResolve,
    origin: "smartLinkCard",
    actionOptions: actionOptions,
    testId: testId,
    ui: uiOptions,
    url: url
  }, /*#__PURE__*/React.createElement(TitleBlock, _extends({}, titleBlockOptions, {
    metadata: titleMetadata,
    subtitle: [{
      name: ElementName.Location
    }],
    metadataPosition: SmartLinkPosition.Top,
    CompetitorPrompt: CompetitorPrompt,
    url: url,
    hideIconLoadingSkeleton: hideIconLoadingSkeleton,
    className: ax(["_zulpu2gc _13mh1pd9 _2yhz13af"])
  })), /*#__PURE__*/React.createElement(MetadataBlock, {
    primary: topMetadata,
    maxLines: 1
  }), /*#__PURE__*/React.createElement(SnippetBlock, null), /*#__PURE__*/React.createElement(MetadataBlock, {
    primary: bottomMetadata,
    maxLines: 1
  }), !isPreviewBlockErrored ? /*#__PURE__*/React.createElement(PreviewBlock, _extends({}, PreviewBlockOptions, {
    onError: () => {
      setIsPreviewBlockErrored(true);
    }
  })) : null, /*#__PURE__*/React.createElement(FooterBlock, {
    actions: footerActions,
    isPreviewBlockErrored: isPreviewBlockErrored,
    className: ax(["_4t3i1k8s _1e0c1txw _1bah1yb4 _4cvresu3 _1wpz1fhb", safari && "_4t3i1osq"])
  }));
};
const _default_1 = withFlexibleUIBlockCardStyle(ResolvedView);
export default _default_1;