"use client";
import * as React from "react";
import { CompositeRoot } from "../../composite/root/CompositeRoot.js";
import { useNavigationMenuRootContext } from "../root/NavigationMenuRootContext.js";
import { NAVIGATION_MENU_TRIGGER_IDENTIFIER } from "../utils/constants.js";
import { NavigationMenuDismissContext } from "./NavigationMenuDismissContext.js";
import { getEmptyRootContext } from "../../floating-ui-react/utils/getEmptyRootContext.js";
import { useRenderElement } from "../../utils/useRenderElement.js";
import { jsx } from "react/jsx-runtime";
import { useDismiss } from "../../floating-ui-react/hooks/useDismiss.js";
import { EMPTY_OBJECT } from "../../../../utils/esm/empty.js";
import { getTarget } from "../../floating-ui-react/utils/element.js";
const NavigationMenuList = /* @__PURE__ */ React.forwardRef(function NavigationMenuList2(componentProps, forwardedRef) {
  const {
    className,
    render,
    ...elementProps
  } = componentProps;
  const {
    orientation,
    open,
    floatingRootContext,
    positionerElement,
    value,
    nested
  } = useNavigationMenuRootContext();
  const fallbackContext = React.useMemo(() => getEmptyRootContext(), []);
  const context = floatingRootContext || fallbackContext;
  const interactionsEnabled = positionerElement ? true : !value;
  const dismiss = useDismiss(context, {
    enabled: interactionsEnabled,
    outsidePressEvent: "intentional",
    outsidePress(event) {
      const target = getTarget(event);
      const closestNavigationMenuTrigger = target?.closest(`[${NAVIGATION_MENU_TRIGGER_IDENTIFIER}]`);
      return closestNavigationMenuTrigger === null;
    }
  });
  const dismissProps = floatingRootContext ? dismiss : void 0;
  const state = React.useMemo(() => ({
    open
  }), [open]);
  const defaultProps = nested ? {} : {
    onKeyDown(event) {
      const shouldStop = orientation === "horizontal" && (event.key === "ArrowLeft" || event.key === "ArrowRight") || orientation === "vertical" && (event.key === "ArrowUp" || event.key === "ArrowDown");
      if (shouldStop) {
        event.stopPropagation();
      }
    }
  };
  const props = [dismissProps?.floating || EMPTY_OBJECT, defaultProps, elementProps];
  const element = useRenderElement("ul", componentProps, {
    state,
    ref: forwardedRef,
    props,
    enabled: nested
  });
  if (nested) {
    return /* @__PURE__ */ jsx(NavigationMenuDismissContext.Provider, {
      value: dismissProps,
      children: element
    });
  }
  return /* @__PURE__ */ jsx(NavigationMenuDismissContext.Provider, {
    value: dismissProps,
    children: /* @__PURE__ */ jsx(CompositeRoot, {
      render,
      className,
      state,
      refs: [forwardedRef],
      props,
      loopFocus: false,
      orientation,
      tag: "ul"
    })
  });
});
if (process.env.NODE_ENV !== "production") NavigationMenuList.displayName = "NavigationMenuList";
export {
  NavigationMenuList
};
//# sourceMappingURL=NavigationMenuList.js.map
