import React, { useState } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
// eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
import uuid from 'uuid';
import { AnalyticsContext } from '@atlaskit/analytics-next';
import { fg } from '@atlaskit/platform-feature-flags';
import { context } from './utils/analytics/analytics';
import { isFlexibleUiCard } from './utils/flexible';
import CardErrorBoundary from './view/CardWithUrl/card-error-boundary';
import { CardWithUrl, CardWithUrlContent } from './view/CardWithUrl/component';
import { LoadingCardLink } from './view/CardWithUrl/component-lazy/LoadingCardLink';
// SSR friendly version of smart-card
// simplifies the logic around rendering and loading placeholders and
// only contains whats necessary to render the card on SSR mode
const CardSSROld = props => {
  // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
  const [id] = useState(() => {
    var _props$id;
    return (_props$id = props.id) !== null && _props$id !== void 0 ? _props$id : uuid();
  });
  // FlexibleCards always need full ORS data regardless of appearance prop,
  // because they render custom blocks (TitleBlock etc.) requiring a complete response.
  // Override appearance to 'block' for FlexibleCards when FG is enabled.
  const effectiveAppearance = isFlexibleUiCard(props.children, props.ui) && fg('platform_smartlink_inline_resolve_optimization') ? 'block' : props.appearance;
  const cardProps = {
    ...props,
    id,
    appearance: effectiveAppearance
  };
  const ErrorFallbackComponent = cardProps.fallbackComponent;
  const errorBoundaryFallbackComponent = () => {
    if (ErrorFallbackComponent) {
      return /*#__PURE__*/React.createElement(ErrorFallbackComponent, null);
    }
    return /*#__PURE__*/React.createElement(LoadingCardLink, cardProps);
  };
  const Component = effectiveAppearance === 'inline' ? 'span' : 'div';
  return /*#__PURE__*/React.createElement(AnalyticsContext, {
    data: context
  }, /*#__PURE__*/React.createElement(ErrorBoundary, {
    FallbackComponent: errorBoundaryFallbackComponent
  }, /*#__PURE__*/React.createElement(Component, {
    className: "loader-wrapper"
  }, /*#__PURE__*/React.createElement(CardWithUrlContent, cardProps))));
};

// SSR friendly version of smart-card
// simplifies the logic around rendering and loading placeholders and
// only contains whats necessary to render the card on SSR mode
const CardSSRNew = props => {
  // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
  const [id] = useState(() => {
    var _props$id2;
    return (_props$id2 = props.id) !== null && _props$id2 !== void 0 ? _props$id2 : uuid();
  });
  // FlexibleCards always need full ORS data regardless of appearance prop,
  // because they render custom blocks (TitleBlock etc.) requiring a complete response.
  // Override appearance to 'block' for FlexibleCards when FG is enabled.
  const effectiveAppearance = isFlexibleUiCard(props.children, props.ui) && fg('platform_smartlink_inline_resolve_optimization') ? 'block' : props.appearance;
  const propsWithId = {
    ...props,
    id,
    appearance: effectiveAppearance
  };
  return /*#__PURE__*/React.createElement(AnalyticsContext, {
    data: context
  }, /*#__PURE__*/React.createElement(CardErrorBoundary, propsWithId, /*#__PURE__*/React.createElement(CardWithUrl, propsWithId)));
};
export const CardSSR = props => {
  if (fg('platform_sl_event_ui_seen')) {
    return /*#__PURE__*/React.createElement(CardSSRNew, props);
  }
  return /*#__PURE__*/React.createElement(CardSSROld, props);
};