import { j as p } from "../../_virtual/jsx-runtime.js";
import { useRef as l, useEffect as d } from "react";
import { createPortal as h } from "react-dom";
import { useKeyPress as x } from "../../hooks/useKeyPress.js";
import { useTrappedFocus as b } from "../../hooks/useTrappedFocus.js";
import { getComponentRoot as j, getId as P } from "../../utils/index.js";
import { BottomSheetContent as R } from "./components/bottomSheetContent/index.js";
import { Overlay as A } from "./styles.js";
function z({
  bottomSheetRoot: s,
  isOpened: t,
  shouldCloseOnEsc: v,
  shouldCloseOnOverlayClick: u,
  onClose: n,
  allowBackgroundInteractions: e,
  bodyRef: E,
  ...c
}) {
  const r = l(null), i = l(j(s)), a = P(c.id);
  b(r);
  const y = x("Escape", i);
  return d(() => {
    v && !e && t && y && n();
  }, [y]), d(() => {
    const o = (f) => f.target === r.current && n(), m = () => {
      var f;
      return (f = r.current) == null ? void 0 : f.removeEventListener("click", o);
    };
    return r.current && (u && !e ? r.current.addEventListener("click", o) : m()), m;
  }, [t, u, e]), d(() => {
    var o;
    return t && (document.body.style.overflow = "hidden", (o = i.current) == null || o.setAttribute("aria-hidden", "true")), () => {
      var m;
      document.body.style.overflow = "auto", (m = i.current) == null || m.removeAttribute("aria-hidden");
    };
  }, [t]), t ? h(
    /* @__PURE__ */ p.jsx(
      A,
      {
        ref: r,
        isOpened: t,
        allowBackgroundInteractions: e,
        "data-testid": `${c["data-testid"]}-bottom-sheet-overlay`,
        id: a("overlay"),
        children: /* @__PURE__ */ p.jsx(
          R,
          {
            ...c,
            bottomSheetRoot: s,
            isOpened: t,
            shouldCloseOnEsc: v && !e,
            shouldCloseOnOverlayClick: u && !e,
            onClose: n,
            bodyRef: E,
            id: a("content")
          }
        )
      }
    ),
    i.current
  ) : null;
}
export {
  z as BottomSheet
};
