import _extends from "@babel/runtime/helpers/extends";
import React, { useEffect, useMemo } from 'react';
import { useSmartLinkContext } from '@atlaskit/link-provider';
import { fg } from '@atlaskit/platform-feature-flags';
import UFOHoldLoad from '@atlaskit/react-ufo/load-hold';
import { useAnalyticsEvents } from '../../common/analytics/generated/use-analytics-events';
import { InternalActionName, SmartLinkStatus } from '../../constants';
import { extractPlaceHolderCardState } from '../../extractors/flexible/extract-placeholder-data';
import { FlexibleCardContext } from '../../state/flexible-ui-context';
import { useAISummaryConfig } from '../../state/hooks/use-ai-summary-config';
import useResolve from '../../state/hooks/use-resolve';
import useRovoConfig from '../../state/hooks/use-rovo-config';
import Container from './components/container';
import { getContextByStatus } from './utils';
const PENDING_LINK_STATUSES = [SmartLinkStatus.Pending, SmartLinkStatus.Resolving];

/**
 * This represents a Flexible Card: a link represented by a card with metadata.
 * This is the container in which all Flexible UI Blocks and Elements exist.
 * Note: TitleBlock is mandatory for a Flexible Card to render.
 * @see Container
 */
const FlexibleCard = ({
  appearance = 'flexible',
  cardState,
  children,
  id,
  onAuthorize,
  onClick,
  onError,
  onResolve,
  origin,
  placeholderData,
  renderers,
  showHoverPreview,
  hoverPreviewOptions,
  actionOptions,
  testId,
  ui,
  url
}) => {
  var _details$meta2, _flexibleCardContext$, _flexibleCardContext$2;
  const aiSummaryConfig = useAISummaryConfig();
  const resolve = useResolve();
  const {
    isPreviewPanelAvailable,
    openPreviewPanel,
    product
  } = useSmartLinkContext();
  const rovoConfig = fg('platform_sl_3p_auth_rovo_action_kill_switch') || fg('rovogrowth-640-inline-action-nudge-fg') ?
  // eslint-disable-next-line react-hooks/rules-of-hooks
  useRovoConfig() : undefined;
  const {
    fireEvent
  } = useAnalyticsEvents();
  const {
    status: cardType,
    details
  } = cardState;
  const status = cardType;

  // if we have placeholder state it means we can internally use it
  // as temporary resolved data until the actual data comes back as one of the final statuses
  const placeholderCardState = useMemo(() => PENDING_LINK_STATUSES.includes(status) && placeholderData && extractPlaceHolderCardState(placeholderData), [placeholderData, status]);
  const placeHolderStatus = placeholderCardState ? placeholderCardState.status : undefined;
  const context = useMemo(() => getContextByStatus({
    aiSummaryConfig,
    appearance,
    fireEvent,
    response: placeholderCardState ? placeholderCardState.details : details,
    id,
    onAuthorize,
    onClick,
    origin,
    renderers,
    resolve,
    ...(fg('platform_sl_3p_auth_rovo_action_kill_switch') || fg('rovogrowth-640-inline-action-nudge-fg') ? {
      product,
      rovoConfig
    } : undefined),
    actionOptions,
    status: placeholderCardState ? placeHolderStatus : status,
    url,
    isPreviewPanelAvailable,
    openPreviewPanel
  }), [aiSummaryConfig, appearance, actionOptions, details, id, isPreviewPanelAvailable, onAuthorize, onClick, openPreviewPanel, origin, placeholderCardState, placeHolderStatus, product, renderers, resolve, rovoConfig, status, url, fireEvent]);
  const flexibleCardContext = useMemo(() => ({
    data: context,
    status: placeHolderStatus !== null && placeHolderStatus !== void 0 ? placeHolderStatus : status,
    ui
  }), [context, placeHolderStatus, status, ui]);
  const {
    linkTitle
  } = context || {};
  const title = linkTitle === null || linkTitle === void 0 ? void 0 : linkTitle.text;
  useEffect(() => {
    switch (status) {
      case SmartLinkStatus.Resolved:
        if (onResolve) {
          var _details$meta;
          onResolve({
            title,
            url,
            extensionKey: details === null || details === void 0 ? void 0 : (_details$meta = details.meta) === null || _details$meta === void 0 ? void 0 : _details$meta.key
          });
        }
        break;
      case SmartLinkStatus.Errored:
      case SmartLinkStatus.Fallback:
      case SmartLinkStatus.Forbidden:
      case SmartLinkStatus.NotFound:
      case SmartLinkStatus.Unauthorized:
        if (onError) {
          onError({
            status,
            url
          });
        }
        break;
    }
  }, [onError, onResolve, status, title, url, details === null || details === void 0 ? void 0 : (_details$meta2 = details.meta) === null || _details$meta2 === void 0 ? void 0 : _details$meta2.key]);
  const retry = (_flexibleCardContext$ = flexibleCardContext.data) === null || _flexibleCardContext$ === void 0 ? void 0 : (_flexibleCardContext$2 = _flexibleCardContext$.actions) === null || _flexibleCardContext$2 === void 0 ? void 0 : _flexibleCardContext$2[InternalActionName.UnresolvedAction];
  return /*#__PURE__*/React.createElement(FlexibleCardContext.Provider, {
    value: flexibleCardContext
  }, PENDING_LINK_STATUSES.includes(status) && !Boolean(placeholderCardState) && /*#__PURE__*/React.createElement(UFOHoldLoad, {
    name: "smart-card-flexible-card"
  }), /*#__PURE__*/React.createElement(Container, _extends({
    testId: testId
  }, ui, {
    onClick: onClick,
    showHoverPreview: showHoverPreview,
    hoverPreviewOptions: hoverPreviewOptions,
    actionOptions: actionOptions,
    status: status,
    retry: retry
  }), children));
};
export default FlexibleCard;