import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
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) { _defineProperty(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; }
import React, { useEffect, useMemo } from 'react';
import { useSmartLinkContext } from '@atlaskit/link-provider';
import { fg } from '@atlaskit/platform-feature-flags';
import UFOHoldLoad from '@atlaskit/react-ufo/load-hold';
import { useAnalyticsEvents } from '../../common/analytics/generated/use-analytics-events';
import { InternalActionName, SmartLinkStatus } from '../../constants';
import { extractPlaceHolderCardState } from '../../extractors/flexible/extract-placeholder-data';
import { FlexibleCardContext } from '../../state/flexible-ui-context';
import { useAISummaryConfig } from '../../state/hooks/use-ai-summary-config';
import useResolve from '../../state/hooks/use-resolve';
import useRovoConfig from '../../state/hooks/use-rovo-config';
import Container from './components/container';
import { getContextByStatus } from './utils';
var PENDING_LINK_STATUSES = [SmartLinkStatus.Pending, 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 = useAISummaryConfig();
  var resolve = useResolve();
  var _useSmartLinkContext = useSmartLinkContext(),
    isPreviewPanelAvailable = _useSmartLinkContext.isPreviewPanelAvailable,
    openPreviewPanel = _useSmartLinkContext.openPreviewPanel,
    product = _useSmartLinkContext.product;
  var rovoConfig = fg('platform_sl_3p_auth_rovo_action_kill_switch') ?
  // eslint-disable-next-line react-hooks/rules-of-hooks
  useRovoConfig() : undefined;
  var _useAnalyticsEvents = 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 = useMemo(function () {
    return PENDING_LINK_STATUSES.includes(status) && placeholderData && extractPlaceHolderCardState(placeholderData);
  }, [placeholderData, status]);
  var placeHolderStatus = placeholderCardState ? placeholderCardState.status : undefined;
  var context = useMemo(function () {
    return 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
    }, fg('platform_sl_3p_auth_rovo_action_kill_switch') ? {
      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 = 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;
  useEffect(function () {
    switch (status) {
      case 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 SmartLinkStatus.Errored:
      case SmartLinkStatus.Fallback:
      case SmartLinkStatus.Forbidden:
      case SmartLinkStatus.NotFound:
      case 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$[InternalActionName.UnresolvedAction];
  return /*#__PURE__*/React.createElement(FlexibleCardContext.Provider, {
    value: flexibleCardContext
  }, PENDING_LINK_STATUSES.includes(status) && !Boolean(placeholderCardState) && /*#__PURE__*/React.createElement(UFOHoldLoad, {
    name: "smart-card-flexible-card"
  }), /*#__PURE__*/React.createElement(Container, _extends({
    testId: testId
  }, ui, {
    onClick: onClick,
    showHoverPreview: showHoverPreview,
    hoverPreviewOptions: hoverPreviewOptions,
    actionOptions: actionOptions,
    status: status,
    retry: retry
  }), children));
};
export default FlexibleCard;