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 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();
  });
  const cardProps = {
    ...props,
    id
  };
  const ErrorFallbackComponent = cardProps.fallbackComponent;
  const errorBoundaryFallbackComponent = () => {
    if (ErrorFallbackComponent) {
      return /*#__PURE__*/React.createElement(ErrorFallbackComponent, null);
    }
    return /*#__PURE__*/React.createElement(LoadingCardLink, cardProps);
  };
  const Component = cardProps.appearance === '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();
  });
  const propsWithId = {
    ...props,
    id
  };
  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);
};