"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.EmbedCardUpdated = exports.EmbedCard = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _analyticsNext = require("@atlaskit/analytics-next");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _loadHold = _interopRequireDefault(require("@atlaskit/react-ufo/load-hold"));
var _extractAccessContext = require("../../extractors/common/context/extractAccessContext");
var _embed = require("../../extractors/embed");
var _inline = require("../../extractors/inline");
var _helpers = require("../../state/helpers");
var _useControlDataExportConfig = require("../../state/hooks/use-control-data-export-config");
var _jsonld = require("../../utils/jsonld");
var _shouldDataExport = require("../../utils/should-data-export");
var _ResolvedView = _interopRequireDefault(require("../BlockCard/views/ResolvedView"));
var _ResolvedView2 = require("../InlineCard/ResolvedView");
var _ErroredView = require("./views/ErroredView");
var _forbiddenView = _interopRequireDefault(require("./views/forbidden-view"));
var _notFoundView = _interopRequireDefault(require("./views/not-found-view"));
var _ResolvedView3 = require("./views/ResolvedView");
var _unauthorizedView = _interopRequireDefault(require("./views/unauthorized-view"));
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var UFOLoadHoldWrapper = function UFOLoadHoldWrapper(_ref) {
  var children = _ref.children;
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_loadHold.default, {
    name: "smart-card-embed-card"
  }), children);
};
var EmbedCard = exports.EmbedCard = /*#__PURE__*/_react.default.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,
    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,
    iframeUrlType = _ref2.iframeUrlType,
    actionOptions = _ref2.actionOptions,
    renderers = _ref2.renderers,
    CompetitorPrompt = _ref2.CompetitorPrompt,
    hideIconLoadingSkeleton = _ref2.hideIconLoadingSkeleton;
  var _useAnalyticsEvents = (0, _analyticsNext.useAnalyticsEvents)(),
    createAnalyticsEvent = _useAnalyticsEvents.createAnalyticsEvent;
  var status = cardState.status,
    details = cardState.details;
  var extensionKey = (0, _helpers.getExtensionKey)(details);
  var isProductIntegrationSupported = (0, _helpers.hasAuthScopeOverrides)(details);
  var _useControlDataExport = (0, _useControlDataExportConfig.useControlDataExportConfig)(),
    _useControlDataExport2 = _useControlDataExport.shouldControlDataExport,
    shouldControlDataExport = _useControlDataExport2 === void 0 ? false : _useControlDataExport2;
  switch (status) {
    case 'pending':
    case 'resolving':
      return /*#__PURE__*/_react.default.createElement(UFOLoadHoldWrapper, null, /*#__PURE__*/_react.default.createElement(_ResolvedView.default, {
        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 = (0, _embed.extractEmbedProps)(details, platform, iframeUrlType);
      if (onResolve) {
        var _resolvedViewProps$pr, _details$meta;
        onResolve(_objectSpread({
          title: resolvedViewProps.title,
          url: url,
          aspectRatio: (_resolvedViewProps$pr = resolvedViewProps.preview) === null || _resolvedViewProps$pr === void 0 ? void 0 : _resolvedViewProps$pr.aspectRatio
        }, (0, _platformFeatureFlags.fg)('expose-product-details-from-smart-card') && {
          extensionKey: details === null || details === void 0 || (_details$meta = details.meta) === null || _details$meta === void 0 ? void 0 : _details$meta.key
        }));
      }
      if ((0, _shouldDataExport.getIsDataExportEnabled)(shouldControlDataExport, cardState.details)) {
        var unauthViewProps = (0, _embed.extractEmbedProps)(details, platform);
        return /*#__PURE__*/_react.default.createElement(_unauthorizedView.default, {
          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.default.createElement(_ResolvedView3.EmbedCardResolvedView, (0, _extends2.default)({}, 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') {
          var resolvedInlineViewProps = (0, _inline.extractInlineProps)(details);
          return /*#__PURE__*/_react.default.createElement(_ResolvedView2.InlineCardResolvedView, (0, _extends2.default)({}, resolvedInlineViewProps, {
            isSelected: isSelected,
            testId: testId,
            onClick: handleFrameClick
          }));
        }
        return /*#__PURE__*/_react.default.createElement(_ResolvedView.default, {
          url: url,
          cardState: cardState,
          onClick: handleFrameClick,
          onError: onError,
          onResolve: onResolve,
          renderers: renderers,
          actionOptions: actionOptions,
          testId: testId
        });
      }
    case 'unauthorized':
      if (onError) {
        onError({
          url: url,
          status: status
        });
      }
      var unauthorisedViewProps = (0, _embed.extractEmbedProps)(details, platform);
      return /*#__PURE__*/_react.default.createElement(_unauthorizedView.default, {
        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 = (0, _embed.extractEmbedProps)(details, platform);
      var cardMetadata = (_details$meta2 = details === null || details === void 0 ? void 0 : details.meta) !== null && _details$meta2 !== void 0 ? _details$meta2 : (0, _jsonld.getForbiddenJsonLd)().meta;
      if (forbiddenViewProps.preview) {
        return /*#__PURE__*/_react.default.createElement(_ResolvedView3.EmbedCardResolvedView, (0, _extends2.default)({}, forbiddenViewProps, {
          title: forbiddenViewProps.link,
          frameStyle: frameStyle,
          isSelected: isSelected,
          inheritDimensions: inheritDimensions,
          onClick: handleFrameClick,
          ref: iframeRef,
          extensionKey: extensionKey
        }));
      }
      var forbiddenAccessContext = (0, _extractAccessContext.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.default.createElement(_forbiddenView.default, {
        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 = (0, _embed.extractEmbedProps)(details, platform);
      var notFoundAccessContext = details !== null && details !== void 0 && details.meta ? (0, _extractAccessContext.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.default.createElement(_notFoundView.default, {
        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.default.createElement(_ErroredView.EmbedCardErroredView, {
        onRetry: handleErrorRetry,
        inheritDimensions: inheritDimensions,
        isSelected: isSelected
      });
  }
});
var EmbedCardUpdated = exports.EmbedCardUpdated = /*#__PURE__*/_react.default.forwardRef(function (_ref3, iframeRef) {
  var _details$meta4, _forbiddenViewProps$c3, _forbiddenViewProps$c4, _notFoundViewProps$co3, _notFoundViewProps$co4;
  var url = _ref3.url,
    cardState = _ref3.cardState,
    handleAuthorize = _ref3.handleAuthorize,
    handleErrorRetry = _ref3.handleErrorRetry,
    handleFrameClick = _ref3.handleFrameClick,
    isSelected = _ref3.isSelected,
    frameStyle = _ref3.frameStyle,
    platform = _ref3.platform,
    onResolve = _ref3.onResolve,
    onError = _ref3.onError,
    testId = _ref3.testId,
    inheritDimensions = _ref3.inheritDimensions,
    onIframeDwell = _ref3.onIframeDwell,
    onIframeFocus = _ref3.onIframeFocus,
    onIframeMouseEnter = _ref3.onIframeMouseEnter,
    onIframeMouseLeave = _ref3.onIframeMouseLeave,
    iframeUrlType = _ref3.iframeUrlType,
    actionOptions = _ref3.actionOptions,
    renderers = _ref3.renderers,
    CompetitorPrompt = _ref3.CompetitorPrompt,
    hideIconLoadingSkeleton = _ref3.hideIconLoadingSkeleton;
  var _useAnalyticsEvents2 = (0, _analyticsNext.useAnalyticsEvents)(),
    createAnalyticsEvent = _useAnalyticsEvents2.createAnalyticsEvent;
  var status = cardState.status,
    details = cardState.details;
  var extensionKey = (0, _helpers.getExtensionKey)(details);
  var isProductIntegrationSupported = (0, _helpers.hasAuthScopeOverrides)(details);
  var _useControlDataExport3 = (0, _useControlDataExportConfig.useControlDataExportConfig)(),
    _useControlDataExport4 = _useControlDataExport3.shouldControlDataExport,
    shouldControlDataExport = _useControlDataExport4 === void 0 ? false : _useControlDataExport4;
  switch (status) {
    case 'pending':
    case 'resolving':
      return /*#__PURE__*/_react.default.createElement(UFOLoadHoldWrapper, null, /*#__PURE__*/_react.default.createElement(_ResolvedView.default, {
        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 = (0, _embed.extractEmbedProps)(details, platform, iframeUrlType);
      if (onResolve) {
        var _resolvedViewProps$pr2, _details$meta3;
        onResolve(_objectSpread({
          title: resolvedViewProps.title,
          url: url,
          aspectRatio: (_resolvedViewProps$pr2 = resolvedViewProps.preview) === null || _resolvedViewProps$pr2 === void 0 ? void 0 : _resolvedViewProps$pr2.aspectRatio
        }, (0, _platformFeatureFlags.fg)('expose-product-details-from-smart-card') && {
          extensionKey: details === null || details === void 0 || (_details$meta3 = details.meta) === null || _details$meta3 === void 0 ? void 0 : _details$meta3.key
        }));
      }
      if ((0, _shouldDataExport.getIsDataExportEnabled)(shouldControlDataExport, cardState.details)) {
        var unauthViewProps = (0, _embed.extractEmbedProps)(details, platform);
        return /*#__PURE__*/_react.default.createElement(_unauthorizedView.default, {
          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.default.createElement(_ResolvedView3.EmbedCardResolvedView, (0, _extends2.default)({}, 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') {
          var resolvedInlineViewProps = (0, _inline.extractInlineProps)(details);
          return /*#__PURE__*/_react.default.createElement(_ResolvedView2.InlineCardResolvedView, (0, _extends2.default)({}, resolvedInlineViewProps, {
            isSelected: isSelected,
            testId: testId,
            onClick: handleFrameClick
          }));
        }
        return /*#__PURE__*/_react.default.createElement(_ResolvedView.default, {
          url: url,
          cardState: cardState,
          onClick: handleFrameClick,
          onError: onError,
          onResolve: onResolve,
          renderers: renderers,
          actionOptions: actionOptions,
          testId: testId
        });
      }
    case 'unauthorized':
      if (onError) {
        onError({
          url: url,
          status: status
        });
      }
      var unauthorisedViewProps = (0, _embed.extractEmbedProps)(details, platform);
      return /*#__PURE__*/_react.default.createElement(_unauthorizedView.default, {
        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 = (0, _embed.extractEmbedProps)(details, platform);
      var cardMetadata = (_details$meta4 = details === null || details === void 0 ? void 0 : details.meta) !== null && _details$meta4 !== void 0 ? _details$meta4 : (0, _jsonld.getForbiddenJsonLd)().meta;
      if (forbiddenViewProps.preview) {
        return /*#__PURE__*/_react.default.createElement(_ResolvedView3.EmbedCardResolvedView, (0, _extends2.default)({}, forbiddenViewProps, {
          title: forbiddenViewProps.link,
          frameStyle: frameStyle,
          isSelected: isSelected,
          inheritDimensions: inheritDimensions,
          onClick: handleFrameClick,
          ref: iframeRef,
          extensionKey: extensionKey
        }));
      }
      var forbiddenAccessContext = (0, _extractAccessContext.extractRequestAccessContextImproved)({
        jsonLd: cardMetadata,
        url: 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: createAnalyticsEvent
      });
      return /*#__PURE__*/_react.default.createElement(_forbiddenView.default, {
        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 = (0, _embed.extractEmbedProps)(details, platform);
      var notFoundAccessContext = details !== null && details !== void 0 && details.meta ? (0, _extractAccessContext.extractRequestAccessContextImproved)({
        jsonLd: details === null || details === void 0 ? void 0 : details.meta,
        url: 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: createAnalyticsEvent
      }) : undefined;
      return /*#__PURE__*/_react.default.createElement(_notFoundView.default, {
        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.default.createElement(_ErroredView.EmbedCardErroredView, {
        onRetry: handleErrorRetry,
        inheritDimensions: inheritDimensions,
        isSelected: isSelected
      });
  }
});