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 { context } from './utils/analytics/analytics';
import { 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
export const CardSSR = 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))));
};