/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-d4ad2ea | Cube Dev Team */
import { TooltipContext } from "./context.js";
import { Block } from "../../Block.js";
import { _ActiveZone } from "../../content/ActiveZone/ActiveZone.js";
import { DisplayTransition } from "../../helpers/DisplayTransition/DisplayTransition.js";
import { Portal } from "../../portal/Portal.js";
import { isValidElement, useRef } from "react";
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
import { useOverlayPosition, useTooltipTrigger } from "react-aria";
import { FocusableProvider } from "@react-aria/focus";
import { useTooltipTriggerState } from "react-stately";

//#region src/components/overlays/Tooltip/TooltipTrigger.tsx
const DEFAULT_OFFSET = 8;
const DEFAULT_CROSS_OFFSET = 0;
function isTriggerFunction(value) {
	return typeof value === "function";
}
function isReactElement(value) {
	return isValidElement(value);
}
/**
* TooltipTrigger wraps around a trigger element and a Tooltip. It handles opening and closing
* the Tooltip when the user hovers over or focuses the trigger, and positioning the Tooltip
* relative to the trigger.
*/
function TooltipTrigger(props) {
	let { children, activeWrap, targetRef: externalRef, crossOffset = DEFAULT_CROSS_OFFSET, isDisabled, isMaterial, isLight, offset = DEFAULT_OFFSET, trigger: triggerAction, delay = 250, isOpen, onOpenChange, defaultOpen, disableFocusableProvider } = props;
	const [trigger, tooltip] = Array.isArray(children) ? children : [children];
	if (!isReactElement(tooltip)) throw new Error("CubeUIKit: TooltipTrigger expects the second child to be a valid React element (Tooltip component).");
	if (!isTriggerFunction(trigger) && !isReactElement(trigger) && typeof trigger !== "string") throw new Error("CubeUIKit: TooltipTrigger expects the first child to be a function, React element, or string.");
	if (activeWrap && true) console.warn("CubeUIKit: The `activeWrap` prop is deprecated. Use function-as-first-child pattern instead.");
	let internalRef = useRef(null);
	let tooltipTriggerRef = externalRef ?? internalRef;
	let overlayRef = useRef(null);
	let state = useTooltipTriggerState({
		delay,
		...props,
		isDismissable: false
	});
	let { triggerProps, tooltipProps } = useTooltipTrigger({
		trigger: triggerAction,
		delay,
		isOpen,
		onOpenChange,
		isDismissable: false,
		defaultOpen
	}, state, tooltipTriggerRef);
	let { overlayProps, arrowProps, placement, updatePosition } = useOverlayPosition({
		placement: props.placement || "top",
		targetRef: tooltipTriggerRef,
		overlayRef,
		offset,
		crossOffset,
		isDismissable: false,
		isOpen: state.isOpen
	});
	const tooltipContextValue = {
		state,
		placement: placement || props.placement || "top",
		ref: overlayRef,
		overlayProps,
		arrowProps,
		minOffset: "var(--gap)",
		minScale: "1",
		isMaterial,
		isLight,
		updatePosition,
		...tooltipProps
	};
	if (isTriggerFunction(trigger)) return /* @__PURE__ */ jsxs(Fragment$1, { children: [trigger(triggerProps, tooltipTriggerRef), /* @__PURE__ */ jsx(DisplayTransition, {
		isShown: state.isOpen,
		children: ({ phase, isShown, ref: transitionRef }) => /* @__PURE__ */ jsx(TooltipContext.Provider, {
			value: {
				...tooltipContextValue,
				phase,
				isShown,
				ref: overlayRef,
				transitionRef
			},
			children: /* @__PURE__ */ jsx(Portal, { children: tooltip })
		})
	})] });
	if (!activeWrap && typeof trigger === "string") {
		console.warn("CubeUIKit: Tooltips are only supported on elements that are both focusable and hoverable. To solve this issue you can: 1) Use active element as a trigger (`Button`, `Link`, etc); 2) Use function-as-first-child pattern to manually apply trigger props; 3) Use `ActiveZone` component to manually wrap the content.");
		return /* @__PURE__ */ jsx(Block, { children: trigger });
	}
	function onClick(e) {
		e?.currentTarget?.parentNode.click();
	}
	const triggerContent = /* @__PURE__ */ jsxs(Fragment$1, { children: [activeWrap ? /* @__PURE__ */ jsx(_ActiveZone, {
		onClick,
		children: trigger
	}) : trigger, /* @__PURE__ */ jsx(DisplayTransition, {
		isShown: state.isOpen && !isDisabled,
		children: ({ phase, isShown, ref: transitionRef }) => /* @__PURE__ */ jsx(TooltipContext.Provider, {
			value: {
				...tooltipContextValue,
				phase,
				isShown,
				ref: overlayRef,
				transitionRef
			},
			children: /* @__PURE__ */ jsx(Portal, { children: tooltip })
		})
	})] });
	if (disableFocusableProvider) return triggerContent;
	return /* @__PURE__ */ jsx(FocusableProvider, {
		...triggerProps,
		ref: tooltipTriggerRef,
		children: triggerContent
	});
}

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