import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { componentWithFG } from '@atlaskit/platform-feature-flags-react';
import { usePrefetch } from '../../../state';
import { startUfoExperience } from '../../../state/analytics/ufoExperiences';
import useIntersectionObserver from '../../../state/hooks/use-intersection-observer';
import { useSmartLinkSeenEvent } from '../../../state/hooks/use-smart-link-seen-event';
import { shouldSample } from '../../../utils/shouldSample';
import CardLoaderWrapper from '../card-loader-wrapper';
import { CardWithUrlContent } from '../component';
import { LoadingCardLink } from './LoadingCardLink';

// This property enables the intersection observer to be run once the
// HTML element being observed is within `X` px of the target container it is
// being compared to. Since the default container is the `document`, we set this
// up to check once a Smart Link is within `X` px from the bottom of the viewport.
var ROOT_MARGIN_VERTICAL = '360px';
function LazyIntersectionObserverCardOld(props) {
  var ref = useRef(null);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isIntersecting = _useState2[0],
    setIsIntersecting = _useState2[1];
  var _useState3 = useState(shouldSample()),
    _useState4 = _slicedToArray(_useState3, 1),
    shouldSendRenderedUFOEvent = _useState4[0];
  var appearance = props.appearance,
    url = props.url,
    id = props.id;
  var prefetch = usePrefetch(url);
  var Component = appearance === 'inline' ? 'span' : 'div';
  var ComponentObserver = Component;
  var onIntersection = useCallback(function (entries, observer) {
    var isVisible = entries.some(function (entry) {
      return entry.isIntersecting;
    });
    if (isVisible) {
      if (shouldSendRenderedUFOEvent) {
        startUfoExperience('smart-link-rendered', id);
      }
      setIsIntersecting(true);
      observer.disconnect();
    } else {
      prefetch();
    }
  }, [id, prefetch, shouldSendRenderedUFOEvent]);
  useEffect(function () {
    if (!ref.current) {
      return;
    }
    var intersectionObserver = new IntersectionObserver(onIntersection, {
      rootMargin: "".concat(ROOT_MARGIN_VERTICAL, " 0px ").concat(ROOT_MARGIN_VERTICAL, " 0px")
    });
    intersectionObserver.observe(ref.current);
    return function () {
      return intersectionObserver.disconnect();
    };
  }, [ref, onIntersection]);
  var content = isIntersecting ? /*#__PURE__*/React.createElement(CardWithUrlContent, props) : /*#__PURE__*/React.createElement(ComponentObserver, {
    ref: ref
  }, /*#__PURE__*/React.createElement(LoadingCardLink, props));

  // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
  return /*#__PURE__*/React.createElement(Component, {
    className: "loader-wrapper"
  }, content);
}
var LazyIntersectionObserverCardNew = function LazyIntersectionObserverCardNew(props) {
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    isIntersected = _useState6[0],
    setIsIntersected = _useState6[1];
  var _useState7 = useState(shouldSample()),
    _useState8 = _slicedToArray(_useState7, 1),
    shouldSendRenderedUFOEvent = _useState8[0];
  var appearance = props.appearance,
    children = props.children,
    id = props.id,
    onErrorCallback = props.onError,
    ui = props.ui,
    url = props.url;
  var prefetch = usePrefetch(url);
  var ComponentObserver = appearance === 'inline' ? 'span' : 'div';
  var _useSmartLinkSeenEven = useSmartLinkSeenEvent({
      appearance: appearance,
      children: children,
      id: id,
      ui: ui,
      url: url
    }),
    onSeenIntersecting = _useSmartLinkSeenEven.onIntersecting,
    onStatusSettled = _useSmartLinkSeenEven.onStatusSettled;
  var onError = useCallback(function (data) {
    if (data !== null && data !== void 0 && data.status) {
      onStatusSettled(data.status);
    }
    onErrorCallback === null || onErrorCallback === void 0 || onErrorCallback(data);
  }, [onStatusSettled, onErrorCallback]);
  var onIntersection = useCallback(function (isIntersecting) {
    if (isIntersecting) {
      if (shouldSendRenderedUFOEvent) {
        startUfoExperience('smart-link-rendered', id);
      }
      onSeenIntersecting();
      setIsIntersected(true);
    } else {
      prefetch();
    }
  }, [id, onSeenIntersecting, prefetch, shouldSendRenderedUFOEvent]);
  var ref = useIntersectionObserver({
    onIntersection: onIntersection
  });
  var content = isIntersected ? /*#__PURE__*/React.createElement(CardWithUrlContent, _extends({}, props, {
    onError: onError
  })) : /*#__PURE__*/React.createElement(ComponentObserver, {
    ref: ref
  }, /*#__PURE__*/React.createElement(LoadingCardLink, props));
  return /*#__PURE__*/React.createElement(CardLoaderWrapper, {
    appearance: appearance
  }, content);
};
export var LazyIntersectionObserverCard = componentWithFG('platform_sl_event_ui_seen', LazyIntersectionObserverCardNew, LazyIntersectionObserverCardOld);