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 } from 'react';
import { useAnalyticsEvents } from '@atlaskit/analytics-next';
import { extractSmartLinkProvider } from '@atlaskit/link-extractors';
import { fg } from '@atlaskit/platform-feature-flags';
import UFOHoldLoad from '@atlaskit/react-ufo/load-hold';
import { SmartLinkStatus } from '../../constants';
import { extractRequestAccessContextImproved } from '../../extractors/common/context/extractAccessContext';
import { extractInlineProps } from '../../extractors/inline';
import { getExtensionKey } from '../../state/helpers';
import { getForbiddenJsonLd } from '../../utils/jsonld';
import { CardLinkView } from '../LinkView';
import { InlineCardErroredView } from './ErroredView';
import { InlineCardForbiddenView } from './ForbiddenView';
import { InlineCardResolvedView } from './ResolvedView';
import { InlineCardResolvingView } from './ResolvingView';
import { InlineCardUnauthorizedView } from './UnauthorisedView';
export { InlineCardResolvedView, InlineCardResolvingView, InlineCardErroredView, InlineCardForbiddenView, InlineCardUnauthorizedView };
var UFOLoadHoldWrapper = function UFOLoadHoldWrapper(_ref) {
  var children = _ref.children;
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(UFOHoldLoad, {
    name: "smart-card-inline-card"
  }), children);
};
export var InlineCard = function InlineCard(_ref2) {
  var _details$meta2, _details$meta3, _providerForbidden$te;
  var id = _ref2.id,
    url = _ref2.url,
    cardState = _ref2.cardState,
    handleAuthorize = _ref2.handleAuthorize,
    handleFrameClick = _ref2.handleFrameClick,
    isSelected = _ref2.isSelected,
    isHovered = _ref2.isHovered,
    renderers = _ref2.renderers,
    onResolve = _ref2.onResolve,
    onError = _ref2.onError,
    testId = _ref2.testId,
    inlinePreloaderStyle = _ref2.inlinePreloaderStyle,
    showHoverPreview = _ref2.showHoverPreview,
    hoverPreviewOptions = _ref2.hoverPreviewOptions,
    actionOptions = _ref2.actionOptions,
    removeTextHighlightingFromTitle = _ref2.removeTextHighlightingFromTitle,
    resolvingPlaceholder = _ref2.resolvingPlaceholder,
    truncateInline = _ref2.truncateInline,
    hideIconLoadingSkeleton = _ref2.hideIconLoadingSkeleton;
  var _useAnalyticsEvents = useAnalyticsEvents(),
    createAnalyticsEvent = _useAnalyticsEvents.createAnalyticsEvent;
  var status = cardState.status,
    details = cardState.details;
  var extensionKey = getExtensionKey(details);
  var testIdWithStatus = testId ? "".concat(testId, "-").concat(status, "-view") : undefined;
  var resolvedProps = status === SmartLinkStatus.Resolved ? extractInlineProps(details, renderers, removeTextHighlightingFromTitle, false) : {};
  useEffect(function () {
    var _details$meta;
    switch (status) {
      case SmartLinkStatus.Resolved:
        onResolve === null || onResolve === void 0 || onResolve(_objectSpread({
          url: url,
          title: resolvedProps.title
        }, 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
        }));
        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, url, resolvedProps.title, details === null || details === void 0 || (_details$meta2 = details.meta) === null || _details$meta2 === void 0 ? void 0 : _details$meta2.key]);
  switch (status) {
    case 'pending':
    case 'resolving':
      return /*#__PURE__*/React.createElement(UFOLoadHoldWrapper, null, /*#__PURE__*/React.createElement(InlineCardResolvingView, {
        url: url,
        isSelected: isSelected,
        onClick: handleFrameClick,
        testId: testIdWithStatus,
        inlinePreloaderStyle: inlinePreloaderStyle,
        resolvingPlaceholder: resolvingPlaceholder,
        truncateInline: truncateInline
      }));
    case 'resolved':
      return /*#__PURE__*/React.createElement(InlineCardResolvedView, _extends({}, resolvedProps, {
        id: id,
        showHoverPreview: showHoverPreview,
        hoverPreviewOptions: hoverPreviewOptions,
        actionOptions: actionOptions,
        link: url,
        isSelected: isSelected,
        isHovered: isHovered,
        onClick: handleFrameClick,
        testId: testIdWithStatus,
        truncateInline: truncateInline,
        hideIconLoadingSkeleton: hideIconLoadingSkeleton
      }));
    case 'unauthorized':
      var provider = extractSmartLinkProvider(details);
      return /*#__PURE__*/React.createElement(InlineCardUnauthorizedView, {
        icon: provider && provider.icon,
        context: provider && provider.text,
        url: url,
        isSelected: isSelected,
        onClick: handleFrameClick,
        onAuthorise: handleAuthorize,
        testId: testIdWithStatus,
        showHoverPreview: showHoverPreview,
        id: id,
        extensionKey: extensionKey,
        truncateInline: truncateInline
      });
    case 'forbidden':
      var providerForbidden = extractSmartLinkProvider(details);
      var cardMetadata = (_details$meta3 = details === null || details === void 0 ? void 0 : details.meta) !== null && _details$meta3 !== void 0 ? _details$meta3 : getForbiddenJsonLd().meta;
      var requestAccessContext = extractRequestAccessContextImproved({
        jsonLd: cardMetadata,
        url: url,
        product: (_providerForbidden$te = providerForbidden === null || providerForbidden === void 0 ? void 0 : providerForbidden.text) !== null && _providerForbidden$te !== void 0 ? _providerForbidden$te : '',
        createAnalyticsEvent: createAnalyticsEvent
      });
      return /*#__PURE__*/React.createElement(InlineCardForbiddenView, {
        url: url,
        icon: providerForbidden && providerForbidden.icon,
        context: providerForbidden === null || providerForbidden === void 0 ? void 0 : providerForbidden.text,
        isSelected: isSelected,
        onClick: handleFrameClick,
        onAuthorise: handleAuthorize,
        testId: testIdWithStatus,
        requestAccessContext: requestAccessContext,
        showHoverPreview: showHoverPreview,
        truncateInline: truncateInline
      });
    case 'not_found':
      var providerNotFound = extractSmartLinkProvider(details);
      return /*#__PURE__*/React.createElement(InlineCardErroredView, {
        url: url,
        icon: providerNotFound && providerNotFound.icon,
        isSelected: isSelected,
        message: "Can't find link",
        onClick: handleFrameClick,
        testId: testIdWithStatus || 'inline-card-not-found-view',
        showHoverPreview: showHoverPreview,
        truncateInline: truncateInline
      });
    case 'fallback':
    case 'errored':
    default:
      return /*#__PURE__*/React.createElement(CardLinkView, _extends({
        link: url,
        isSelected: isSelected,
        onClick: handleFrameClick,
        testId: testIdWithStatus || 'inline-card-errored-view',
        truncateInline: truncateInline
      }, fg('navx-2565-inline-card-error-state-underline') ? {
        viewType: 'errored'
      } : {}));
  }
};