"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _linkProvider = require("@atlaskit/link-provider");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _loadHold = _interopRequireDefault(require("@atlaskit/react-ufo/load-hold"));
var _useAnalyticsEvents2 = require("../../common/analytics/generated/use-analytics-events");
var _constants = require("../../constants");
var _extractPlaceholderData = require("../../extractors/flexible/extract-placeholder-data");
var _flexibleUiContext = require("../../state/flexible-ui-context");
var _useAiSummaryConfig = require("../../state/hooks/use-ai-summary-config");
var _useResolve = _interopRequireDefault(require("../../state/hooks/use-resolve"));
var _useRovoConfig = _interopRequireDefault(require("../../state/hooks/use-rovo-config"));
var _container = _interopRequireDefault(require("./components/container"));
var _utils = require("./utils");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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 PENDING_LINK_STATUSES = [_constants.SmartLinkStatus.Pending, _constants.SmartLinkStatus.Resolving];

/**
 * This represents a Flexible Card: a link represented by a card with metadata.
 * This is the container in which all Flexible UI Blocks and Elements exist.
 * Note: TitleBlock is mandatory for a Flexible Card to render.
 * @see Container
 */
var FlexibleCard = function FlexibleCard(_ref) {
  var _details$meta2, _flexibleCardContext$;
  var _ref$appearance = _ref.appearance,
    appearance = _ref$appearance === void 0 ? 'flexible' : _ref$appearance,
    cardState = _ref.cardState,
    children = _ref.children,
    id = _ref.id,
    onAuthorize = _ref.onAuthorize,
    onClick = _ref.onClick,
    onError = _ref.onError,
    onResolve = _ref.onResolve,
    origin = _ref.origin,
    placeholderData = _ref.placeholderData,
    renderers = _ref.renderers,
    showHoverPreview = _ref.showHoverPreview,
    hoverPreviewOptions = _ref.hoverPreviewOptions,
    actionOptions = _ref.actionOptions,
    testId = _ref.testId,
    ui = _ref.ui,
    url = _ref.url;
  var aiSummaryConfig = (0, _useAiSummaryConfig.useAISummaryConfig)();
  var resolve = (0, _useResolve.default)();
  var _useSmartLinkContext = (0, _linkProvider.useSmartLinkContext)(),
    isPreviewPanelAvailable = _useSmartLinkContext.isPreviewPanelAvailable,
    openPreviewPanel = _useSmartLinkContext.openPreviewPanel,
    product = _useSmartLinkContext.product;
  var rovoConfig = (0, _platformFeatureFlags.fg)('platform_sl_3p_auth_rovo_action_kill_switch') || (0, _platformFeatureFlags.fg)('rovogrowth-640-inline-action-nudge-fg') ?
  // eslint-disable-next-line react-hooks/rules-of-hooks
  (0, _useRovoConfig.default)() : undefined;
  var _useAnalyticsEvents = (0, _useAnalyticsEvents2.useAnalyticsEvents)(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var cardType = cardState.status,
    details = cardState.details;
  var status = cardType;

  // if we have placeholder state it means we can internally use it
  // as temporary resolved data until the actual data comes back as one of the final statuses
  var placeholderCardState = (0, _react.useMemo)(function () {
    return PENDING_LINK_STATUSES.includes(status) && placeholderData && (0, _extractPlaceholderData.extractPlaceHolderCardState)(placeholderData);
  }, [placeholderData, status]);
  var placeHolderStatus = placeholderCardState ? placeholderCardState.status : undefined;
  var context = (0, _react.useMemo)(function () {
    return (0, _utils.getContextByStatus)(_objectSpread(_objectSpread({
      aiSummaryConfig: aiSummaryConfig,
      appearance: appearance,
      fireEvent: fireEvent,
      response: placeholderCardState ? placeholderCardState.details : details,
      id: id,
      onAuthorize: onAuthorize,
      onClick: onClick,
      origin: origin,
      renderers: renderers,
      resolve: resolve
    }, (0, _platformFeatureFlags.fg)('platform_sl_3p_auth_rovo_action_kill_switch') || (0, _platformFeatureFlags.fg)('rovogrowth-640-inline-action-nudge-fg') ? {
      product: product,
      rovoConfig: rovoConfig
    } : undefined), {}, {
      actionOptions: actionOptions,
      status: placeholderCardState ? placeHolderStatus : status,
      url: url,
      isPreviewPanelAvailable: isPreviewPanelAvailable,
      openPreviewPanel: openPreviewPanel
    }));
  }, [aiSummaryConfig, appearance, actionOptions, details, id, isPreviewPanelAvailable, onAuthorize, onClick, openPreviewPanel, origin, placeholderCardState, placeHolderStatus, product, renderers, resolve, rovoConfig, status, url, fireEvent]);
  var flexibleCardContext = (0, _react.useMemo)(function () {
    return {
      data: context,
      status: placeHolderStatus !== null && placeHolderStatus !== void 0 ? placeHolderStatus : status,
      ui: ui
    };
  }, [context, placeHolderStatus, status, ui]);
  var _ref2 = context || {},
    linkTitle = _ref2.linkTitle;
  var title = linkTitle === null || linkTitle === void 0 ? void 0 : linkTitle.text;
  (0, _react.useEffect)(function () {
    switch (status) {
      case _constants.SmartLinkStatus.Resolved:
        if (onResolve) {
          var _details$meta;
          onResolve({
            title: title,
            url: url,
            extensionKey: details === null || details === void 0 || (_details$meta = details.meta) === null || _details$meta === void 0 ? void 0 : _details$meta.key
          });
        }
        break;
      case _constants.SmartLinkStatus.Errored:
      case _constants.SmartLinkStatus.Fallback:
      case _constants.SmartLinkStatus.Forbidden:
      case _constants.SmartLinkStatus.NotFound:
      case _constants.SmartLinkStatus.Unauthorized:
        if (onError) {
          onError({
            status: status,
            url: url
          });
        }
        break;
    }
  }, [onError, onResolve, status, title, url, details === null || details === void 0 || (_details$meta2 = details.meta) === null || _details$meta2 === void 0 ? void 0 : _details$meta2.key]);
  var retry = (_flexibleCardContext$ = flexibleCardContext.data) === null || _flexibleCardContext$ === void 0 || (_flexibleCardContext$ = _flexibleCardContext$.actions) === null || _flexibleCardContext$ === void 0 ? void 0 : _flexibleCardContext$[_constants.InternalActionName.UnresolvedAction];
  return /*#__PURE__*/_react.default.createElement(_flexibleUiContext.FlexibleCardContext.Provider, {
    value: flexibleCardContext
  }, PENDING_LINK_STATUSES.includes(status) && !Boolean(placeholderCardState) && /*#__PURE__*/_react.default.createElement(_loadHold.default, {
    name: "smart-card-flexible-card"
  }), /*#__PURE__*/_react.default.createElement(_container.default, (0, _extends2.default)({
    testId: testId
  }, ui, {
    onClick: onClick,
    showHoverPreview: showHoverPreview,
    hoverPreviewOptions: hoverPreviewOptions,
    actionOptions: actionOptions,
    status: status,
    retry: retry
  }), children));
};
var _default = exports.default = FlexibleCard;