'use strict';

var ABQUS43J = require('./ABQUS43J.cjs');
var PBFD2E7P = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/PBFD2E7P.cjs');
var BCALMBPZ = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/BCALMBPZ.cjs');
var React = require('react');
var jsxRuntime = require('../../../../../../react@18.2.0/node_modules/react/jsx-runtime.cjs');

var NestedDialogsContext = React.createContext({});
function useNestedDialogs(store) {
  const context = React.useContext(NestedDialogsContext);
  const [dialogs, setDialogs] = React.useState([]);
  const add = React.useCallback(
    (dialog) => {
      var _a;
      setDialogs((dialogs2) => [...dialogs2, dialog]);
      return PBFD2E7P.chain((_a = context.add) == null ? void 0 : _a.call(context, dialog), () => {
        setDialogs((dialogs2) => dialogs2.filter((d) => d !== dialog));
      });
    },
    [context]
  );
  ABQUS43J.useSafeLayoutEffect(() => {
    return BCALMBPZ.sync(store, ["open", "contentElement"], (state) => {
      var _a;
      if (!state.open) return;
      if (!state.contentElement) return;
      return (_a = context.add) == null ? void 0 : _a.call(context, store);
    });
  }, [store, context]);
  const providerValue = React.useMemo(() => ({ store, add }), [store, add]);
  const wrapElement = React.useCallback(
    (element) => /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(NestedDialogsContext.Provider, { value: providerValue, children: element }),
    [providerValue]
  );
  return { wrapElement, nestedDialogs: dialogs };
}

exports.useNestedDialogs = useNestedDialogs;
//# sourceMappingURL=TOU75OXH.cjs.map
