"use client";
import * as React from "react";
import * as ReactDOM from "react-dom";
import { inertValue } from "../../../../utils/esm/inertValue.js";
import { useNavigationMenuRootContext, useNavigationMenuTreeContext } from "../root/NavigationMenuRootContext.js";
import { useNavigationMenuItemContext } from "../item/NavigationMenuItemContext.js";
import { useTransitionStatus } from "../../utils/useTransitionStatus.js";
import { useOpenChangeComplete } from "../../utils/useOpenChangeComplete.js";
import { transitionStatusMapping } from "../../utils/stateAttributesMapping.js";
import { CompositeRoot } from "../../composite/root/CompositeRoot.js";
import { popupStateMapping } from "../../utils/popupStateMapping.js";
import { jsx } from "react/jsx-runtime";
import { FloatingNode } from "../../floating-ui-react/components/FloatingTree.js";
import { contains, getTarget } from "../../floating-ui-react/utils/element.js";
const stateAttributesMapping = {
  ...popupStateMapping,
  ...transitionStatusMapping,
  activationDirection(value) {
    if (!value) {
      return null;
    }
    return {
      "data-activation-direction": value
    };
  }
};
const NavigationMenuContent = /* @__PURE__ */ React.forwardRef(function NavigationMenuContent2(componentProps, forwardedRef) {
  const {
    className,
    render,
    ...elementProps
  } = componentProps;
  const {
    mounted: popupMounted,
    viewportElement,
    value,
    activationDirection,
    currentContentRef,
    viewportTargetElement
  } = useNavigationMenuRootContext();
  const {
    value: itemValue
  } = useNavigationMenuItemContext();
  const nodeId = useNavigationMenuTreeContext();
  const open = popupMounted && value === itemValue;
  const ref = React.useRef(null);
  const [focusInside, setFocusInside] = React.useState(false);
  const {
    mounted,
    setMounted,
    transitionStatus
  } = useTransitionStatus(open);
  useOpenChangeComplete({
    ref,
    open,
    onComplete() {
      if (!open) {
        setMounted(false);
      }
    }
  });
  const state = React.useMemo(() => ({
    open,
    transitionStatus,
    activationDirection
  }), [open, transitionStatus, activationDirection]);
  const handleCurrentContentRef = React.useCallback((node) => {
    if (node) {
      currentContentRef.current = node;
    }
  }, [currentContentRef]);
  const commonProps = {
    onFocus(event) {
      const target = getTarget(event.nativeEvent);
      if (target?.hasAttribute("data-base-ui-focus-guard")) {
        return;
      }
      setFocusInside(true);
    },
    onBlur(event) {
      if (!contains(event.currentTarget, event.relatedTarget)) {
        setFocusInside(false);
      }
    }
  };
  const defaultProps = !open && mounted ? {
    style: {
      position: "absolute",
      top: 0,
      left: 0
    },
    inert: inertValue(!focusInside),
    ...commonProps
  } : commonProps;
  const portalContainer = viewportTargetElement || viewportElement;
  const shouldRender = portalContainer !== null && mounted;
  if (!portalContainer || !shouldRender) {
    return null;
  }
  return /* @__PURE__ */ ReactDOM.createPortal(/* @__PURE__ */ jsx(FloatingNode, {
    id: nodeId,
    children: /* @__PURE__ */ jsx(CompositeRoot, {
      render,
      className,
      state,
      refs: [forwardedRef, ref, handleCurrentContentRef],
      props: [defaultProps, elementProps],
      stateAttributesMapping
    })
  }), portalContainer);
});
if (process.env.NODE_ENV !== "production") NavigationMenuContent.displayName = "NavigationMenuContent";
export {
  NavigationMenuContent
};
//# sourceMappingURL=NavigationMenuContent.js.map
