"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.GeminiInteractionContext = GeminiInteractionContext;
exports.withWaitForItem = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _interactionContext = _interopRequireDefault(require("@atlaskit/react-ufo/interaction-context"));
var _loadHold = _interopRequireDefault(require("@atlaskit/react-ufo/load-hold"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
/**
 * Proxies `@atlaskit/react-ufo/interaction-context` context with hold markers
 * for Gemini
 * Copied from @atlassian/gemini as it is a privately scoped package
 */
function GeminiInteractionContext(_ref) {
  var children = _ref.children;
  var interactionCount = (0, _react.useRef)(0);
  var interactionTracker = _react.default.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.default.createElement(_interactionContext.default.Provider, {
    value: interactionTracker
  }, children);
}
var MockLoading = function MockLoading(_ref2) {
  var getItem = _ref2.getItem,
    onError = _ref2.onError;
  var _useState = (0, _react.useState)(true),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    isLoading = _useState2[0],
    setIsLoading = _useState2[1];
  var counter = (0, _react.useRef)(0);
  (0, _react.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.default.createElement(_loadHold.default, {
    name: "wait-for-item"
  }) : null;
};
var withGeminiInteractionContext = function withGeminiInteractionContext(Component) {
  return function (props) {
    return /*#__PURE__*/_react.default.createElement(GeminiInteractionContext, null, /*#__PURE__*/_react.default.createElement(Component, props));
  };
};
var withWaitForItem = exports.withWaitForItem = function withWaitForItem(Component, getItem) {
  return withGeminiInteractionContext(function (props) {
    var _useState3 = (0, _react.useState)(false),
      _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
      error = _useState4[0],
      setError = _useState4[1];
    var onError = (0, _react.useCallback)(function () {
      setError(true);
    }, []);
    if (error) {
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, "Item not found");
    }
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(MockLoading, {
      getItem: getItem,
      onError: onError
    }), /*#__PURE__*/_react.default.createElement(Component, props !== null && props !== void 0 ? props : {}));
  });
};