import _extends from "@babel/runtime/helpers/extends";
import React from 'react';
import { useAnalyticsEvents } from '@atlaskit/analytics-next';
import UFOHoldLoad from '@atlaskit/react-ufo/load-hold';
import { extractRequestAccessContextImproved } from '../../extractors/common/context/extractAccessContext';
import { extractEmbedProps } from '../../extractors/embed';
import { extractInlineProps } from '../../extractors/inline';
import { getExtensionKey, hasAuthScopeOverrides } from '../../state/helpers';
import { useControlDataExportConfig } from '../../state/hooks/use-control-data-export-config';
import { getForbiddenJsonLd } from '../../utils/jsonld';
import { getIsDataExportEnabled } from '../../utils/should-data-export';
import BlockCardResolvedView from '../BlockCard/views/ResolvedView';
import { InlineCardResolvedView } from '../InlineCard/ResolvedView';
import { EmbedCardErroredView } from './views/ErroredView';
import ForbiddenView from './views/forbidden-view';
import NotFoundView from './views/not-found-view';
import { EmbedCardResolvedView } from './views/ResolvedView';
import UnauthorizedView from './views/unauthorized-view';
const UFOLoadHoldWrapper = ({
  children
}) => /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(UFOHoldLoad, {
  name: "smart-card-embed-card"
}), children);
export const EmbedCard = /*#__PURE__*/React.forwardRef(({
  url,
  cardState,
  handleAuthorize,
  handleErrorRetry,
  handleFrameClick,
  isSelected,
  frameStyle,
  platform,
  onResolve,
  onError,
  testId,
  inheritDimensions,
  onIframeDwell,
  onIframeFocus,
  iframeUrlType,
  actionOptions,
  renderers,
  CompetitorPrompt,
  hideIconLoadingSkeleton
}, iframeRef) => {
  var _details$meta2, _forbiddenViewProps$c, _forbiddenViewProps$c2, _notFoundViewProps$co, _notFoundViewProps$co2;
  const {
    createAnalyticsEvent
  } = useAnalyticsEvents();
  const {
    status,
    details
  } = cardState;
  const extensionKey = getExtensionKey(details);
  const isProductIntegrationSupported = hasAuthScopeOverrides(details);
  const {
    shouldControlDataExport = false
  } = useControlDataExportConfig();
  switch (status) {
    case 'pending':
    case 'resolving':
      return /*#__PURE__*/React.createElement(UFOLoadHoldWrapper, null, /*#__PURE__*/React.createElement(BlockCardResolvedView, {
        url: url,
        cardState: cardState,
        onClick: handleFrameClick,
        onError: onError,
        onResolve: onResolve,
        renderers: renderers,
        actionOptions: actionOptions,
        testId: testId ? `${testId}-resolving-view` : 'embed-card-resolving-view'
      }));
    case 'resolved':
      const resolvedViewProps = extractEmbedProps(details, platform, iframeUrlType);
      if (onResolve) {
        var _resolvedViewProps$pr, _details$meta;
        onResolve({
          title: resolvedViewProps.title,
          url,
          aspectRatio: (_resolvedViewProps$pr = resolvedViewProps.preview) === null || _resolvedViewProps$pr === void 0 ? void 0 : _resolvedViewProps$pr.aspectRatio,
          extensionKey: details === null || details === void 0 ? void 0 : (_details$meta = details.meta) === null || _details$meta === void 0 ? void 0 : _details$meta.key
        });
      }
      if (getIsDataExportEnabled(shouldControlDataExport, cardState.details)) {
        const unauthViewProps = extractEmbedProps(details, platform);
        return /*#__PURE__*/React.createElement(UnauthorizedView, {
          context: unauthViewProps.context,
          extensionKey: extensionKey,
          frameStyle: frameStyle,
          isProductIntegrationSupported: isProductIntegrationSupported,
          inheritDimensions: inheritDimensions,
          isSelected: isSelected,
          onAuthorize: handleAuthorize,
          onClick: handleFrameClick,
          testId: testId,
          url: unauthViewProps.link
        });
      }
      if (resolvedViewProps.preview) {
        return /*#__PURE__*/React.createElement(EmbedCardResolvedView, _extends({}, resolvedViewProps, {
          isSelected: isSelected,
          frameStyle: frameStyle,
          inheritDimensions: inheritDimensions,
          onClick: handleFrameClick,
          ref: iframeRef,
          onIframeDwell: onIframeDwell,
          onIframeFocus: onIframeFocus,
          testId: testId,
          CompetitorPrompt: CompetitorPrompt,
          hideIconLoadingSkeleton: hideIconLoadingSkeleton,
          extensionKey: extensionKey
        }));
      } else {
        if (platform === 'mobile') {
          const resolvedInlineViewProps = extractInlineProps(details);
          return /*#__PURE__*/React.createElement(InlineCardResolvedView, _extends({}, resolvedInlineViewProps, {
            isSelected: isSelected,
            testId: testId,
            onClick: handleFrameClick
          }));
        }
        return /*#__PURE__*/React.createElement(BlockCardResolvedView, {
          url: url,
          cardState: cardState,
          onClick: handleFrameClick,
          onError: onError,
          onResolve: onResolve,
          renderers: renderers,
          actionOptions: actionOptions,
          testId: testId
        });
      }
    case 'unauthorized':
      if (onError) {
        onError({
          url,
          status
        });
      }
      const unauthorisedViewProps = extractEmbedProps(details, platform);
      return /*#__PURE__*/React.createElement(UnauthorizedView, {
        context: unauthorisedViewProps.context,
        extensionKey: extensionKey,
        frameStyle: frameStyle,
        isProductIntegrationSupported: isProductIntegrationSupported,
        inheritDimensions: inheritDimensions,
        isSelected: isSelected,
        onAuthorize: handleAuthorize,
        onClick: handleFrameClick,
        testId: testId,
        url: unauthorisedViewProps.link
      });
    case 'forbidden':
      if (onError) {
        onError({
          url,
          status
        });
      }
      const forbiddenViewProps = extractEmbedProps(details, platform);
      const cardMetadata = (_details$meta2 = details === null || details === void 0 ? void 0 : details.meta) !== null && _details$meta2 !== void 0 ? _details$meta2 : getForbiddenJsonLd().meta;
      if (forbiddenViewProps.preview) {
        return /*#__PURE__*/React.createElement(EmbedCardResolvedView, _extends({}, forbiddenViewProps, {
          title: forbiddenViewProps.link,
          frameStyle: frameStyle,
          isSelected: isSelected,
          inheritDimensions: inheritDimensions,
          onClick: handleFrameClick,
          ref: iframeRef,
          extensionKey: extensionKey
        }));
      }
      const forbiddenAccessContext = extractRequestAccessContextImproved({
        jsonLd: cardMetadata,
        url,
        product: (_forbiddenViewProps$c = (_forbiddenViewProps$c2 = forbiddenViewProps.context) === null || _forbiddenViewProps$c2 === void 0 ? void 0 : _forbiddenViewProps$c2.text) !== null && _forbiddenViewProps$c !== void 0 ? _forbiddenViewProps$c : '',
        createAnalyticsEvent
      });
      return /*#__PURE__*/React.createElement(ForbiddenView, {
        context: forbiddenViewProps.context,
        frameStyle: frameStyle,
        inheritDimensions: inheritDimensions,
        isSelected: isSelected,
        onAuthorize: handleAuthorize,
        onClick: handleFrameClick,
        accessContext: forbiddenAccessContext,
        url: forbiddenViewProps.link
      });
    case 'not_found':
      if (onError) {
        onError({
          url,
          status
        });
      }
      const notFoundViewProps = extractEmbedProps(details, platform);
      const notFoundAccessContext = details !== null && details !== void 0 && details.meta ? extractRequestAccessContextImproved({
        jsonLd: details === null || details === void 0 ? void 0 : details.meta,
        url,
        product: (_notFoundViewProps$co = (_notFoundViewProps$co2 = notFoundViewProps.context) === null || _notFoundViewProps$co2 === void 0 ? void 0 : _notFoundViewProps$co2.text) !== null && _notFoundViewProps$co !== void 0 ? _notFoundViewProps$co : '',
        createAnalyticsEvent
      }) : undefined;
      return /*#__PURE__*/React.createElement(NotFoundView, {
        context: notFoundViewProps.context,
        frameStyle: frameStyle,
        inheritDimensions: inheritDimensions,
        isSelected: isSelected,
        onClick: handleFrameClick,
        accessContext: notFoundAccessContext,
        url: notFoundViewProps.link
      });
    case 'fallback':
    case 'errored':
    default:
      if (onError) {
        onError({
          url,
          status
        });
      }
      return /*#__PURE__*/React.createElement(EmbedCardErroredView, {
        onRetry: handleErrorRetry,
        inheritDimensions: inheritDimensions,
        isSelected: isSelected
      });
  }
});
export const EmbedCardUpdated = /*#__PURE__*/React.forwardRef(({
  url,
  cardState,
  handleAuthorize,
  handleErrorRetry,
  handleFrameClick,
  isSelected,
  frameStyle,
  platform,
  onResolve,
  onError,
  testId,
  inheritDimensions,
  onIframeDwell,
  onIframeFocus,
  onIframeMouseEnter,
  onIframeMouseLeave,
  iframeUrlType,
  actionOptions,
  renderers,
  CompetitorPrompt,
  hideIconLoadingSkeleton
}, iframeRef) => {
  var _details$meta4, _forbiddenViewProps$c3, _forbiddenViewProps$c4, _notFoundViewProps$co3, _notFoundViewProps$co4;
  const {
    createAnalyticsEvent
  } = useAnalyticsEvents();
  const {
    status,
    details
  } = cardState;
  const extensionKey = getExtensionKey(details);
  const isProductIntegrationSupported = hasAuthScopeOverrides(details);
  const {
    shouldControlDataExport = false
  } = useControlDataExportConfig();
  switch (status) {
    case 'pending':
    case 'resolving':
      return /*#__PURE__*/React.createElement(UFOLoadHoldWrapper, null, /*#__PURE__*/React.createElement(BlockCardResolvedView, {
        url: url,
        cardState: cardState,
        onClick: handleFrameClick,
        onError: onError,
        onResolve: onResolve,
        renderers: renderers,
        actionOptions: actionOptions,
        testId: testId ? `${testId}-resolving-view` : 'embed-card-resolving-view'
      }));
    case 'resolved':
      const resolvedViewProps = extractEmbedProps(details, platform, iframeUrlType);
      if (onResolve) {
        var _resolvedViewProps$pr2, _details$meta3;
        onResolve({
          title: resolvedViewProps.title,
          url,
          aspectRatio: (_resolvedViewProps$pr2 = resolvedViewProps.preview) === null || _resolvedViewProps$pr2 === void 0 ? void 0 : _resolvedViewProps$pr2.aspectRatio,
          extensionKey: details === null || details === void 0 ? void 0 : (_details$meta3 = details.meta) === null || _details$meta3 === void 0 ? void 0 : _details$meta3.key
        });
      }
      if (getIsDataExportEnabled(shouldControlDataExport, cardState.details)) {
        const unauthViewProps = extractEmbedProps(details, platform);
        return /*#__PURE__*/React.createElement(UnauthorizedView, {
          context: unauthViewProps.context,
          extensionKey: extensionKey,
          frameStyle: frameStyle,
          isProductIntegrationSupported: isProductIntegrationSupported,
          inheritDimensions: inheritDimensions,
          isSelected: isSelected,
          onAuthorize: handleAuthorize,
          onClick: handleFrameClick,
          testId: testId,
          url: unauthViewProps.link
        });
      }
      if (resolvedViewProps.preview) {
        return /*#__PURE__*/React.createElement(EmbedCardResolvedView, _extends({}, resolvedViewProps, {
          isSelected: isSelected,
          frameStyle: frameStyle,
          inheritDimensions: inheritDimensions,
          onClick: handleFrameClick,
          ref: iframeRef,
          onIframeDwell: onIframeDwell,
          onIframeFocus: onIframeFocus,
          onIframeMouseEnter: onIframeMouseEnter,
          onIframeMouseLeave: onIframeMouseLeave,
          testId: testId,
          CompetitorPrompt: CompetitorPrompt,
          hideIconLoadingSkeleton: hideIconLoadingSkeleton,
          extensionKey: extensionKey
        }));
      } else {
        if (platform === 'mobile') {
          const resolvedInlineViewProps = extractInlineProps(details);
          return /*#__PURE__*/React.createElement(InlineCardResolvedView, _extends({}, resolvedInlineViewProps, {
            isSelected: isSelected,
            testId: testId,
            onClick: handleFrameClick
          }));
        }
        return /*#__PURE__*/React.createElement(BlockCardResolvedView, {
          url: url,
          cardState: cardState,
          onClick: handleFrameClick,
          onError: onError,
          onResolve: onResolve,
          renderers: renderers,
          actionOptions: actionOptions,
          testId: testId
        });
      }
    case 'unauthorized':
      if (onError) {
        onError({
          url,
          status
        });
      }
      const unauthorisedViewProps = extractEmbedProps(details, platform);
      return /*#__PURE__*/React.createElement(UnauthorizedView, {
        context: unauthorisedViewProps.context,
        extensionKey: extensionKey,
        frameStyle: frameStyle,
        isProductIntegrationSupported: isProductIntegrationSupported,
        inheritDimensions: inheritDimensions,
        isSelected: isSelected,
        onAuthorize: handleAuthorize,
        onClick: handleFrameClick,
        testId: testId,
        url: unauthorisedViewProps.link
      });
    case 'forbidden':
      if (onError) {
        onError({
          url,
          status
        });
      }
      const forbiddenViewProps = extractEmbedProps(details, platform);
      const cardMetadata = (_details$meta4 = details === null || details === void 0 ? void 0 : details.meta) !== null && _details$meta4 !== void 0 ? _details$meta4 : getForbiddenJsonLd().meta;
      if (forbiddenViewProps.preview) {
        return /*#__PURE__*/React.createElement(EmbedCardResolvedView, _extends({}, forbiddenViewProps, {
          title: forbiddenViewProps.link,
          frameStyle: frameStyle,
          isSelected: isSelected,
          inheritDimensions: inheritDimensions,
          onClick: handleFrameClick,
          ref: iframeRef,
          extensionKey: extensionKey
        }));
      }
      const forbiddenAccessContext = extractRequestAccessContextImproved({
        jsonLd: cardMetadata,
        url,
        product: (_forbiddenViewProps$c3 = (_forbiddenViewProps$c4 = forbiddenViewProps.context) === null || _forbiddenViewProps$c4 === void 0 ? void 0 : _forbiddenViewProps$c4.text) !== null && _forbiddenViewProps$c3 !== void 0 ? _forbiddenViewProps$c3 : '',
        createAnalyticsEvent
      });
      return /*#__PURE__*/React.createElement(ForbiddenView, {
        context: forbiddenViewProps.context,
        frameStyle: frameStyle,
        inheritDimensions: inheritDimensions,
        isSelected: isSelected,
        onAuthorize: handleAuthorize,
        onClick: handleFrameClick,
        accessContext: forbiddenAccessContext,
        url: forbiddenViewProps.link
      });
    case 'not_found':
      if (onError) {
        onError({
          url,
          status
        });
      }
      const notFoundViewProps = extractEmbedProps(details, platform);
      const notFoundAccessContext = details !== null && details !== void 0 && details.meta ? extractRequestAccessContextImproved({
        jsonLd: details === null || details === void 0 ? void 0 : details.meta,
        url,
        product: (_notFoundViewProps$co3 = (_notFoundViewProps$co4 = notFoundViewProps.context) === null || _notFoundViewProps$co4 === void 0 ? void 0 : _notFoundViewProps$co4.text) !== null && _notFoundViewProps$co3 !== void 0 ? _notFoundViewProps$co3 : '',
        createAnalyticsEvent
      }) : undefined;
      return /*#__PURE__*/React.createElement(NotFoundView, {
        context: notFoundViewProps.context,
        frameStyle: frameStyle,
        inheritDimensions: inheritDimensions,
        isSelected: isSelected,
        onClick: handleFrameClick,
        accessContext: notFoundAccessContext,
        url: notFoundViewProps.link
      });
    case 'fallback':
    case 'errored':
    default:
      if (onError) {
        onError({
          url,
          status
        });
      }
      return /*#__PURE__*/React.createElement(EmbedCardErroredView, {
        onRetry: handleErrorRetry,
        inheritDimensions: inheritDimensions,
        isSelected: isSelected
      });
  }
});