/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-f1f3d22 | Cube Dev Team */
import { useCombinedRefs } from "../../../utils/react/useCombinedRefs.js";
import { _Modal } from "../Modal/Modal.js";
import { _Tray } from "../Modal/Tray.js";
import { _Popover } from "../Modal/Popover.js";
import { DialogContext } from "./context.js";
import { Fragment, useEffect, useRef } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { useOverlayPosition, useOverlayTrigger } from "react-aria";
import { useMediaQuery } from "@react-spectrum/utils";
import { useOverlayTriggerState } from "react-stately";
import { PressResponder } from "@react-aria/interactions";

//#region src/components/overlays/Dialog/DialogTrigger.tsx
/**
* DialogTrigger serves as a wrapper around a Dialog and its associated trigger, linking the Dialog's
* open state with the trigger's press state. Additionally, it allows you to customize the type and
* positioning of the Dialog.
*/
function DialogTrigger(props) {
	let { children, type = "modal", mobileType = type === "popover" ? "modal" : type, hideArrow, targetRef, onDismiss, isDismissable = true, isKeyboardDismissDisabled, styles, mobileViewport = 700, hideOnClose, shouldCloseOnInteractOutside, ...positionProps } = props;
	if (!Array.isArray(children) || children.length > 2) throw new Error("DialogTrigger must have exactly 2 children");
	let [trigger, content] = children;
	if (useMediaQuery(`(max-width: ${mobileViewport}px)`)) {
		if (type !== "modal" && mobileType === "modal") isDismissable = true;
		type = mobileType;
	}
	let state = useOverlayTriggerState(props);
	if (typeof trigger === "function") trigger = trigger(state);
	let wasOpen = useRef(false);
	let isExiting = useRef(false);
	let onExiting = () => isExiting.current = true;
	let onExited = () => isExiting.current = false;
	wasOpen.current = state.isOpen;
	useEffect(() => {
		return () => {
			if ((wasOpen.current || isExiting.current) && type !== "popover" && type !== "tray") console.warn("CubeUIKit: A DialogTrigger unmounted while open. This is likely due to being placed within a trigger that unmounts or inside a conditional. Consider using a DialogContainer instead.");
		};
	}, []);
	function onClose(action) {
		if (isDismissable) {
			onDismiss && onDismiss(action);
			state.close();
		}
	}
	if (type === "popover") return /* @__PURE__ */ jsx(PopoverTrigger, {
		...positionProps,
		hideOnClose,
		state,
		styles,
		targetRef,
		trigger,
		content,
		isKeyboardDismissDisabled,
		hideArrow,
		shouldCloseOnInteractOutside,
		onClose
	});
	let renderOverlay = () => {
		switch (type) {
			case "panel":
			case "fullscreen":
			case "fullscreenTakeover":
			case "modal": return /* @__PURE__ */ jsx(_Modal, {
				hideOnClose,
				isOpen: state.isOpen,
				isDismissable,
				type,
				isKeyboardDismissDisabled,
				styles,
				shouldCloseOnInteractOutside,
				onClose,
				onExiting,
				onExited,
				children: typeof content === "function" ? content(state.close) : content
			});
			case "tray": return /* @__PURE__ */ jsx(_Tray, {
				hideOnClose,
				isOpen: state.isOpen,
				isKeyboardDismissDisabled,
				styles,
				onClose,
				children: typeof content === "function" ? content(state.close) : content
			});
		}
	};
	return /* @__PURE__ */ jsx(DialogTriggerBase, {
		type,
		state,
		isDismissable,
		trigger,
		overlay: renderOverlay(),
		hideOnClose,
		onClose
	});
}
function PopoverTrigger(allProps) {
	let { state, styles, targetRef, trigger, content, hideArrow, onClose, isKeyboardDismissDisabled, hideOnClose, shouldCloseOnInteractOutside, keepOpenOnScroll, ...props } = allProps;
	let triggerRef = useRef(null);
	let overlayRef = useRef(null);
	let { overlayProps: popoverProps, placement, arrowProps, updatePosition } = useOverlayPosition({
		targetRef: targetRef || triggerRef,
		overlayRef,
		placement: props.placement,
		containerPadding: props.containerPadding,
		offset: props.offset || 8,
		crossOffset: props.crossOffset,
		shouldFlip: props.shouldFlip,
		shouldUpdatePosition: props.shouldUpdatePosition,
		isOpen: state.isOpen
	});
	let overlayTriggerState = state;
	if (keepOpenOnScroll) overlayTriggerState = {
		...state,
		close: updatePosition
	};
	let { triggerProps, overlayProps } = useOverlayTrigger({ type: "dialog" }, overlayTriggerState, triggerRef);
	return /* @__PURE__ */ jsx(DialogTriggerBase, {
		type: "popover",
		state,
		triggerProps: {
			...triggerProps,
			ref: targetRef ? void 0 : triggerRef
		},
		dialogProps: overlayProps,
		trigger,
		overlay: /* @__PURE__ */ jsx(_Popover, {
			ref: overlayRef,
			styles,
			hideOnClose,
			isOpen: state.isOpen,
			style: popoverProps.style,
			placement,
			arrowProps,
			isKeyboardDismissDisabled,
			hideArrow,
			updatePosition,
			shouldCloseOnInteractOutside,
			onClose,
			children: typeof content === "function" ? content(state.close) : content
		}),
		onClose
	});
}
function DialogTriggerBase(props) {
	const ref = useCombinedRefs(props.ref);
	const wasOpenRef = useRef(false);
	let { type, state, onClose, isDismissable, dialogProps = {}, triggerProps = {}, overlay, trigger } = props;
	let context = {
		type,
		onClose,
		isDismissable,
		isOpen: state.isOpen,
		...dialogProps
	};
	useEffect(() => {
		if (!state.isOpen && wasOpenRef.current) {
			wasOpenRef.current = false;
			ref.current?.focus();
		} else if (state.isOpen) wasOpenRef.current = true;
	}, [state.isOpen]);
	return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(PressResponder, {
		ref,
		...triggerProps,
		isPressed: state.isOpen && type !== "modal" && type !== "fullscreen" && type !== "fullscreenTakeover",
		onPress: state.toggle,
		children: trigger
	}), /* @__PURE__ */ jsx(DialogContext.Provider, {
		value: context,
		children: overlay
	})] });
}

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