"use client";
import * as React from "react";
import { useIsoLayoutEffect } from "../../../../utils/esm/useIsoLayoutEffect.js";
import { TooltipRootContext } from "./TooltipRootContext.js";
import { createChangeEventDetails } from "../../utils/createBaseUIEventDetails.js";
import { TooltipStore } from "../store/TooltipStore.js";
import { jsx } from "react/jsx-runtime";
import { useImplicitActiveTrigger, useOpenStateTransitions } from "../../utils/popups/popupStoreUtils.js";
import { useFocus } from "../../floating-ui-react/hooks/useFocus.js";
import { useDismiss } from "../../floating-ui-react/hooks/useDismiss.js";
import { useClientPoint } from "../../floating-ui-react/hooks/useClientPoint.js";
import { useInteractions } from "../../floating-ui-react/hooks/useInteractions.js";
import { none, imperativeAction, disabled } from "../../utils/reason-parts.js";
function TooltipRoot(props) {
  const {
    disabled: disabled$1 = false,
    defaultOpen = false,
    open: openProp,
    disableHoverablePopup = false,
    trackCursorAxis = "none",
    actionsRef,
    onOpenChange,
    onOpenChangeComplete,
    handle,
    triggerId: triggerIdProp,
    defaultTriggerId: defaultTriggerIdProp = null,
    children
  } = props;
  const store = TooltipStore.useStore(handle?.store, {
    open: openProp ?? defaultOpen,
    activeTriggerId: triggerIdProp !== void 0 ? triggerIdProp : defaultTriggerIdProp
  });
  store.useControlledProp("open", openProp, defaultOpen);
  store.useControlledProp("activeTriggerId", triggerIdProp, defaultTriggerIdProp);
  store.useContextCallback("onOpenChange", onOpenChange);
  store.useContextCallback("onOpenChangeComplete", onOpenChangeComplete);
  const openState = store.useState("open");
  const activeTriggerId = store.useState("activeTriggerId");
  const payload = store.useState("payload");
  store.useSyncedValues({
    trackCursorAxis,
    disableHoverablePopup
  });
  const open = !disabled$1 && openState;
  useIsoLayoutEffect(() => {
    if (openState && disabled$1) {
      store.setOpen(false, createChangeEventDetails(disabled));
    }
  }, [openState, disabled$1, store]);
  store.useSyncedValue("disabled", disabled$1);
  useImplicitActiveTrigger(store);
  const {
    forceUnmount,
    transitionStatus
  } = useOpenStateTransitions(open, store);
  const isInstantPhase = store.useState("isInstantPhase");
  const instantType = store.useState("instantType");
  const lastOpenChangeReason = store.useState("lastOpenChangeReason");
  const previousInstantTypeRef = React.useRef(null);
  useIsoLayoutEffect(() => {
    if (transitionStatus === "ending" && lastOpenChangeReason === none || transitionStatus !== "ending" && isInstantPhase) {
      if (instantType !== "delay") {
        previousInstantTypeRef.current = instantType;
      }
      store.set("instantType", "delay");
    } else if (previousInstantTypeRef.current !== null) {
      store.set("instantType", previousInstantTypeRef.current);
      previousInstantTypeRef.current = null;
    }
  }, [transitionStatus, isInstantPhase, lastOpenChangeReason, instantType, store]);
  useIsoLayoutEffect(() => {
    if (open) {
      if (activeTriggerId == null) {
        store.set("payload", void 0);
      }
    }
  }, [store, activeTriggerId, open]);
  const handleImperativeClose = React.useCallback(() => {
    store.setOpen(false, createTooltipEventDetails(store, imperativeAction));
  }, [store]);
  React.useImperativeHandle(actionsRef, () => ({
    unmount: forceUnmount,
    close: handleImperativeClose
  }), [forceUnmount, handleImperativeClose]);
  const floatingRootContext = store.useState("floatingRootContext");
  const focus = useFocus(floatingRootContext, {
    enabled: !disabled$1
  });
  const dismiss = useDismiss(floatingRootContext, {
    enabled: !disabled$1,
    referencePress: true
  });
  const clientPoint = useClientPoint(floatingRootContext, {
    enabled: !disabled$1 && trackCursorAxis !== "none",
    axis: trackCursorAxis === "none" ? void 0 : trackCursorAxis
  });
  const {
    getReferenceProps,
    getFloatingProps,
    getTriggerProps
  } = useInteractions([focus, dismiss, clientPoint]);
  const activeTriggerProps = React.useMemo(() => getReferenceProps(), [getReferenceProps]);
  const inactiveTriggerProps = React.useMemo(() => getTriggerProps(), [getTriggerProps]);
  const popupProps = React.useMemo(() => getFloatingProps(), [getFloatingProps]);
  store.useSyncedValues({
    floatingRootContext,
    activeTriggerProps,
    inactiveTriggerProps,
    popupProps
  });
  return /* @__PURE__ */ jsx(TooltipRootContext.Provider, {
    value: store,
    children: typeof children === "function" ? children({
      payload
    }) : children
  });
}
function createTooltipEventDetails(store, reason) {
  const details = createChangeEventDetails(reason);
  details.preventUnmountOnClose = () => {
    store.set("preventUnmountingOnClose", true);
  };
  return details;
}
export {
  TooltipRoot
};
//# sourceMappingURL=TooltipRoot.js.map
