/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-29d58ba | Cube Dev Team */
import { TooltipProvider } from "../overlays/Tooltip/TooltipProvider.js";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { jsx } from "react/jsx-runtime";

//#region src/components/content/use-auto-tooltip.tsx
function useAutoTooltip({ tooltip, children, labelProps, isDynamicLabel = false }) {
	const isAutoTooltipEnabled = useMemo(() => {
		if (typeof children !== "string") return false;
		if (tooltip === true) return true;
		if (typeof tooltip === "object") {
			if (tooltip.title) return tooltip.auto === true;
			return !!(tooltip.auto !== void 0 ? tooltip.auto : true);
		}
		return false;
	}, [tooltip, children]);
	const externalLabelRef = labelProps?.ref;
	const [isLabelOverflowed, setIsLabelOverflowed] = useState(false);
	const elementRef = useRef(null);
	const resizeObserverRef = useRef(null);
	const checkLabelOverflow = useCallback(() => {
		const label = elementRef.current;
		if (!label) {
			setIsLabelOverflowed(false);
			return;
		}
		setIsLabelOverflowed(label.scrollWidth > label.clientWidth);
	}, []);
	useEffect(() => {
		if (isAutoTooltipEnabled) checkLabelOverflow();
	}, [isAutoTooltipEnabled, checkLabelOverflow]);
	const handleLabelElementRef = useCallback((element) => {
		if (externalLabelRef) if (typeof externalLabelRef === "function") externalLabelRef(element);
		else externalLabelRef.current = element;
		if (resizeObserverRef.current) {
			try {
				resizeObserverRef.current.disconnect();
			} catch {}
			resizeObserverRef.current = null;
		}
		elementRef.current = element;
		if (element && isAutoTooltipEnabled) {
			const obs = new ResizeObserver(() => {
				checkLabelOverflow();
			});
			resizeObserverRef.current = obs;
			obs.observe(element);
			checkLabelOverflow();
		} else setIsLabelOverflowed(false);
	}, [
		externalLabelRef,
		isAutoTooltipEnabled,
		checkLabelOverflow
	]);
	useEffect(() => {
		return () => {
			if (resizeObserverRef.current) {
				try {
					resizeObserverRef.current.disconnect();
				} catch {}
				resizeObserverRef.current = null;
			}
			elementRef.current = null;
		};
	}, []);
	const finalLabelProps = useMemo(() => {
		const props = { ...labelProps || {} };
		delete props.ref;
		return props;
	}, [labelProps]);
	const renderWithTooltip = (renderElement, defaultTooltipPlacement) => {
		if (tooltip) {
			if (typeof tooltip === "string") return /* @__PURE__ */ jsx(TooltipProvider, {
				placement: defaultTooltipPlacement,
				title: tooltip,
				children: (triggerProps, ref) => renderElement(triggerProps, ref)
			});
			if (tooltip === true) {
				if ((children || labelProps) && (isLabelOverflowed || isDynamicLabel)) return /* @__PURE__ */ jsx(TooltipProvider, {
					placement: defaultTooltipPlacement,
					title: children,
					isDisabled: !isLabelOverflowed && isDynamicLabel,
					children: (triggerProps, ref) => renderElement(triggerProps, ref)
				});
			}
			if (typeof tooltip === "object") {
				const { auto, ...tooltipProps } = tooltip;
				if (tooltipProps.title && auto !== true) return /* @__PURE__ */ jsx(TooltipProvider, {
					placement: defaultTooltipPlacement,
					...tooltipProps,
					children: (triggerProps, ref) => renderElement(triggerProps, ref)
				});
				if ((children || labelProps) && (isLabelOverflowed || isDynamicLabel)) return /* @__PURE__ */ jsx(TooltipProvider, {
					placement: defaultTooltipPlacement,
					title: tooltipProps.title ?? children,
					isDisabled: !isLabelOverflowed && isDynamicLabel && tooltipProps.isDisabled !== true,
					...tooltipProps,
					children: (triggerProps, ref) => renderElement(triggerProps, ref)
				});
			}
		}
		return renderElement();
	};
	return {
		labelRef: handleLabelElementRef,
		labelProps: finalLabelProps,
		isLabelOverflowed,
		isAutoTooltipEnabled,
		hasTooltip: !!tooltip,
		renderWithTooltip
	};
}

//#endregion
export { useAutoTooltip };
//# sourceMappingURL=use-auto-tooltip.js.map