/** @license MIT | @cube-dev/ui-kit v0.142.7 | Cube Dev Team */
import { SlotProvider } from "../../../utils/react/Slots.js";
import { usePopoverSync } from "../../../utils/react/usePopoverSync.js";
import { useEvent } from "../../../_internal/hooks/use-event.js";
import { MenuContext } from "./context.js";
import { generateRandomId } from "../../../utils/random.js";
import { _Tray } from "../../overlays/Modal/Tray.js";
import { _Popover } from "../../overlays/Modal/Popover.js";
import { Fragment, forwardRef, useEffect, useMemo, useRef } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { DismissButton, FocusScope, useMenuTrigger, useOverlayPosition } from "react-aria";
import { useIsMobileDevice } from "@react-spectrum/utils";
import { useMenuTriggerState } from "react-stately";
import { useObjectRef } from "@react-aria/utils";
import { PressResponder } from "@react-aria/interactions";

//#region src/components/actions/Menu/MenuTrigger.tsx
function MenuTrigger(props, ref) {
	const internalPopoverRef = useRef(null);
	const menuPopoverRef = props.popoverRef ?? internalPopoverRef;
	const triggerRef = useRef(null);
	const domRef = useObjectRef(ref);
	const menuTriggerRef = props.targetRef || domRef || triggerRef;
	const menuRef = useRef(null);
	const wasOpenRef = useRef(false);
	const { children, shouldFlip = true, closeOnSelect, trigger = "press", isDisabled, isDummy, mobileType = "popover" } = props;
	const menuId = useMemo(() => generateRandomId(), []);
	if (!Array.isArray(children) || children.length > 2) throw new Error("MenuTrigger must have exactly 2 children");
	let [menuTrigger, menu] = children;
	const state = useMenuTriggerState(props);
	usePopoverSync({
		menuId,
		isOpen: state.isOpen,
		onClose: () => state.close(),
		enabled: !isDummy,
		triggerRef: menuTriggerRef,
		containerRef: menuPopoverRef
	});
	useEffect(() => {
		if (!state.isOpen && wasOpenRef.current && !isDummy) {
			wasOpenRef.current = false;
			setTimeout(() => {
				menuTriggerRef.current?.focus();
			}, 0);
		} else if (state.isOpen) wasOpenRef.current = true;
	}, [
		state.isOpen,
		menuTriggerRef,
		isDummy
	]);
	if (typeof menuTrigger === "function") menuTrigger = menuTrigger(state);
	const { menuTriggerProps, menuProps } = useMenuTrigger({ isDisabled }, state, menuTriggerRef);
	let initialPlacement = props.placement ?? "bottom start";
	const isMobileDevice = useIsMobileDevice();
	const isTray = mobileType === "tray" && isMobileDevice;
	const { overlayProps: positionProps, placement } = useOverlayPosition({
		targetRef: menuTriggerRef,
		overlayRef: menuPopoverRef,
		scrollRef: menuRef,
		placement: initialPlacement,
		shouldFlip,
		isOpen: state.isOpen && !isTray,
		onClose: state.close,
		containerPadding: props.containerPadding,
		offset: props.offset ?? 8,
		crossOffset: props.crossOffset ?? 0
	});
	const menuContext = {
		...menuProps,
		ref: menuRef,
		onClose: state.close,
		closeOnSelect,
		autoFocus: state.focusStrategy ?? "first",
		style: isTray ? {
			width: "100%",
			maxHeight: "inherit"
		} : void 0,
		mods: {
			popover: !isTray,
			tray: isTray
		},
		isClosing: !state.isOpen
	};
	const contents = /* @__PURE__ */ jsxs(FocusScope, {
		restoreFocus: true,
		children: [
			/* @__PURE__ */ jsx(DismissButton, { onDismiss: state.close }),
			menu,
			/* @__PURE__ */ jsx(DismissButton, { onDismiss: state.close })
		]
	});
	const shouldCloseOnInteractOutside = useEvent((el) => {
		if (!state.isOpen) return false;
		const menuTriggerEl = el.closest("[data-popover-trigger]");
		if (!menuTriggerEl) {
			if (el.closest("[data-popover-keep]")) return false;
			if (el.closest("[data-popover-dismiss]")) {
				setTimeout(() => state.close(), 0);
				return false;
			}
			return true;
		}
		if (isDummy && (menuTriggerEl === menuTriggerRef.current || menuTriggerRef.current?.contains(el))) return true;
		if (menuTriggerEl === menuTriggerRef.current) return true;
		return false;
	});
	let overlay;
	if (isTray) overlay = /* @__PURE__ */ jsx(_Tray, {
		isOpen: state.isOpen,
		shouldCloseOnInteractOutside,
		onClose: state.close,
		children: contents
	});
	else overlay = /* @__PURE__ */ jsx(_Popover, {
		ref: menuPopoverRef,
		hideArrow: true,
		isNonModal: true,
		isOpen: state.isOpen,
		style: positionProps.style,
		placement,
		shouldCloseOnInteractOutside,
		onClose: state.close,
		children: contents
	});
	return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(SlotProvider, {
		slots: { actionButton: { holdAffordance: trigger === "longPress" } },
		children: !isDummy ? /* @__PURE__ */ jsx(PressResponder, {
			...menuTriggerProps,
			ref: menuTriggerRef,
			"data-popover-trigger": true,
			isPressed: state.isOpen,
			children: menuTrigger
		}) : null
	}), /* @__PURE__ */ jsx(MenuContext.Provider, {
		value: menuContext,
		children: overlay
	})] });
}
/**
* The MenuTrigger serves as a wrapper around a Menu and its associated trigger,
* linking the Menu's open state with the trigger's press state.
*/
let _MenuTrigger = forwardRef(MenuTrigger);
_MenuTrigger.displayName = "MenuTrigger";

//#endregion
export { _MenuTrigger };
//# sourceMappingURL=MenuTrigger.js.map