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';
var UFOLoadHoldWrapper = function UFOLoadHoldWrapper(_ref) {
  var children = _ref.children;
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(UFOHoldLoad, {
    name: "smart-card-embed-card"
  }), children);
};
export var EmbedCard = /*#__PURE__*/React.forwardRef(function (_ref2, iframeRef) {
  var _details$meta2, _forbiddenViewProps$c, _forbiddenViewProps$c2, _notFoundViewProps$co, _notFoundViewProps$co2;
  var url = _ref2.url,
    cardState = _ref2.cardState,
    handleAuthorize = _ref2.handleAuthorize,
    handleErrorRetry = _ref2.handleErrorRetry,
    handleFrameClick = _ref2.handleFrameClick,
    handleFrameAuxClick = _ref2.handleFrameAuxClick,
    handleFrameContextMenu = _ref2.handleFrameContextMenu,
    isSelected = _ref2.isSelected,
    frameStyle = _ref2.frameStyle,
    platform = _ref2.platform,
    onResolve = _ref2.onResolve,
    onError = _ref2.onError,
    testId = _ref2.testId,
    inheritDimensions = _ref2.inheritDimensions,
    onIframeDwell = _ref2.onIframeDwell,
    onIframeFocus = _ref2.onIframeFocus,
    onIframeMouseEnter = _ref2.onIframeMouseEnter,
    onIframeMouseLeave = _ref2.onIframeMouseLeave,
    iframeUrlType = _ref2.iframeUrlType,
    actionOptions = _ref2.actionOptions,
    renderers = _ref2.renderers,
    CompetitorPrompt = _ref2.CompetitorPrompt,
    hideIconLoadingSkeleton = _ref2.hideIconLoadingSkeleton;
  var _useAnalyticsEvents = useAnalyticsEvents(),
    createAnalyticsEvent = _useAnalyticsEvents.createAnalyticsEvent;
  var status = cardState.status,
    details = cardState.details;
  var extensionKey = getExtensionKey(details);
  var isProductIntegrationSupported = hasAuthScopeOverrides(details);
  var _useControlDataExport = useControlDataExportConfig(),
    _useControlDataExport2 = _useControlDataExport.shouldControlDataExport,
    shouldControlDataExport = _useControlDataExport2 === void 0 ? false : _useControlDataExport2;
  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 ? "".concat(testId, "-resolving-view") : 'embed-card-resolving-view'
      }));
    case 'resolved':
      var resolvedViewProps = extractEmbedProps(details, platform, iframeUrlType);
      if (onResolve) {
        var _resolvedViewProps$pr, _details$meta;
        onResolve({
          title: resolvedViewProps.title,
          url: url,
          aspectRatio: (_resolvedViewProps$pr = resolvedViewProps.preview) === null || _resolvedViewProps$pr === void 0 ? void 0 : _resolvedViewProps$pr.aspectRatio,
          extensionKey: details === null || details === void 0 || (_details$meta = details.meta) === null || _details$meta === void 0 ? void 0 : _details$meta.key
        });
      }
      if (getIsDataExportEnabled(shouldControlDataExport, cardState.details)) {
        var 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
          // Support middle-/right-clicks for "resolved" only — the vast majority of clicks happen here.
          // Other states (e.g. "resolving") could be added later for completeness.
          ,
          onAuxClick: handleFrameAuxClick,
          onContextMenu: handleFrameContextMenu,
          ref: iframeRef,
          onIframeDwell: onIframeDwell,
          onIframeFocus: onIframeFocus,
          onIframeMouseEnter: onIframeMouseEnter,
          onIframeMouseLeave: onIframeMouseLeave,
          testId: testId,
          CompetitorPrompt: CompetitorPrompt,
          hideIconLoadingSkeleton: hideIconLoadingSkeleton,
          extensionKey: extensionKey
        }));
      } else {
        if (platform === 'mobile') {
          var 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
          // Support middle-/right-clicks for "resolved" only — the vast majority of clicks happen here.
          // Other states (e.g. "resolving") could be added later for completeness.
          ,
          onAuxClick: handleFrameAuxClick,
          onContextMenu: handleFrameContextMenu,
          onError: onError,
          onResolve: onResolve,
          renderers: renderers,
          actionOptions: actionOptions,
          testId: testId
        });
      }
    case 'unauthorized':
      if (onError) {
        onError({
          url: url,
          status: status
        });
      }
      var 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: url,
          status: status
        });
      }
      var forbiddenViewProps = extractEmbedProps(details, platform);
      var 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
        }));
      }
      var forbiddenAccessContext = extractRequestAccessContextImproved({
        jsonLd: cardMetadata,
        url: 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: 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: url,
          status: status
        });
      }
      var notFoundViewProps = extractEmbedProps(details, platform);
      var notFoundAccessContext = details !== null && details !== void 0 && details.meta ? extractRequestAccessContextImproved({
        jsonLd: details === null || details === void 0 ? void 0 : details.meta,
        url: 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: 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: url,
          status: status
        });
      }
      return /*#__PURE__*/React.createElement(EmbedCardErroredView, {
        onRetry: handleErrorRetry,
        inheritDimensions: inheritDimensions,
        isSelected: isSelected
      });
  }
});