import _extends from "@babel/runtime/helpers/extends";
import React, { useEffect } from 'react';
import { useAnalyticsEvents } from '@atlaskit/analytics-next';
import { extractSmartLinkProvider } from '@atlaskit/link-extractors';
import UFOHoldLoad from '@atlaskit/react-ufo/load-hold';
import { SmartLinkStatus } from '../../constants';
import { extractRequestAccessContextImproved } from '../../extractors/common/context/extractAccessContext';
import { extractInlineProps } from '../../extractors/inline';
import { getExtensionKey } from '../../state/helpers';
import { getForbiddenJsonLd } from '../../utils/jsonld';
import { CardLinkView } from '../LinkView';
import { InlineCardErroredView } from './ErroredView';
import { InlineCardForbiddenView } from './ForbiddenView';
import { InlineCardResolvedView } from './ResolvedView';
import { InlineCardResolvingView } from './ResolvingView';
import { InlineCardUnauthorizedView } from './UnauthorisedView';
export { InlineCardResolvedView, InlineCardResolvingView, InlineCardErroredView, InlineCardForbiddenView, InlineCardUnauthorizedView };
const UFOLoadHoldWrapper = ({
  children
}) => /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(UFOHoldLoad, {
  name: "smart-card-inline-card"
}), children);
export const InlineCard = ({
  id,
  url,
  cardState,
  handleAuthorize,
  handleFrameClick,
  isSelected,
  isHovered,
  renderers,
  onResolve,
  onError,
  testId,
  inlinePreloaderStyle,
  showHoverPreview,
  hoverPreviewOptions,
  actionOptions,
  removeTextHighlightingFromTitle,
  resolvingPlaceholder,
  truncateInline,
  hideIconLoadingSkeleton
}) => {
  var _details$meta2, _details$meta3, _providerForbidden$te;
  const {
    createAnalyticsEvent
  } = useAnalyticsEvents();
  const {
    status,
    details
  } = cardState;
  const extensionKey = getExtensionKey(details);
  const testIdWithStatus = testId ? `${testId}-${status}-view` : undefined;
  const resolvedProps = status === SmartLinkStatus.Resolved ? extractInlineProps(details, renderers, removeTextHighlightingFromTitle, false) : {};
  useEffect(() => {
    var _details$meta;
    switch (status) {
      case SmartLinkStatus.Resolved:
        onResolve === null || onResolve === void 0 ? void 0 : onResolve({
          url,
          title: resolvedProps.title,
          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, url, resolvedProps.title, details === null || details === void 0 ? void 0 : (_details$meta2 = details.meta) === null || _details$meta2 === void 0 ? void 0 : _details$meta2.key]);
  switch (status) {
    case 'pending':
    case 'resolving':
      return /*#__PURE__*/React.createElement(UFOLoadHoldWrapper, null, /*#__PURE__*/React.createElement(InlineCardResolvingView, {
        url: url,
        isSelected: isSelected,
        onClick: handleFrameClick,
        testId: testIdWithStatus,
        inlinePreloaderStyle: inlinePreloaderStyle,
        resolvingPlaceholder: resolvingPlaceholder,
        truncateInline: truncateInline
      }));
    case 'resolved':
      return /*#__PURE__*/React.createElement(InlineCardResolvedView, _extends({}, resolvedProps, {
        id: id,
        showHoverPreview: showHoverPreview,
        hoverPreviewOptions: hoverPreviewOptions,
        actionOptions: actionOptions,
        link: url,
        isSelected: isSelected,
        isHovered: isHovered,
        onClick: handleFrameClick,
        testId: testIdWithStatus,
        truncateInline: truncateInline,
        hideIconLoadingSkeleton: hideIconLoadingSkeleton
      }));
    case 'unauthorized':
      const provider = extractSmartLinkProvider(details);
      return /*#__PURE__*/React.createElement(InlineCardUnauthorizedView, {
        icon: provider && provider.icon,
        context: provider && provider.text,
        url: url,
        isSelected: isSelected,
        onClick: handleFrameClick,
        onAuthorise: handleAuthorize,
        testId: testIdWithStatus,
        showHoverPreview: showHoverPreview,
        id: id,
        extensionKey: extensionKey,
        truncateInline: truncateInline
      });
    case 'forbidden':
      const providerForbidden = extractSmartLinkProvider(details);
      const cardMetadata = (_details$meta3 = details === null || details === void 0 ? void 0 : details.meta) !== null && _details$meta3 !== void 0 ? _details$meta3 : getForbiddenJsonLd().meta;
      const requestAccessContext = extractRequestAccessContextImproved({
        jsonLd: cardMetadata,
        url,
        product: (_providerForbidden$te = providerForbidden === null || providerForbidden === void 0 ? void 0 : providerForbidden.text) !== null && _providerForbidden$te !== void 0 ? _providerForbidden$te : '',
        createAnalyticsEvent
      });
      return /*#__PURE__*/React.createElement(InlineCardForbiddenView, {
        url: url,
        icon: providerForbidden && providerForbidden.icon,
        context: providerForbidden === null || providerForbidden === void 0 ? void 0 : providerForbidden.text,
        isSelected: isSelected,
        onClick: handleFrameClick,
        onAuthorise: handleAuthorize,
        testId: testIdWithStatus,
        requestAccessContext: requestAccessContext,
        showHoverPreview: showHoverPreview,
        truncateInline: truncateInline
      });
    case 'not_found':
      const providerNotFound = extractSmartLinkProvider(details);
      return /*#__PURE__*/React.createElement(InlineCardErroredView, {
        url: url,
        icon: providerNotFound && providerNotFound.icon,
        isSelected: isSelected,
        message: "Can't find link",
        onClick: handleFrameClick,
        testId: testIdWithStatus || 'inline-card-not-found-view',
        showHoverPreview: showHoverPreview,
        truncateInline: truncateInline
      });
    case 'fallback':
    case 'errored':
    default:
      return /*#__PURE__*/React.createElement(CardLinkView, {
        link: url,
        isSelected: isSelected,
        onClick: handleFrameClick,
        testId: testIdWithStatus || 'inline-card-errored-view',
        truncateInline: truncateInline,
        viewType: "errored"
      });
  }
};