"use client";
import * as React from "react";
import * as ReactDOM from "react-dom";
import { useTimeout } from "../../../../utils/esm/useTimeout.js";
import { getEmptyRootContext } from "../../floating-ui-react/utils/getEmptyRootContext.js";
import { useRenderElement } from "../../utils/useRenderElement.js";
import { useNavigationMenuRootContext, useNavigationMenuTreeContext } from "../root/NavigationMenuRootContext.js";
import { useNavigationMenuPortalContext } from "../portal/NavigationMenuPortalContext.js";
import { useAnchorPositioning } from "../../utils/useAnchorPositioning.js";
import { NavigationMenuPositionerContext } from "./NavigationMenuPositionerContext.js";
import { popupStateMapping } from "../../utils/popupStateMapping.js";
import { POPUP_COLLISION_AVOIDANCE, DROPDOWN_COLLISION_AVOIDANCE } from "../../utils/constants.js";
import { adaptiveOrigin } from "../../utils/adaptiveOriginMiddleware.js";
import { getDisabledMountTransitionStyles } from "../../utils/getDisabledMountTransitionStyles.js";
import { jsx } from "react/jsx-runtime";
import { getWindow } from "../../../../../@floating-ui/utils/dist/floating-ui.utils.dom.js";
import { isOutsideEvent, enableFocusInside, disableFocusInside } from "../../floating-ui-react/utils/tabbable.js";
const EMPTY_ROOT_CONTEXT = getEmptyRootContext();
const NavigationMenuPositioner = /* @__PURE__ */ React.forwardRef(function NavigationMenuPositioner2(componentProps, forwardedRef) {
  const {
    open,
    mounted,
    positionerElement,
    setPositionerElement,
    floatingRootContext,
    nested,
    transitionStatus
  } = useNavigationMenuRootContext();
  const {
    className,
    render,
    anchor,
    positionMethod = "absolute",
    side = "bottom",
    align = "center",
    sideOffset = 0,
    alignOffset = 0,
    collisionBoundary = "clipping-ancestors",
    collisionPadding = 5,
    collisionAvoidance = nested ? POPUP_COLLISION_AVOIDANCE : DROPDOWN_COLLISION_AVOIDANCE,
    arrowPadding = 5,
    sticky = false,
    disableAnchorTracking = false,
    ...elementProps
  } = componentProps;
  const keepMounted = useNavigationMenuPortalContext();
  const nodeId = useNavigationMenuTreeContext();
  const resizeTimeout = useTimeout();
  const [instant, setInstant] = React.useState(false);
  const positionerRef = React.useRef(null);
  const prevTriggerElementRef = React.useRef(null);
  React.useEffect(() => {
    if (!positionerElement) {
      return void 0;
    }
    function onFocus(event) {
      if (positionerElement && isOutsideEvent(event)) {
        const focusing = event.type === "focusin";
        const manageFocus = focusing ? enableFocusInside : disableFocusInside;
        manageFocus(positionerElement);
      }
    }
    positionerElement.addEventListener("focusin", onFocus, true);
    positionerElement.addEventListener("focusout", onFocus, true);
    return () => {
      positionerElement.removeEventListener("focusin", onFocus, true);
      positionerElement.removeEventListener("focusout", onFocus, true);
    };
  }, [positionerElement]);
  const domReference = (floatingRootContext || EMPTY_ROOT_CONTEXT).useState("domReferenceElement");
  const positioning = useAnchorPositioning({
    anchor: anchor ?? domReference ?? prevTriggerElementRef,
    positionMethod,
    mounted,
    side,
    sideOffset,
    align,
    alignOffset,
    arrowPadding,
    collisionBoundary,
    collisionPadding,
    sticky,
    disableAnchorTracking,
    keepMounted,
    floatingRootContext,
    collisionAvoidance,
    nodeId,
    // Allows the menu to remain anchored without wobbling while its size
    // and position transition simultaneously when side=top or side=left.
    adaptiveOrigin
  });
  const defaultProps = React.useMemo(() => {
    const hiddenStyles = {};
    if (!open) {
      hiddenStyles.pointerEvents = "none";
    }
    return {
      role: "presentation",
      hidden: !mounted,
      style: {
        ...positioning.positionerStyles,
        ...hiddenStyles
      }
    };
  }, [open, mounted, positioning.positionerStyles]);
  const state = React.useMemo(() => ({
    open,
    side: positioning.side,
    align: positioning.align,
    anchorHidden: positioning.anchorHidden,
    instant
  }), [open, positioning.side, positioning.align, positioning.anchorHidden, instant]);
  React.useEffect(() => {
    if (!open) {
      return void 0;
    }
    function handleResize() {
      ReactDOM.flushSync(() => {
        setInstant(true);
      });
      resizeTimeout.start(100, () => {
        setInstant(false);
      });
    }
    const win = getWindow(positionerElement);
    win.addEventListener("resize", handleResize);
    return () => {
      win.removeEventListener("resize", handleResize);
    };
  }, [open, resizeTimeout, positionerElement]);
  const element = useRenderElement("div", componentProps, {
    state,
    ref: [forwardedRef, setPositionerElement, positionerRef],
    props: [defaultProps, getDisabledMountTransitionStyles(transitionStatus), elementProps],
    stateAttributesMapping: popupStateMapping
  });
  return /* @__PURE__ */ jsx(NavigationMenuPositionerContext.Provider, {
    value: positioning,
    children: element
  });
});
if (process.env.NODE_ENV !== "production") NavigationMenuPositioner.displayName = "NavigationMenuPositioner";
export {
  NavigationMenuPositioner
};
//# sourceMappingURL=NavigationMenuPositioner.js.map
