import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
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(_ref) {
  var children = _ref.children;
  var interactionCount = useRef(0);
  var interactionTracker = React.useMemo(function () {
    return {
      hold: function 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 function () {
          setTimeout(function () {
            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: function tracePress() {},
      labelStack: [],
      segmentIdMap: new Map(),
      addMark: function addMark() {},
      addCustomData: function addCustomData() {},
      addCustomTimings: function addCustomTimings() {},
      addApdex: function addApdex() {},
      onRender: function onRender() {},
      retainQuery: function retainQuery() {},
      addPreload: function addPreload() {},
      addLoad: function addLoad() {},
      _internalHold: function _internalHold() {},
      complete: function complete() {}
    };
  }, []);
  return /*#__PURE__*/React.createElement(InteractionContext.Provider, {
    value: interactionTracker
  }, children);
}
var MockLoading = function MockLoading(_ref2) {
  var getItem = _ref2.getItem,
    onError = _ref2.onError;
  var _useState = useState(true),
    _useState2 = _slicedToArray(_useState, 2),
    isLoading = _useState2[0],
    setIsLoading = _useState2[1];
  var counter = useRef(0);
  useEffect(function () {
    if (!isLoading) {
      return;
    }
    var interval = setInterval(function () {
      counter.current++;
      var item = getItem();
      if (item) {
        setIsLoading(false);
      } else {
        if (counter.current >= 20) {
          onError();
          throw new Error('Item not found');
        }
      }
    }, 100);
    return function () {
      return clearTimeout(interval);
    };
  }, [onError, getItem, isLoading]);
  return isLoading ? /*#__PURE__*/React.createElement(UFOLoadHold, {
    name: "wait-for-item"
  }) : null;
};
var withGeminiInteractionContext = function withGeminiInteractionContext(Component) {
  return function (props) {
    return /*#__PURE__*/React.createElement(GeminiInteractionContext, null, /*#__PURE__*/React.createElement(Component, props));
  };
};
export var withWaitForItem = function withWaitForItem(Component, getItem) {
  return withGeminiInteractionContext(function (props) {
    var _useState3 = useState(false),
      _useState4 = _slicedToArray(_useState3, 2),
      error = _useState4[0],
      setError = _useState4[1];
    var onError = useCallback(function () {
      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 : {}));
  });
};