import * as s from "react";
import { composeEventHandlers as h } from "../../../../../@radix-ui_primitive@1.1.0/node_modules/@radix-ui/primitive/dist/index.js";
import { Primitive as w, dispatchDiscreteCustomEvent as k } from "../../../../../@radix-ui_react-primitive@2.0.0_@types_react-dom@18.3.6_@types_react@18.3.3__@types_rea_bdcf182289a3a76356f07e550726e479/node_modules/@radix-ui/react-primitive/dist/index.js";
import { useComposedRefs as R } from "../../../../../@radix-ui_react-compose-refs@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-compose-refs/dist/index.js";
import { useCallbackRef as g } from "../../../../../@radix-ui_react-use-callback-ref@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-use-callback-ref/dist/index.js";
import { useEscapeKeydown as U } from "../../../../../@radix-ui_react-use-escape-keydown@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-use-escape-keydown/dist/index.js";
import { j as x } from "../../../../../../../_virtual/jsx-runtime.js";
var j = "DismissableLayer", p = "dismissableLayer.update", z = "dismissableLayer.pointerDownOutside", H = "dismissableLayer.focusOutside", C, T = s.createContext({
  layers: /* @__PURE__ */ new Set(),
  layersWithOutsidePointerEventsDisabled: /* @__PURE__ */ new Set(),
  branches: /* @__PURE__ */ new Set()
}), M = s.forwardRef(
  (n, e) => {
    const {
      disableOutsidePointerEvents: r = !1,
      onEscapeKeyDown: a,
      onPointerDownOutside: i,
      onFocusOutside: c,
      onInteractOutside: f,
      onDismiss: d,
      ...E
    } = n, o = s.useContext(T), [u, F] = s.useState(null), l = u?.ownerDocument ?? globalThis?.document, [, I] = s.useState({}), S = R(e, (t) => F(t)), y = Array.from(o.layers), [W] = [...o.layersWithOutsidePointerEventsDisabled].slice(-1), A = y.indexOf(W), b = u ? y.indexOf(u) : -1, N = o.layersWithOutsidePointerEventsDisabled.size > 0, D = b >= A, _ = Y((t) => {
      const v = t.target, P = [...o.branches].some((m) => m.contains(v));
      !D || P || (i?.(t), f?.(t), t.defaultPrevented || d?.());
    }, l), O = q((t) => {
      const v = t.target;
      [...o.branches].some((m) => m.contains(v)) || (c?.(t), f?.(t), t.defaultPrevented || d?.());
    }, l);
    return U((t) => {
      b === o.layers.size - 1 && (a?.(t), !t.defaultPrevented && d && (t.preventDefault(), d()));
    }, l), s.useEffect(() => {
      if (u)
        return r && (o.layersWithOutsidePointerEventsDisabled.size === 0 && (C = l.body.style.pointerEvents, l.body.style.pointerEvents = "none"), o.layersWithOutsidePointerEventsDisabled.add(u)), o.layers.add(u), L(), () => {
          r && o.layersWithOutsidePointerEventsDisabled.size === 1 && (l.body.style.pointerEvents = C);
        };
    }, [u, l, r, o]), s.useEffect(() => () => {
      u && (o.layers.delete(u), o.layersWithOutsidePointerEventsDisabled.delete(u), L());
    }, [u, o]), s.useEffect(() => {
      const t = () => I({});
      return document.addEventListener(p, t), () => document.removeEventListener(p, t);
    }, []), /* @__PURE__ */ x.jsx(
      w.div,
      {
        ...E,
        ref: S,
        style: {
          pointerEvents: N ? D ? "auto" : "none" : void 0,
          ...n.style
        },
        onFocusCapture: h(n.onFocusCapture, O.onFocusCapture),
        onBlurCapture: h(n.onBlurCapture, O.onBlurCapture),
        onPointerDownCapture: h(
          n.onPointerDownCapture,
          _.onPointerDownCapture
        )
      }
    );
  }
);
M.displayName = j;
var K = "DismissableLayerBranch", X = s.forwardRef((n, e) => {
  const r = s.useContext(T), a = s.useRef(null), i = R(e, a);
  return s.useEffect(() => {
    const c = a.current;
    if (c)
      return r.branches.add(c), () => {
        r.branches.delete(c);
      };
  }, [r.branches]), /* @__PURE__ */ x.jsx(w.div, { ...n, ref: i });
});
X.displayName = K;
function Y(n, e = globalThis?.document) {
  const r = g(n), a = s.useRef(!1), i = s.useRef(() => {
  });
  return s.useEffect(() => {
    const c = (d) => {
      if (d.target && !a.current) {
        let E = function() {
          B(
            z,
            r,
            o,
            { discrete: !0 }
          );
        };
        const o = { originalEvent: d };
        d.pointerType === "touch" ? (e.removeEventListener("click", i.current), i.current = E, e.addEventListener("click", i.current, { once: !0 })) : E();
      } else
        e.removeEventListener("click", i.current);
      a.current = !1;
    }, f = window.setTimeout(() => {
      e.addEventListener("pointerdown", c);
    }, 0);
    return () => {
      window.clearTimeout(f), e.removeEventListener("pointerdown", c), e.removeEventListener("click", i.current);
    };
  }, [e, r]), {
    // ensures we check React component tree (not just DOM tree)
    onPointerDownCapture: () => a.current = !0
  };
}
function q(n, e = globalThis?.document) {
  const r = g(n), a = s.useRef(!1);
  return s.useEffect(() => {
    const i = (c) => {
      c.target && !a.current && B(H, r, { originalEvent: c }, {
        discrete: !1
      });
    };
    return e.addEventListener("focusin", i), () => e.removeEventListener("focusin", i);
  }, [e, r]), {
    onFocusCapture: () => a.current = !0,
    onBlurCapture: () => a.current = !1
  };
}
function L() {
  const n = new CustomEvent(p);
  document.dispatchEvent(n);
}
function B(n, e, r, { discrete: a }) {
  const i = r.originalEvent.target, c = new CustomEvent(n, { bubbles: !1, cancelable: !0, detail: r });
  e && i.addEventListener(n, e, { once: !0 }), a ? k(i, c) : i.dispatchEvent(c);
}
export {
  M as DismissableLayer,
  X as DismissableLayerBranch
};
//# sourceMappingURL=index.js.map
