import { j as n } from "../../_virtual/jsx-runtime.js";
import { useRef as v, useEffect as c } from "react";
import H from "react-dom";
import { useKeyPress as L } from "../../hooks/useKeyPress.js";
import { getId as g, getComponentRoot as M, noop as _ } from "../../utils/index.js";
import { Overlay as F, Dialog as K } from "./styles.js";
import "../IconButton/styles.js";
import "../Spinner/styles.js";
import "../Button/index.js";
import { DialogContent as q } from "./components/DialogContent.js";
import { useTrappedFocus as z } from "../../hooks/useTrappedFocus.js";
function Y({
  children: y,
  className: k,
  isOpen: e = !1,
  modalRoot: E,
  onClose: d = _,
  shouldCloseOnEsc: b = !0,
  shouldCloseOnOverlayClick: l = !0,
  pinnedFooter: x = !0,
  size: j = "medium",
  position: w = "center",
  action: P,
  variant: m = "wide",
  hideCloseIcon: R = !1,
  title: h,
  primaryAction: I,
  dismissAction: D,
  stackedActionButtons: T = !1,
  isLoading: A = !1,
  ...o
}) {
  const t = v(null);
  z(t);
  const u = g(o["data-testid"]), s = g(o["data-trackid"]), r = v(M(E)), f = L("Escape", r);
  return c(() => {
    b && e && f && d();
  }, [f]), c(() => {
    const a = (i) => i.target === t.current && d(), p = () => {
      var i;
      return (i = t.current) == null ? void 0 : i.removeEventListener("click", a);
    };
    return t.current && (l ? t.current.addEventListener("click", a) : p()), p;
  }, [e, l]), c(() => (e && (document.body.style.overflow = "hidden", r.current && r.current.setAttribute("aria-hidden", "true")), () => {
    document.body.style.overflow = "auto", r.current && r.current.removeAttribute("aria-hidden");
  }), [e]), e ? H.createPortal(
    /* @__PURE__ */ n.jsx(
      F,
      {
        className: k,
        ref: t,
        isOpen: e,
        "data-testid": u("overlay"),
        "data-trackid": s("overlay"),
        "aria-label": o["aria-label"],
        children: /* @__PURE__ */ n.jsx(
          K,
          {
            onClick: (a) => {
              a.stopPropagation(), a.nativeEvent.stopImmediatePropagation();
            },
            role: "dialog",
            "data-testid": u("dialog-wrapper"),
            "data-trackid": s("dialog-wrapper"),
            size: j,
            position: w,
            variant: m,
            children: /* @__PURE__ */ n.jsx(
              q,
              {
                onClose: d,
                pinnedFooter: x,
                action: P,
                variant: m,
                hideCloseIcon: R,
                title: h,
                primaryAction: I,
                dismissAction: D,
                stackedActionButtons: T,
                isLoading: A,
                ...o,
                children: y
              }
            )
          }
        )
      }
    ),
    r.current
  ) : null;
}
export {
  Y as Modal
};
