import * as n from "react";
import { useIsoLayoutEffect as m } 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 { TooltipRootContext as L } from "./TooltipRootContext.js";
import { createChangeEventDetails as R } from "../../utils/createBaseUIEventDetails.js";
import { TooltipStore as $ } from "../store/TooltipStore.js";
import { jsx as q } from "react/jsx-runtime";
import { useFocus as w } from "../../floating-ui-react/hooks/useFocus.js";
import { useDismiss as z } from "../../floating-ui-react/hooks/useDismiss.js";
import { useClientPoint as B } from "../../floating-ui-react/hooks/useClientPoint.js";
import { useInteractions as G } from "../../floating-ui-react/hooks/useInteractions.js";
import { useImplicitActiveTrigger as J, useOpenStateTransitions as K } from "../../utils/popups/popupStoreUtils.js";
import { none as N, imperativeAction as Q, disabled as W } from "../../utils/reason-parts.js";
function le(i) {
  const {
    disabled: t = !1,
    defaultOpen: o = !1,
    open: C,
    disableHoverablePopup: x = !1,
    trackCursorAxis: s = "none",
    actionsRef: k,
    onOpenChange: A,
    onOpenChangeComplete: D,
    handle: a,
    triggerId: l,
    defaultTriggerId: T = null,
    children: p
  } = i, e = $.useStore(a == null ? void 0 : a.store, {
    open: C ?? o,
    activeTriggerId: l !== void 0 ? l : T
  });
  e.useControlledProp("open", C, o), e.useControlledProp("activeTriggerId", l, T), e.useContextCallback("onOpenChange", A), e.useContextCallback("onOpenChangeComplete", D);
  const u = e.useState("open"), c = !t && u, v = e.useState("activeTriggerId"), E = e.useState("payload");
  e.useSyncedValues({
    trackCursorAxis: s,
    disableHoverablePopup: x
  }), m(() => {
    u && t && e.setOpen(!1, R(W));
  }, [u, t, e]), e.useSyncedValue("disabled", t), J(e);
  const {
    forceUnmount: P,
    transitionStatus: d
  } = K(c, e), I = e.useState("isInstantPhase"), f = e.useState("instantType"), y = e.useState("lastOpenChangeReason"), r = n.useRef(null);
  m(() => {
    d === "ending" && y === N || d !== "ending" && I ? (f !== "delay" && (r.current = f), e.set("instantType", "delay")) : r.current !== null && (e.set("instantType", r.current), r.current = null);
  }, [d, I, y, f, e]), m(() => {
    c && v == null && e.set("payload", void 0);
  }, [e, v, c]);
  const S = n.useCallback(() => {
    e.setOpen(!1, X(e, Q));
  }, [e]);
  n.useImperativeHandle(k, () => ({
    unmount: P,
    close: S
  }), [P, S]);
  const g = e.useState("floatingRootContext"), M = w(g, {
    enabled: !t
  }), U = z(g, {
    enabled: !t,
    referencePress: !0
  }), V = B(g, {
    enabled: !t && s !== "none",
    axis: s === "none" ? void 0 : s
  }), {
    getReferenceProps: b,
    getFloatingProps: O,
    getTriggerProps: h
  } = G([M, U, V]), F = n.useMemo(() => b(), [b]), H = n.useMemo(() => h(), [h]), j = n.useMemo(() => O(), [O]);
  return e.useSyncedValues({
    activeTriggerProps: F,
    inactiveTriggerProps: H,
    popupProps: j
  }), /* @__PURE__ */ q(L.Provider, {
    value: e,
    children: typeof p == "function" ? p({
      payload: E
    }) : p
  });
}
function X(i, t) {
  const o = R(t);
  return o.preventUnmountOnClose = () => {
    i.set("preventUnmountingOnClose", !0);
  }, o;
}
export {
  le as TooltipRoot
};
