import { jsx as o } from "react/jsx-runtime";
import { pubSubService as d, PUB_SUB_EVENTS as u } from "./index-pub-sub.js";
import { OverlayStackingService as s } from "./index-OverlayStackingService.js";
import { useState as t, useRef as g, useEffect as C } from "react";
import { Drawer as b } from "./index-Drawer.js";
import { DrawerService as y } from "./index-DrawerService.js";
import O from "./index-DrawerRoot.module.scss.js";
const Z = (l) => {
  const {
    padding: p = "40px 48px"
  } = l, a = {
    variant: "temporary"
  }, [f, c] = t(!1), [m, w] = t(null), [i, D] = t(a), [v, x] = t(void 0), r = g(null), S = (e) => {
    r.current && y.close(r.current);
  };
  C(() => {
    d.$sub(u.reactDrawerOpen, ({
      component: e,
      options: h,
      id: n
    }) => {
      r.current && r.current !== n && s.unregister(r.current), x(s.register(n, "drawer")), w(e), D({
        ...a,
        ...h
      }), r.current = n, c(!0);
    }), d.$sub(u.reactDrawerClose, ({
      id: e
    }) => {
      e === r.current && (s.unregister(e), r.current = null, c(!1));
    });
  }, []);
  const I = i.zIndex ?? v;
  return o("div", {
    "data-testid": "drawer-root",
    id: "drawer-root",
    children: o(b, {
      open: f,
      ...i,
      zIndex: I,
      onClose: S,
      children: o("div", {
        className: O.drawerRootContentWrapper,
        style: {
          padding: p
        },
        children: m
      })
    })
  });
};
export {
  Z as DrawerRoot
};
