import * as d from "react";
import { inertValue as Y } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/inertValue.js";
import { MenuPositionerContext as Z } from "./MenuPositionerContext.js";
import { useMenuRootContext as $ } from "../root/MenuRootContext.js";
import { useAnchorPositioning as ee } from "../../utils/useAnchorPositioning.js";
import { useRenderElement as te } from "../../utils/useRenderElement.js";
import { popupStateMapping as ne } from "../../utils/popupStateMapping.js";
import { CompositeList as oe } from "../../composite/list/CompositeList.js";
import { InternalBackdrop as re } from "../../utils/InternalBackdrop.js";
import { useMenuPortalContext as se } from "../portal/MenuPortalContext.js";
import { DROPDOWN_COLLISION_AVOIDANCE as ie, POPUP_COLLISION_AVOIDANCE as ae } from "../../utils/constants.js";
import { useContextMenuRootContext as de } from "../../context-menu/root/ContextMenuRootContext.js";
import { createChangeEventDetails as x } from "../../utils/createBaseUIEventDetails.js";
import { jsxs as le, jsx as I } from "react/jsx-runtime";
import { FloatingNode as fe } from "../../floating-ui-react/components/FloatingTree.js";
import { triggerHover as ce, siblingOpen as g } from "../../utils/reason-parts.js";
const ue = /* @__PURE__ */ d.forwardRef(function(l, E) {
  var M;
  const {
    anchor: v,
    positionMethod: b = "absolute",
    className: me,
    render: ge,
    side: O,
    align: A,
    sideOffset: w = 0,
    alignOffset: D = 0,
    collisionBoundary: k = "clipping-ancestors",
    collisionPadding: L = 5,
    arrowPadding: H = 5,
    sticky: T = !1,
    disableAnchorTracking: V = !1,
    collisionAvoidance: _ = ie,
    ...B
  } = l, {
    store: e
  } = $(), U = se(), j = de(!0), n = e.useState("parent"), F = e.useState("floatingRootContext"), s = e.useState("floatingTreeRoot"), u = e.useState("mounted"), i = e.useState("open"), W = e.useState("modal"), p = e.useState("activeTriggerElement"), q = e.useState("lastOpenChangeReason"), a = e.useState("floatingNodeId"), P = e.useState("floatingParentNodeId");
  let y = v, N = w, S = D, m = A, C = _;
  n.type === "context-menu" && (y = v ?? ((M = n.context) == null ? void 0 : M.anchor), m = m ?? "start", !O && m !== "center" && (S = l.alignOffset ?? 2, N = l.sideOffset ?? -5));
  let f = O, c = m;
  n.type === "menu" ? (f = f ?? "inline-end", c = c ?? "start", C = l.collisionAvoidance ?? ae) : n.type === "menubar" && (f = f ?? "bottom", c = c ?? "start");
  const R = n.type === "context-menu", t = ee({
    anchor: y,
    floatingRootContext: F,
    positionMethod: j ? "fixed" : b,
    mounted: u,
    side: f,
    sideOffset: N,
    align: c,
    alignOffset: S,
    arrowPadding: R ? 0 : H,
    collisionBoundary: k,
    collisionPadding: L,
    sticky: T,
    nodeId: a,
    keepMounted: U,
    disableAnchorTracking: V,
    collisionAvoidance: C,
    shiftCrossAxis: R,
    externalTree: s
  }), z = d.useMemo(() => {
    const r = {};
    return i || (r.pointerEvents = "none"), {
      role: "presentation",
      hidden: !u,
      style: {
        ...t.positionerStyles,
        ...r
      }
    };
  }, [i, u, t.positionerStyles]);
  d.useEffect(() => {
    function r(o) {
      o.open ? (o.parentNodeId === a && e.set("hoverEnabled", !1), o.nodeId !== a && o.parentNodeId === e.select("floatingParentNodeId") && e.setOpen(!1, x(g))) : o.parentNodeId === a && o.reason !== g && e.set("hoverEnabled", !0);
    }
    return s.events.on("menuopenchange", r), () => {
      s.events.off("menuopenchange", r);
    };
  }, [e, s.events, a]), d.useEffect(() => {
    if (e.select("floatingParentNodeId") == null)
      return;
    function r(o) {
      if (o.open || o.nodeId !== e.select("floatingParentNodeId"))
        return;
      const X = o.reason ?? g;
      e.setOpen(!1, x(X));
    }
    return s.events.on("menuopenchange", r), () => {
      s.events.off("menuopenchange", r);
    };
  }, [s.events, e]), d.useEffect(() => {
    function r(o) {
      !i || o.nodeId !== e.select("floatingParentNodeId") || o.target && p && p !== o.target && e.setOpen(!1, x(g));
    }
    return s.events.on("itemhover", r), () => {
      s.events.off("itemhover", r);
    };
  }, [s.events, i, p, e]), d.useEffect(() => {
    const r = {
      open: i,
      nodeId: a,
      parentNodeId: P,
      reason: e.select("lastOpenChangeReason")
    };
    s.events.emit("menuopenchange", r);
  }, [s.events, i, e, a, P]);
  const G = d.useMemo(() => ({
    open: i,
    side: t.side,
    align: t.align,
    anchorHidden: t.anchorHidden,
    nested: n.type === "menu"
  }), [i, t.side, t.align, t.anchorHidden, n.type]), J = d.useMemo(() => ({
    side: t.side,
    align: t.align,
    arrowRef: t.arrowRef,
    arrowUncentered: t.arrowUncentered,
    arrowStyles: t.arrowStyles,
    nodeId: t.context.nodeId
  }), [t.side, t.align, t.arrowRef, t.arrowUncentered, t.arrowStyles, t.context.nodeId]), K = te("div", l, {
    state: G,
    stateAttributesMapping: ne,
    ref: [E, e.useStateSetter("positionerElement")],
    props: [z, B]
  }), Q = u && n.type !== "menu" && (n.type !== "menubar" && W && q !== ce || n.type === "menubar" && n.context.modal);
  let h = null;
  return n.type === "menubar" ? h = n.context.contentElement : n.type === void 0 && (h = p), /* @__PURE__ */ le(Z.Provider, {
    value: J,
    children: [Q && /* @__PURE__ */ I(re, {
      ref: n.type === "context-menu" || n.type === "nested-context-menu" ? n.context.internalBackdropRef : null,
      inert: Y(!i),
      cutout: h
    }), /* @__PURE__ */ I(fe, {
      id: a,
      children: /* @__PURE__ */ I(oe, {
        elementsRef: e.context.itemDomElements,
        labelsRef: e.context.itemLabels,
        children: K
      })
    })]
  });
});
process.env.NODE_ENV !== "production" && (ue.displayName = "MenuPositioner");
export {
  ue as MenuPositioner
};
