import * as u from "react";
import { useTimeout as P, Timeout as C } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useTimeout.js";
import { useIsoLayoutEffect as y } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useIsoLayoutEffect.js";
import { getDelay as M } from "../hooks/useHover.js";
import { createChangeEventDetails as E } from "../../utils/createBaseUIEventDetails.js";
import { jsx as G } from "react/jsx-runtime";
import { none as O } from "../../utils/reason-parts.js";
const h = /* @__PURE__ */ u.createContext({
  hasProvider: !1,
  timeoutMs: 0,
  delayRef: {
    current: 0
  },
  initialDelayRef: {
    current: 0
  },
  timeout: new C(),
  currentIdRef: {
    current: null
  },
  currentContextRef: {
    current: null
  }
});
process.env.NODE_ENV !== "production" && (h.displayName = "FloatingDelayGroupContext");
function b(i) {
  const {
    children: I,
    delay: o,
    timeoutMs: n = 0
  } = i, l = u.useRef(o), c = u.useRef(o), R = u.useRef(null), t = u.useRef(null), r = P();
  return /* @__PURE__ */ G(h.Provider, {
    value: u.useMemo(() => ({
      hasProvider: !0,
      delayRef: l,
      initialDelayRef: c,
      currentIdRef: R,
      timeoutMs: n,
      currentContextRef: t,
      timeout: r
    }), [n, r]),
    children: I
  });
}
function j(i, I = {
  open: !1
}) {
  const o = "rootStore" in i ? i.rootStore : i, n = o.useState("floatingId"), {
    enabled: l = !0,
    open: c
  } = I, R = u.useContext(h), {
    currentIdRef: t,
    delayRef: r,
    timeoutMs: m,
    initialDelayRef: p,
    currentContextRef: s,
    hasProvider: g,
    timeout: f
  } = R, [D, d] = u.useState(!1);
  return y(() => {
    function e() {
      var a;
      d(!1), (a = s.current) == null || a.setIsInstantPhase(!1), t.current = null, s.current = null, r.current = p.current;
    }
    if (l && t.current && !c && t.current === n) {
      if (d(!1), m) {
        const a = n;
        return f.start(m, () => {
          o.select("open") || t.current && t.current !== a || e();
        }), () => {
          f.clear();
        };
      }
      e();
    }
  }, [l, c, n, t, r, m, p, s, f, o]), y(() => {
    if (!l || !c)
      return;
    const e = s.current, a = t.current;
    f.clear(), s.current = {
      onOpenChange: o.setOpen,
      setIsInstantPhase: d
    }, t.current = n, r.current = {
      open: 0,
      close: M(p.current, "close")
    }, a !== null && a !== n ? (d(!0), e == null || e.setIsInstantPhase(!0), e == null || e.onOpenChange(!1, E(O))) : (d(!1), e == null || e.setIsInstantPhase(!1));
  }, [l, c, n, o, t, r, m, p, s, f]), y(() => () => {
    s.current = null;
  }, [s]), u.useMemo(() => ({
    hasProvider: g,
    delayRef: r,
    isInstantPhase: D
  }), [g, r, D]);
}
export {
  b as FloatingDelayGroup,
  j as useDelayGroup
};
