import React, { useCallback, useEffect, useRef, useState } from 'react';
import InteractionContext from '@atlaskit/react-ufo/interaction-context';
import UFOLoadHold from '@atlaskit/react-ufo/load-hold';

/**
 * Proxies `@atlaskit/react-ufo/interaction-context` context with hold markers
 * for Gemini
 * Copied from @atlassian/gemini as it is a privately scoped package
 */
export function GeminiInteractionContext({
  children
}) {
  let interactionCount = useRef(0);
  const interactionTracker = React.useMemo(() => ({
    hold: () => {
      // @ts-ignore ts(2339) --`__gemini_component_hold__` is injected by gemini's component loader
      window.__gemini_component_hold__ = true;
      // track UFOHold components as blocking
      interactionCount.current += 1;

      // unlock with timeout as new elements might come in place
      return () => {
        setTimeout(() => {
          interactionCount.current -= 1;
          if (interactionCount.current === 0) {
            // @ts-ignore ts(2339) --`__gemini_component_hold__` is injected by gemini's component loader
            window.__gemini_component_hold__ = false;
          }
        }, 0);
      };
    },
    tracePress: () => {},
    labelStack: [],
    segmentIdMap: new Map(),
    addMark: () => {},
    addCustomData: () => {},
    addCustomTimings: () => {},
    addApdex: () => {},
    onRender: () => {},
    retainQuery: () => {},
    addPreload: () => {},
    addLoad: () => {},
    _internalHold: () => {},
    complete: () => {}
  }), []);
  return /*#__PURE__*/React.createElement(InteractionContext.Provider, {
    value: interactionTracker
  }, children);
}
const MockLoading = ({
  getItem,
  onError
}) => {
  const [isLoading, setIsLoading] = useState(true);
  const counter = useRef(0);
  useEffect(() => {
    if (!isLoading) {
      return;
    }
    const interval = setInterval(() => {
      counter.current++;
      const item = getItem();
      if (item) {
        setIsLoading(false);
      } else {
        if (counter.current >= 20) {
          onError();
          throw new Error('Item not found');
        }
      }
    }, 100);
    return () => clearTimeout(interval);
  }, [onError, getItem, isLoading]);
  return isLoading ? /*#__PURE__*/React.createElement(UFOLoadHold, {
    name: "wait-for-item"
  }) : null;
};
const withGeminiInteractionContext = Component => props => /*#__PURE__*/React.createElement(GeminiInteractionContext, null, /*#__PURE__*/React.createElement(Component, props));
export const withWaitForItem = (Component, getItem) => withGeminiInteractionContext(props => {
  const [error, setError] = useState(false);
  const onError = useCallback(() => {
    setError(true);
  }, []);
  if (error) {
    return /*#__PURE__*/React.createElement(React.Fragment, null, "Item not found");
  }
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(MockLoading, {
    getItem: getItem,
    onError: onError
  }), /*#__PURE__*/React.createElement(Component, props !== null && props !== void 0 ? props : {}));
});