/* ResolvedView.tsx generated by @compiled/babel-plugin v0.39.1 */
import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
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 useBlockCardRovoAction from '../../../state/hooks/use-block-card-rovo-action-experiment';
import useRovoConfig from '../../../state/hooks/use-rovo-config';
import { RovoChatPromptKey } from '../../common/rovo-chat-utils';
import FlexibleCard from '../../FlexibleCard';
import { FooterBlock, MetadataBlock, PreviewBlock, SnippetBlock, TitleBlock } from '../../FlexibleCard/components/blocks';
import { FlexibleCardUiOptions, getSimulatedBetterMetadata, PreviewBlockOptions, titleBlockOptions } from './utils';
import { withFlexibleUIBlockCardStyle } from './utils/withFlexibleUIBlockCardStyle';
var titleBlockCss = null;
var footerBlockCss = null;
var footerExtraSpacingCss = null;
var footerBlockSafariStyles = null;

/**
 * This view represents a Block card that has an 'Resolved' status.
 * @see SmartLinkStatus
 * @see FlexibleCardProps
 */
var ResolvedView = function ResolvedView(_ref) {
  var _cardState$details2;
  var cardState = _ref.cardState,
    onClick = _ref.onClick,
    onAuxClick = _ref.onAuxClick,
    onContextMenu = _ref.onContextMenu,
    onError = _ref.onError,
    onResolve = _ref.onResolve,
    actionOptions = _ref.actionOptions,
    _ref$testId = _ref.testId,
    testId = _ref$testId === void 0 ? 'smart-block-resolved-view' : _ref$testId,
    url = _ref.url,
    CompetitorPrompt = _ref.CompetitorPrompt,
    hideIconLoadingSkeleton = _ref.hideIconLoadingSkeleton;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isPreviewBlockErrored = _useState2[0],
    setIsPreviewBlockErrored = _useState2[1];
  var extensionKey = getExtensionKey(cardState.details);
  var is3PRovoBlockRovoActionEnabled = useBlockCardRovoAction(url, actionOptions);
  var rovoConfig = useRovoConfig();

  // eslint-disable-next-line react-hooks/rules-of-hooks
  var _useMemo = useMemo(function () {
      return browser();
    }, []),
    _useMemo$safari = _useMemo.safari,
    safari = _useMemo$safari === void 0 ? false : _useMemo$safari;
  useEffect(function () {
    setIsPreviewBlockErrored(false);
  }, [url, cardState]);
  var _getSimulatedBetterMe = getSimulatedBetterMetadata(cardState.details),
    titleMetadata = _getSimulatedBetterMe.titleMetadata,
    topMetadata = _getSimulatedBetterMe.topMetadata,
    bottomMetadata = _getSimulatedBetterMe.bottomMetadata;
  var prompts = useMemo(function () {
    if (is3PRovoBlockRovoActionEnabled) {
      var _cardState$details;
      var defaultPrompts = [RovoChatPromptKey.KEY_HIGHLIGHTS];
      var linkType = (_cardState$details = cardState.details) === null || _cardState$details === void 0 || (_cardState$details = _cardState$details.data) === null || _cardState$details === void 0 ? void 0 : _cardState$details['@type'];
      if (extensionKey === 'slack-object-provider') {
        return [RovoChatPromptKey.FIND_OPEN_QUESTIONS].concat(defaultPrompts);
      }
      if (extensionKey === 'google-object-provider' && linkType !== null && linkType !== void 0 && linkType.includes('schema:PresentationDigitalDocument')) {
        return [RovoChatPromptKey.IDENTIFY_KEY_POINTS].concat(defaultPrompts);
      }
      if (extensionKey === 'google-object-provider' && linkType !== null && linkType !== void 0 && linkType.includes('schema:SpreadsheetDigitalDocument')) {
        return [RovoChatPromptKey.IDENTIFY_KEY_TRENDS].concat(defaultPrompts);
      }
      return [RovoChatPromptKey.SUMMARIZE_LINK].concat(defaultPrompts);
    }
    return [];
  }, [cardState === null || cardState === void 0 || (_cardState$details2 = cardState.details) === null || _cardState$details2 === void 0 ? void 0 : _cardState$details2.data, extensionKey, is3PRovoBlockRovoActionEnabled]);
  var footerActions = useMemo(function () {
    var showRovoResolvedView = (cardState === null || cardState === void 0 ? void 0 : cardState.status) === 'resolved' && cardState.details && extractRovoChatAction({
      response: cardState.details,
      rovoConfig: rovoConfig,
      actionOptions: actionOptions
    }) !== undefined;
    return showRovoResolvedView && is3PRovoBlockRovoActionEnabled ? [{
      name: ActionName.RovoChatAction,
      prompts: prompts,
      iconSize: 'small',
      cardAppearance: CardDisplay.Block
    }, {
      name: ActionName.FollowAction,
      iconSize: 'small'
    }, {
      name: ActionName.DownloadAction,
      iconSize: 'small'
    }] : [{
      name: ActionName.FollowAction,
      hideIcon: true
    }, {
      name: ActionName.PreviewAction,
      hideIcon: true
    }, {
      name: ActionName.DownloadAction,
      hideIcon: true
    }];
  }, [prompts, is3PRovoBlockRovoActionEnabled, cardState, rovoConfig, actionOptions]);
  var uiOptions = FlexibleCardUiOptions;
  uiOptions.enableSnippetRenderer = true;
  uiOptions.hideLoadingSkeleton = hideIconLoadingSkeleton;
  return /*#__PURE__*/React.createElement(FlexibleCard, {
    appearance: "block",
    cardState: cardState,
    onClick: onClick,
    onAuxClick: onAuxClick,
    onContextMenu: onContextMenu,
    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: function onError() {
      setIsPreviewBlockErrored(true);
    }
  })) : null, /*#__PURE__*/React.createElement(FooterBlock, {
    actions: footerActions,
    isPreviewBlockErrored: isPreviewBlockErrored,
    className: ax(["_4t3i1k8s _1e0c1txw _1bah1yb4 _4cvresu3 _1wpz1fhb", safari && "_4t3i1osq", fg('platform_navx_block_card_footer_spacing') && "_19pku2gc"])
  }));
};
var _default_1 = withFlexibleUIBlockCardStyle(ResolvedView);
export default _default_1;