"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.BlockCard = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireDefault(require("react"));
var _loadHold = _interopRequireDefault(require("@atlaskit/react-ufo/load-hold"));
var _useControlDataExportConfig = require("../../state/hooks/use-control-data-export-config");
var _shouldDataExport = require("../../utils/should-data-export");
var _utils = require("../common/utils");
var _LinkView = require("../LinkView");
var _ErroredView = _interopRequireDefault(require("./views/ErroredView"));
var _ForbiddenView = _interopRequireDefault(require("./views/ForbiddenView"));
var _NotFoundView = _interopRequireDefault(require("./views/NotFoundView"));
var _ResolvedView = _interopRequireDefault(require("./views/ResolvedView"));
var _UnauthorisedView = _interopRequireDefault(require("./views/UnauthorisedView"));
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-block-card"
  }), children);
};
var BlockCard = exports.BlockCard = function BlockCard(_ref2) {
  var id = _ref2.id,
    url = _ref2.url,
    cardState = _ref2.cardState,
    authFlow = _ref2.authFlow,
    handleAuthorize = _ref2.handleAuthorize,
    handleFrameClick = _ref2.handleFrameClick,
    handleFrameAuxClick = _ref2.handleFrameAuxClick,
    handleFrameContextMenu = _ref2.handleFrameContextMenu,
    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 (0, _utils.handleClickCommon)(event, handleFrameClick);
    },
    onAuxClick: handleFrameAuxClick,
    onContextMenu: handleFrameContextMenu,
    onError: onError,
    onResolve: onResolve,
    renderers: renderers,
    actionOptions: actionOptions,
    CompetitorPrompt: CompetitorPrompt,
    hideIconLoadingSkeleton: hideIconLoadingSkeleton
  };
  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, (0, _extends2.default)({}, blockCardProps, {
        testId: "smart-block-resolving-view"
      })));
    case 'resolved':
      if ((0, _shouldDataExport.getIsDataExportEnabled)(shouldControlDataExport, cardState.details)) {
        return /*#__PURE__*/_react.default.createElement(_UnauthorisedView.default, (0, _extends2.default)({}, blockCardProps, {
          onAuthorize: handleAuthorize
        }));
      }
      return /*#__PURE__*/_react.default.createElement(_ResolvedView.default, blockCardProps);
    case 'unauthorized':
      return /*#__PURE__*/_react.default.createElement(_UnauthorisedView.default, (0, _extends2.default)({}, blockCardProps, {
        onAuthorize: handleAuthorize
      }));
    case 'forbidden':
      return /*#__PURE__*/_react.default.createElement(_ForbiddenView.default, (0, _extends2.default)({}, blockCardProps, {
        onAuthorize: handleAuthorize
      }));
    case 'not_found':
      return /*#__PURE__*/_react.default.createElement(_NotFoundView.default, (0, _extends2.default)({}, blockCardProps, {
        onAuthorize: handleAuthorize
      }));
    case 'fallback':
    case 'errored':
    default:
      if (onError) {
        onError({
          url: url,
          status: status
        });
      }
      if (authFlow && authFlow === 'disabled') {
        return /*#__PURE__*/_react.default.createElement(_LinkView.CardLinkView, {
          link: url,
          isSelected: isSelected,
          onClick: handleFrameClick,
          testId: "".concat(testId, "-").concat(status)
        });
      }
      return /*#__PURE__*/_react.default.createElement(_ErroredView.default, (0, _extends2.default)({}, blockCardProps, {
        onAuthorize: handleAuthorize
      }));
  }
};