import _extends from "@babel/runtime/helpers/extends";
import React from 'react';

// TODO: Package Owner - please fix:
// eslint-disable-next-line import/no-extraneous-dependencies
import UFOHoldLoad from '@atlaskit/react-ufo/load-hold';
import { useControlDataExportConfig } from '../../state/hooks/use-control-data-export-config';
import { getIsDataExportEnabled } from '../../utils/should-data-export';
import { handleClickCommon } from '../common/utils';
import { CardLinkView } from '../LinkView';
import ErroredView from './views/ErroredView';
import ForbiddenView from './views/ForbiddenView';
import NotFoundView from './views/NotFoundView';
import ResolvedView from './views/ResolvedView';
import UnauthorisedView from './views/UnauthorisedView';
var UFOLoadHoldWrapper = function UFOLoadHoldWrapper(_ref) {
  var children = _ref.children;
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(UFOHoldLoad, {
    name: "smart-card-block-card"
  }), children);
};
export var BlockCard = function BlockCard(_ref2) {
  var id = _ref2.id,
    url = _ref2.url,
    cardState = _ref2.cardState,
    authFlow = _ref2.authFlow,
    handleAuthorize = _ref2.handleAuthorize,
    handleFrameClick = _ref2.handleFrameClick,
    renderers = _ref2.renderers,
    isSelected = _ref2.isSelected,
    onResolve = _ref2.onResolve,
    onError = _ref2.onError,
    testId = _ref2.testId,
    actionOptions = _ref2.actionOptions,
    CompetitorPrompt = _ref2.CompetitorPrompt,
    hideIconLoadingSkeleton = _ref2.hideIconLoadingSkeleton;
  var status = cardState.status;
  var blockCardProps = {
    id: id,
    cardState: cardState,
    url: url,
    testId: testId,
    onClick: function onClick(event) {
      return handleClickCommon(event, handleFrameClick);
    },
    onError: onError,
    onResolve: onResolve,
    renderers: renderers,
    actionOptions: actionOptions,
    CompetitorPrompt: CompetitorPrompt,
    hideIconLoadingSkeleton: hideIconLoadingSkeleton
  };
  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(ResolvedView, _extends({}, blockCardProps, {
        testId: "smart-block-resolving-view"
      })));
    case 'resolved':
      if (getIsDataExportEnabled(shouldControlDataExport, cardState.details)) {
        return /*#__PURE__*/React.createElement(UnauthorisedView, _extends({}, blockCardProps, {
          onAuthorize: handleAuthorize
        }));
      }
      return /*#__PURE__*/React.createElement(ResolvedView, blockCardProps);
    case 'unauthorized':
      return /*#__PURE__*/React.createElement(UnauthorisedView, _extends({}, blockCardProps, {
        onAuthorize: handleAuthorize
      }));
    case 'forbidden':
      return /*#__PURE__*/React.createElement(ForbiddenView, _extends({}, blockCardProps, {
        onAuthorize: handleAuthorize
      }));
    case 'not_found':
      return /*#__PURE__*/React.createElement(NotFoundView, _extends({}, blockCardProps, {
        onAuthorize: handleAuthorize
      }));
    case 'fallback':
    case 'errored':
    default:
      if (onError) {
        onError({
          url: url,
          status: status
        });
      }
      if (authFlow && authFlow === 'disabled') {
        return /*#__PURE__*/React.createElement(CardLinkView, {
          link: url,
          isSelected: isSelected,
          onClick: handleFrameClick,
          testId: "".concat(testId, "-").concat(status)
        });
      }
      return /*#__PURE__*/React.createElement(ErroredView, _extends({}, blockCardProps, {
        onAuthorize: handleAuthorize
      }));
  }
};