"use client";
import * as f from "react";
import { u as T } from "./useStableCallback-CudqoENs.js";
import { u as E } from "./useIsoLayoutEffect-DpJo20by.js";
import { u as C, a as x } from "./useOpenChangeComplete-DkeH9FGJ.js";
import { F as v, P as I, d as r, u as P } from "./popupStateMapping-BT9Y_jIq.js";
import { E as d, a as R } from "./useRenderElement-DNB2vtF1.js";
import { a as O } from "./useBaseUiId-C8Dq0BQq.js";
import { a as y } from "./floating-ui.utils.dom-jr2suqy2.js";
function b(e, n) {
  const t = f.useRef(null), o = f.useRef(null);
  return f.useCallback((i) => {
    if (e !== void 0) {
      if (t.current !== null) {
        const l = t.current, s = o.current, u = n.context.triggerElements.getById(l);
        s && u === s && n.context.triggerElements.delete(l), t.current = null, o.current = null;
      }
      i !== null && (t.current = e, o.current = i, n.context.triggerElements.add(e, i));
    }
  }, [n, e]);
}
function j(e, n, t, o) {
  const i = t.useState("isMountedByTrigger", e), l = b(e, t), s = T((u) => {
    if (l(u), !u || !t.select("open"))
      return;
    const a = t.select("activeTriggerId");
    if (a === e) {
      t.update({
        activeTriggerElement: u,
        ...o
      });
      return;
    }
    a == null && t.update({
      activeTriggerId: e,
      activeTriggerElement: u,
      ...o
    });
  });
  return E(() => {
    i && t.update({
      activeTriggerElement: n.current,
      ...o
    });
  }, [i, t, n, ...Object.values(o)]), {
    registerTrigger: s,
    isMountedByThisTrigger: i
  };
}
function z(e) {
  const n = e.useState("open");
  E(() => {
    if (n && !e.select("activeTriggerId") && e.context.triggerElements.size === 1) {
      const t = e.context.triggerElements.entries().next();
      if (!t.done) {
        const [o, i] = t.value;
        e.update({
          activeTriggerId: o,
          activeTriggerElement: i
        });
      }
    }
  }, [n, e]);
}
function D(e, n, t) {
  const {
    mounted: o,
    setMounted: i,
    transitionStatus: l
  } = C(e);
  n.useSyncedValues({
    mounted: o,
    transitionStatus: l
  });
  const s = T(() => {
    i(!1), n.update({
      activeTriggerId: null,
      activeTriggerElement: null,
      mounted: !1
    }), t?.(), n.context.onOpenChangeComplete?.(!1);
  }), u = n.useState("preventUnmountingOnClose");
  return x({
    enabled: !u,
    open: e,
    ref: n.context.popupRef,
    onComplete() {
      e || s();
    }
  }), {
    forceUnmount: s,
    transitionStatus: l
  };
}
function h() {
  return new v({
    open: !1,
    floatingElement: null,
    referenceElement: null,
    triggerElements: new I(),
    floatingId: "",
    nested: !1,
    noEmit: !1,
    onOpenChange: void 0
  });
}
function J() {
  return {
    open: !1,
    openProp: void 0,
    mounted: !1,
    transitionStatus: "idle",
    floatingRootContext: h(),
    preventUnmountingOnClose: !1,
    payload: void 0,
    activeTriggerId: null,
    activeTriggerElement: null,
    triggerIdProp: void 0,
    popupElement: null,
    positionerElement: null,
    activeTriggerProps: d,
    inactiveTriggerProps: d,
    popupProps: d
  };
}
const c = r((e) => e.triggerIdProp ?? e.activeTriggerId), L = {
  open: r((e) => e.openProp ?? e.open),
  mounted: r((e) => e.mounted),
  transitionStatus: r((e) => e.transitionStatus),
  floatingRootContext: r((e) => e.floatingRootContext),
  preventUnmountingOnClose: r((e) => e.preventUnmountingOnClose),
  payload: r((e) => e.payload),
  activeTriggerId: c,
  activeTriggerElement: r((e) => e.mounted ? e.activeTriggerElement : null),
  /**
   * Whether the trigger with the given ID was used to open the popup.
   */
  isTriggerActive: r((e, n) => n !== void 0 && c(e) === n),
  /**
   * Whether the popup is open and was activated by a trigger with the given ID.
   */
  isOpenedByTrigger: r((e, n) => n !== void 0 && c(e) === n && e.open),
  /**
   * Whether the popup is mounted and was activated by a trigger with the given ID.
   */
  isMountedByTrigger: r((e, n) => n !== void 0 && c(e) === n && e.mounted),
  triggerProps: r((e, n) => n ? e.activeTriggerProps : e.inactiveTriggerProps),
  popupProps: r((e) => e.popupProps),
  popupElement: r((e) => e.popupElement),
  positionerElement: r((e) => e.positionerElement)
};
function N(e) {
  const {
    popupStore: n,
    noEmit: t = !1,
    treatPopupAsFloatingElement: o = !1,
    onOpenChange: i
  } = e, l = O(), s = P() != null, u = n.useState("open"), a = n.useState("activeTriggerElement"), p = n.useState(o ? "popupElement" : "positionerElement"), S = n.context.triggerElements, g = R(() => new v({
    open: u,
    referenceElement: a,
    floatingElement: p,
    triggerElements: S,
    onOpenChange: i,
    floatingId: l,
    nested: s,
    noEmit: t
  })).current;
  return E(() => {
    const m = {
      open: u,
      floatingId: l,
      referenceElement: a,
      floatingElement: p
    };
    y(a) && (m.domReferenceElement = a), g.state.positionReference === g.state.referenceElement && (m.positionReference = a), g.update(m);
  }, [u, l, a, p, g]), g.context.onOpenChange = i, g.context.nested = s, g.context.noEmit = t, g;
}
export {
  z as a,
  D as b,
  J as c,
  j as d,
  b as e,
  L as p,
  N as u
};
