"use client";
import * as React from "react";
import * as ReactDOM from "react-dom";
import { useControlled } from "../../../../utils/esm/useControlled.js";
import { useStableCallback } from "../../../../utils/esm/useStableCallback.js";
import { PreviewCardRootContext } from "./PreviewCardContext.js";
import { OPEN_DELAY, CLOSE_DELAY } from "../utils/constants.js";
import { useFocusWithDelay } from "../../utils/interactions/useFocusWithDelay.js";
import { useOpenChangeComplete } from "../../utils/useOpenChangeComplete.js";
import { useTransitionStatus } from "../../utils/useTransitionStatus.js";
import { jsx } from "react/jsx-runtime";
import { useFloatingRootContext } from "../../floating-ui-react/hooks/useFloatingRootContext.js";
import { useHover } from "../../floating-ui-react/hooks/useHover.js";
import { safePolygon } from "../../floating-ui-react/safePolygon.js";
import { useDismiss } from "../../floating-ui-react/hooks/useDismiss.js";
import { useInteractions } from "../../floating-ui-react/hooks/useInteractions.js";
import { triggerHover, triggerFocus, triggerPress, escapeKey } from "../../utils/reason-parts.js";
function PreviewCardRoot(props) {
  const {
    open: externalOpen,
    defaultOpen,
    onOpenChange: onOpenChangeProp,
    onOpenChangeComplete,
    actionsRef
  } = props;
  const delayRef = React.useRef(OPEN_DELAY);
  const closeDelayRef = React.useRef(CLOSE_DELAY);
  const writeDelayRefs = useStableCallback((config) => {
    delayRef.current = config.delay ?? OPEN_DELAY;
    closeDelayRef.current = config.closeDelay ?? CLOSE_DELAY;
  });
  const [triggerElement, setTriggerElement] = React.useState(null);
  const [positionerElement, setPositionerElement] = React.useState(null);
  const [instantTypeState, setInstantTypeState] = React.useState();
  const popupRef = React.useRef(null);
  const [open, setOpenUnwrapped] = useControlled({
    controlled: externalOpen,
    default: defaultOpen,
    name: "PreviewCard",
    state: "open"
  });
  const onOpenChange = useStableCallback(onOpenChangeProp);
  const {
    mounted,
    setMounted,
    transitionStatus
  } = useTransitionStatus(open);
  const handleUnmount = useStableCallback(() => {
    setMounted(false);
    onOpenChangeComplete?.(false);
  });
  useOpenChangeComplete({
    enabled: !actionsRef,
    open,
    ref: popupRef,
    onComplete() {
      if (!open) {
        handleUnmount();
      }
    }
  });
  React.useImperativeHandle(actionsRef, () => ({
    unmount: handleUnmount
  }), [handleUnmount]);
  const setOpen = useStableCallback((nextOpen, eventDetails) => {
    const isHover = eventDetails.reason === triggerHover;
    const isFocusOpen = nextOpen && eventDetails.reason === triggerFocus;
    const isDismissClose = !nextOpen && (eventDetails.reason === triggerPress || eventDetails.reason === escapeKey);
    onOpenChange(nextOpen, eventDetails);
    if (eventDetails.isCanceled) {
      return;
    }
    function changeState() {
      setOpenUnwrapped(nextOpen);
    }
    if (isHover) {
      ReactDOM.flushSync(changeState);
    } else {
      changeState();
    }
    if (isFocusOpen || isDismissClose) {
      setInstantTypeState(isFocusOpen ? "focus" : "dismiss");
    } else if (eventDetails.reason === triggerHover) {
      setInstantTypeState(void 0);
    }
  });
  const context = useFloatingRootContext({
    elements: {
      reference: triggerElement,
      floating: positionerElement
    },
    open,
    onOpenChange: setOpen
  });
  const instantType = instantTypeState;
  const getDelayValue = () => delayRef.current;
  const getCloseDelayValue = () => closeDelayRef.current;
  const hover = useHover(context, {
    mouseOnly: true,
    move: false,
    handleClose: safePolygon(),
    restMs: getDelayValue,
    delay: () => ({
      close: getCloseDelayValue()
    })
  });
  const focus = useFocusWithDelay(context, {
    delay: getDelayValue
  });
  const dismiss = useDismiss(context);
  const {
    getReferenceProps,
    getFloatingProps
  } = useInteractions([hover, focus, dismiss]);
  const contextValue = React.useMemo(() => ({
    open,
    setOpen,
    mounted,
    setMounted,
    setTriggerElement,
    positionerElement,
    setPositionerElement,
    popupRef,
    triggerProps: getReferenceProps(),
    popupProps: getFloatingProps(),
    floatingRootContext: context,
    instantType,
    transitionStatus,
    onOpenChangeComplete,
    writeDelayRefs
  }), [open, setOpen, mounted, setMounted, positionerElement, getReferenceProps, getFloatingProps, context, instantType, transitionStatus, onOpenChangeComplete, writeDelayRefs]);
  return /* @__PURE__ */ jsx(PreviewCardRootContext.Provider, {
    value: contextValue,
    children: props.children
  });
}
export {
  PreviewCardRoot
};
//# sourceMappingURL=PreviewCardRoot.js.map
