import { useEffect } from 'react';
/**
 *
 * A wrapper for popups to signal popupOpen user intent
 */
export var UserIntentPopupWrapper = function UserIntentPopupWrapper(_ref) {
  var _ref$userIntent = _ref.userIntent,
    userIntent = _ref$userIntent === void 0 ? 'popupOpen' : _ref$userIntent,
    children = _ref.children,
    api = _ref.api;
  useEffect(function () {
    var _api$userIntent;
    api === null || api === void 0 || api.core.actions.execute(api === null || api === void 0 || (_api$userIntent = api.userIntent) === null || _api$userIntent === void 0 ? void 0 : _api$userIntent.commands.setCurrentUserIntent(userIntent));
    return function () {
      var _api$userIntent2;
      if (userIntent === (api === null || api === void 0 || (_api$userIntent2 = api.userIntent) === null || _api$userIntent2 === void 0 || (_api$userIntent2 = _api$userIntent2.sharedState.currentState()) === null || _api$userIntent2 === void 0 ? void 0 : _api$userIntent2.currentUserIntent)) {
        // Defer the reset to avoid interfering with ongoing ProseMirror transactions
        // This fixes a race condition where cleanup happens during a transaction
        // (e.g., during drag handle mouse over -> unmountDecorations -> flushSync)
        setTimeout(function () {
          var _api$userIntent3;
          api === null || api === void 0 || api.core.actions.execute(api === null || api === void 0 || (_api$userIntent3 = api.userIntent) === null || _api$userIntent3 === void 0 ? void 0 : _api$userIntent3.commands.setCurrentUserIntent('default'));
        }, 0);
      }
    };

    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  return children;
};