"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { ConfirmDialog } from "./ConfirmDialog.js";
function DialogMountTracker({ trackerRef }) {
  React.useEffect(() => {
    trackerRef.current = true;
    return () => {
      trackerRef.current = false;
    };
  }, [trackerRef]);
  return null;
}
function useConfirmDialog() {
  const [open, setOpen] = React.useState(false);
  const [options, setOptions] = React.useState({});
  const [loading, setLoading] = React.useState(false);
  const resolveRef = React.useRef(null);
  const openRef = React.useRef(false);
  const isDialogElementRenderedRef = React.useRef(false);
  const queueRef = React.useRef([]);
  const processQueue = React.useCallback(() => {
    if (openRef.current) return;
    const next = queueRef.current.shift();
    if (!next) return;
    openRef.current = true;
    resolveRef.current = next.resolve;
    queueMicrotask(() => {
      setOptions(next.options || {});
      setOpen(true);
    });
  }, []);
  const finalizeInteraction = React.useCallback(() => {
    openRef.current = false;
    setOpen(false);
    queueMicrotask(() => {
      processQueue();
    });
  }, [processQueue]);
  const confirm = React.useCallback(
    (newOptions) => {
      return new Promise((resolve) => {
        if (process.env.NODE_ENV === "development" && !isDialogElementRenderedRef.current) {
          console.warn(
            "useConfirmDialog: confirm() was called but ConfirmDialogElement is not rendered. Add {ConfirmDialogElement} to your JSX."
          );
        }
        if (openRef.current || resolveRef.current) {
          queueRef.current.push({ options: newOptions, resolve });
          return;
        }
        openRef.current = true;
        resolveRef.current = resolve;
        queueMicrotask(() => {
          setOptions(newOptions || {});
          setOpen(true);
        });
      });
    },
    []
  );
  const handleConfirm = React.useCallback(async () => {
    setLoading(true);
    try {
      resolveRef.current?.(true);
      resolveRef.current = null;
    } finally {
      setLoading(false);
      finalizeInteraction();
    }
  }, [finalizeInteraction]);
  const handleCancel = React.useCallback(() => {
    resolveRef.current?.(false);
    resolveRef.current = null;
    finalizeInteraction();
  }, [finalizeInteraction]);
  const handleOpenChange = React.useCallback(
    (newOpen) => {
      if (!newOpen && openRef.current) {
        handleCancel();
      }
    },
    [handleCancel]
  );
  const ConfirmDialogElement = React.useMemo(
    () => /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(DialogMountTracker, { trackerRef: isDialogElementRenderedRef }),
      /* @__PURE__ */ jsx(
        ConfirmDialog,
        {
          open,
          onOpenChange: handleOpenChange,
          onConfirm: handleConfirm,
          title: options.title,
          text: options.text ?? options.description,
          confirmText: options.confirmText,
          cancelText: options.cancelText,
          variant: options.variant,
          loading
        }
      )
    ] }),
    [
      open,
      handleOpenChange,
      handleConfirm,
      options,
      loading
    ]
  );
  return {
    confirm,
    ConfirmDialogElement
  };
}
export {
  useConfirmDialog
};
//# sourceMappingURL=useConfirmDialog.js.map
