import { jsx as k } from "@emotion/react/jsx-runtime";
import { useRef as n, useCallback as s, useEffect as v } from "react";
import { useWindowToolsMF as x } from "../../components/WindowBase/hooks/useWindowToolsMF/index.js";
import { useModal as F } from "../useModal/index.js";
import { WindowConfirm as M } from "../../components/WindowConfirm/WindowConfirm.js";
const G = ({
  confirmMessage: f,
  confirmTitle: l,
  isDirty: i,
  linkToCloseWindow: u = !1,
  onAfterClose: a,
  onCancelClose: m,
  onConfirmClose: C
}) => {
  const { openModal: d } = F(), { close: p, setFnQueryClose: w } = x(), R = n(i), g = n(a), o = n(m), e = n(C);
  R.current = i, g.current = a, o.current = m, e.current = C;
  const t = s((r) => {
    if (e.current) {
      e.current(r);
      return;
    }
    r ? r.proceed() : p(!1), g.current?.();
  }, [p]), h = s((r) => {
    if (o.current) {
      o.current(r);
      return;
    }
    r?.cancel();
  }, []), c = s((r) => {
    if (R.current) {
      d({
        window: /* @__PURE__ */ k(
          M,
          {
            variant: "warning",
            title: l,
            msg: f,
            onClickIntro: () => t(r),
            onClickCancel: () => h(r)
          }
        ),
        variant: "warning"
      });
      return;
    }
    t(r);
  }, [f, l, h, t, d]);
  return v(() => {
    u && w(c);
  }, [u, c, w]), {
    requestClose: c
  };
};
export {
  G as useWindowCloseGuard
};
