import * as i from "react";
import { useMenuRootContext as D } from "../root/MenuRootContext.js";
import { useMenuPositionerContext as I } from "../positioner/MenuPositionerContext.js";
import { useRenderElement as N } from "../../utils/useRenderElement.js";
import { popupStateMapping as w } from "../../utils/popupStateMapping.js";
import { transitionStatusMapping as G } from "../../utils/stateAttributesMapping.js";
import { useOpenChangeComplete as K } from "../../utils/useOpenChangeComplete.js";
import { createChangeEventDetails as _ } from "../../utils/createBaseUIEventDetails.js";
import { useToolbarRootContext as j } from "../../toolbar/root/ToolbarRootContext.js";
import { COMPOSITE_KEYS as k } from "../../composite/composite.js";
import { getDisabledMountTransitionStyles as A } from "../../utils/getDisabledMountTransitionStyles.js";
import { jsx as H } from "react/jsx-runtime";
import { useHoverFloatingInteraction as V } from "../../floating-ui-react/hooks/useHoverFloatingInteraction.js";
import { FloatingFocusManager as Y } from "../../floating-ui-react/components/FloatingFocusManager.js";
import { outsidePress as q } from "../../utils/reason-parts.js";
const z = {
  ...w,
  ...G
}, B = /* @__PURE__ */ i.forwardRef(function(p, S) {
  const {
    render: L,
    className: Q,
    finalFocus: c,
    ...y
  } = p, {
    store: e
  } = D(), {
    side: m,
    align: l
  } = I(), C = j() != null, s = e.useState("open"), a = e.useState("transitionStatus"), b = e.useState("popupProps"), x = e.useState("mounted"), f = e.useState("instantType"), E = e.useState("activeTriggerElement"), t = e.useState("parent"), R = e.useState("lastOpenChangeReason"), F = e.useState("rootId"), d = e.useState("floatingRootContext"), r = e.useState("floatingTreeRoot"), M = e.useState("closeDelay"), T = e.useState("activeTriggerElement"), u = t.type === "context-menu";
  K({
    open: s,
    ref: e.context.popupRef,
    onComplete() {
      var o, n;
      s && ((n = (o = e.context).onOpenChangeComplete) == null || n.call(o, !0));
    }
  }), i.useEffect(() => {
    function o(n) {
      e.setOpen(!1, _(n.reason, n.domEvent));
    }
    return r.events.on("close", o), () => {
      r.events.off("close", o);
    };
  }, [r.events, e]);
  const v = e.useState("hoverEnabled"), h = e.useState("disabled");
  V(d, {
    enabled: v && !h && !u && t.type !== "menubar",
    closeDelay: M
  });
  const P = i.useMemo(() => ({
    transitionStatus: a,
    side: m,
    align: l,
    open: s,
    nested: t.type === "menu",
    instant: f
  }), [a, m, l, s, t.type, f]), O = N("div", p, {
    state: P,
    ref: [S, e.context.popupRef],
    stateAttributesMapping: z,
    props: [b, {
      onKeyDown(o) {
        C && k.has(o.key) && o.stopPropagation();
      }
    }, A(a), y, {
      "data-rootownerid": F
    }]
  });
  let g = t.type === void 0 || u;
  return (E || t.type === "menubar" && R !== q) && (g = !0), /* @__PURE__ */ H(Y, {
    context: d,
    modal: u,
    disabled: !x,
    returnFocus: c === void 0 ? g : c,
    initialFocus: t.type !== "menu",
    restoreFocus: !0,
    externalTree: t.type !== "menubar" ? r : void 0,
    previousFocusableElement: T,
    nextFocusableElement: t.type === void 0 ? e.context.triggerFocusTargetRef : void 0,
    beforeContentFocusGuardRef: t.type === void 0 ? e.context.beforeContentFocusGuardRef : void 0,
    children: O
  });
});
process.env.NODE_ENV !== "production" && (B.displayName = "MenuPopup");
export {
  B as MenuPopup
};
