import { o as S } from "./rolldown-runtime.js";
import { i as C } from "./vendor/react.js";
import { f as I } from "./safeReact.js";
import { r as m, u as x } from "./useRenderElement.js";
import { n as v, t as d } from "./useIsoLayoutEffect.js";
import { n as y, t as P } from "./useTransitionStatus.js";
import { n as R } from "./useBaseUiId.js";
import { _ as O, c as E, f as i, s as h } from "./popupStateMapping.js";
var f = /* @__PURE__ */ S(C());
function M(e, t) {
  const n = f.useRef(null), r = f.useRef(null);
  return f.useCallback((o) => {
    if (e !== void 0) {
      if (n.current !== null) {
        const l = n.current, s = r.current, a = t.context.triggerElements.getById(l);
        s && a === s && t.context.triggerElements.delete(l), n.current = null, r.current = null;
      }
      o !== null && (n.current = e, r.current = o, t.context.triggerElements.add(e, o));
    }
  }, [t, e]);
}
function q(e, t, n, r) {
  const o = n.useState("isMountedByTrigger", e), l = M(e, n), s = v((a) => {
    if (l(a), !a || !n.select("open")) return;
    const g = n.select("activeTriggerId");
    if (g === e) {
      n.update({
        activeTriggerElement: a,
        ...r
      });
      return;
    }
    g == null && n.update({
      activeTriggerId: e,
      activeTriggerElement: a,
      ...r
    });
  });
  return d(() => {
    o && n.update({
      activeTriggerElement: t.current,
      ...r
    });
  }, [
    o,
    n,
    t,
    ...Object.values(r)
  ]), {
    registerTrigger: s,
    isMountedByThisTrigger: o
  };
}
function z(e) {
  const t = e.useState("open");
  d(() => {
    if (t && !e.select("activeTriggerId") && e.context.triggerElements.size === 1) {
      const n = e.context.triggerElements.entries().next();
      if (!n.done) {
        const [r, o] = n.value;
        e.update({
          activeTriggerId: r,
          activeTriggerElement: o
        });
      }
    }
  }, [t, e]);
}
function D(e, t, n) {
  const { mounted: r, setMounted: o, transitionStatus: l } = P(e);
  t.useSyncedValues({
    mounted: r,
    transitionStatus: l
  });
  const s = v(() => {
    o(!1), t.update({
      activeTriggerId: null,
      activeTriggerElement: null,
      mounted: !1
    }), n?.(), t.context.onOpenChangeComplete?.(!1);
  });
  return y({
    enabled: !t.useState("preventUnmountingOnClose"),
    open: e,
    ref: t.context.popupRef,
    onComplete() {
      e || s();
    }
  }), {
    forceUnmount: s,
    transitionStatus: l
  };
}
function b() {
  return new E({
    open: !1,
    transitionStatus: void 0,
    floatingElement: null,
    referenceElement: null,
    triggerElements: new h(),
    floatingId: "",
    syncOnly: !1,
    nested: !1,
    onOpenChange: void 0
  });
}
function J() {
  return {
    open: !1,
    openProp: void 0,
    mounted: !1,
    transitionStatus: void 0,
    floatingRootContext: b(),
    preventUnmountingOnClose: !1,
    payload: void 0,
    activeTriggerId: null,
    activeTriggerElement: null,
    triggerIdProp: void 0,
    popupElement: null,
    positionerElement: null,
    activeTriggerProps: m,
    inactiveTriggerProps: m,
    popupProps: m
  };
}
var c = i((e) => e.triggerIdProp ?? e.activeTriggerId), L = {
  open: i((e) => e.openProp ?? e.open),
  mounted: i((e) => e.mounted),
  transitionStatus: i((e) => e.transitionStatus),
  floatingRootContext: i((e) => e.floatingRootContext),
  preventUnmountingOnClose: i((e) => e.preventUnmountingOnClose),
  payload: i((e) => e.payload),
  activeTriggerId: c,
  activeTriggerElement: i((e) => e.mounted ? e.activeTriggerElement : null),
  isTriggerActive: i((e, t) => t !== void 0 && c(e) === t),
  isOpenedByTrigger: i((e, t) => t !== void 0 && c(e) === t && e.open),
  isMountedByTrigger: i((e, t) => t !== void 0 && c(e) === t && e.mounted),
  triggerProps: i((e, t) => t ? e.activeTriggerProps : e.inactiveTriggerProps),
  popupProps: i((e) => e.popupProps),
  popupElement: i((e) => e.popupElement),
  positionerElement: i((e) => e.positionerElement)
};
function N(e) {
  const { popupStore: t, treatPopupAsFloatingElement: n = !1, onOpenChange: r } = e, o = R(), l = O() != null, s = t.useState("open"), a = t.useState("activeTriggerElement"), g = t.useState(n ? "popupElement" : "positionerElement"), T = t.context.triggerElements, u = x(() => new E({
    open: s,
    transitionStatus: void 0,
    referenceElement: a,
    floatingElement: g,
    triggerElements: T,
    onOpenChange: r,
    floatingId: o,
    syncOnly: !0,
    nested: l
  })).current;
  return d(() => {
    const p = {
      open: s,
      floatingId: o,
      referenceElement: a,
      floatingElement: g
    };
    I(a) && (p.domReferenceElement = a), u.state.positionReference === u.state.referenceElement && (p.positionReference = a), u.update(p);
  }, [
    s,
    o,
    a,
    g,
    u
  ]), u.context.onOpenChange = r, u.context.nested = l, u;
}
export {
  D as a,
  z as i,
  J as n,
  q as o,
  L as r,
  M as s,
  N as t
};

//# sourceMappingURL=useSyncedFloatingRootContext.js.map