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