/** @license MIT | @cube-dev/ui-kit v0.135.1 | Cube Dev Team */
import { useCallback, useEffect, useRef, useState } from "react";

//#region src/components/content/Layout/hooks/useTinyScrollbar.ts
const MIN_HANDLE_SIZE = 20;
const SCROLL_VISIBILITY_DURATION = 1e3;
const OVERFLOW_THRESHOLD = 2;
function useTinyScrollbar(ref, enabled) {
	const [scrollState, setScrollState] = useState({
		hasOverflowY: false,
		hasOverflowX: false,
		scrollTop: 0,
		scrollLeft: 0,
		scrollHeight: 0,
		scrollWidth: 0,
		clientHeight: 0,
		clientWidth: 0
	});
	const [isScrolling, setIsScrolling] = useState(false);
	const scrollTimeoutRef = useRef(null);
	const resizeFrameRef = useRef(null);
	const updateScrollState = useCallback(() => {
		const element = ref.current;
		if (!element) return;
		const { scrollTop, scrollLeft, scrollHeight, scrollWidth, clientHeight, clientWidth } = element;
		const hasOverflowY = scrollHeight > clientHeight + OVERFLOW_THRESHOLD;
		const hasOverflowX = scrollWidth > clientWidth + OVERFLOW_THRESHOLD;
		setScrollState((prev) => {
			if (prev.hasOverflowY === hasOverflowY && prev.hasOverflowX === hasOverflowX && prev.scrollTop === scrollTop && prev.scrollLeft === scrollLeft && prev.scrollHeight === scrollHeight && prev.scrollWidth === scrollWidth && prev.clientHeight === clientHeight && prev.clientWidth === clientWidth) return prev;
			return {
				hasOverflowY,
				hasOverflowX,
				scrollTop,
				scrollLeft,
				scrollHeight,
				scrollWidth,
				clientHeight,
				clientWidth
			};
		});
	}, [ref]);
	useEffect(() => {
		if (!enabled) return;
		const element = ref.current;
		if (!element) return;
		updateScrollState();
		const showScrollbarBriefly = () => {
			setIsScrolling(true);
			if (scrollTimeoutRef.current) clearTimeout(scrollTimeoutRef.current);
			scrollTimeoutRef.current = setTimeout(() => {
				setIsScrolling(false);
			}, SCROLL_VISIBILITY_DURATION);
		};
		const handleScroll = () => {
			updateScrollState();
			showScrollbarBriefly();
		};
		const handleMouseEnter = () => {
			showScrollbarBriefly();
		};
		element.addEventListener("scroll", handleScroll, { passive: true });
		element.addEventListener("mouseenter", handleMouseEnter);
		const resizeObserver = new ResizeObserver(() => {
			if (resizeFrameRef.current) cancelAnimationFrame(resizeFrameRef.current);
			resizeFrameRef.current = requestAnimationFrame(() => {
				updateScrollState();
				showScrollbarBriefly();
			});
		});
		resizeObserver.observe(element);
		return () => {
			element.removeEventListener("scroll", handleScroll);
			element.removeEventListener("mouseenter", handleMouseEnter);
			resizeObserver.disconnect();
			if (scrollTimeoutRef.current) clearTimeout(scrollTimeoutRef.current);
			if (resizeFrameRef.current) cancelAnimationFrame(resizeFrameRef.current);
		};
	}, [
		enabled,
		ref,
		updateScrollState
	]);
	const calculateVHandle = useCallback(() => {
		const { scrollHeight, clientHeight, scrollTop, hasOverflowY } = scrollState;
		if (!hasOverflowY || !enabled) return {
			top: 0,
			height: 0
		};
		const trackHeight = clientHeight - 2;
		const ratio = clientHeight / scrollHeight;
		let handleHeight = Math.max(MIN_HANDLE_SIZE, Math.round(trackHeight * ratio));
		const availableTrack = trackHeight - handleHeight;
		const scrollRatio = scrollHeight - clientHeight > 0 ? scrollTop / (scrollHeight - clientHeight) : 0;
		return {
			top: 1 + Math.round(availableTrack * scrollRatio),
			height: handleHeight
		};
	}, [scrollState, enabled]);
	const calculateHHandle = useCallback(() => {
		const { scrollWidth, clientWidth, scrollLeft, hasOverflowX } = scrollState;
		if (!hasOverflowX || !enabled) return {
			left: 0,
			width: 0
		};
		const trackWidth = clientWidth - 2;
		const ratio = clientWidth / scrollWidth;
		let handleWidth = Math.max(MIN_HANDLE_SIZE, Math.round(trackWidth * ratio));
		const availableTrack = trackWidth - handleWidth;
		const scrollRatio = scrollWidth - clientWidth > 0 ? scrollLeft / (scrollWidth - clientWidth) : 0;
		return {
			left: 1 + Math.round(availableTrack * scrollRatio),
			width: handleWidth
		};
	}, [scrollState, enabled]);
	const vHandle = calculateVHandle();
	const hHandle = calculateHHandle();
	const { scrollTop, scrollLeft, scrollHeight, scrollWidth, clientHeight, clientWidth, hasOverflowY, hasOverflowX } = scrollState;
	const isAtStartY = !hasOverflowY || scrollTop <= 0;
	const isAtEndY = !hasOverflowY || scrollTop >= scrollHeight - clientHeight - 1;
	const isAtStartX = !hasOverflowX || scrollLeft <= 0;
	const isAtEndX = !hasOverflowX || scrollLeft >= scrollWidth - clientWidth - 1;
	return {
		handleVStyle: {
			"--scrollbar-v-top": `${vHandle.top}px`,
			"--scrollbar-v-height": `${vHandle.height}px`
		},
		handleHStyle: {
			"--scrollbar-h-left": `${hHandle.left}px`,
			"--scrollbar-h-width": `${hHandle.width}px`
		},
		hasOverflowY: scrollState.hasOverflowY,
		hasOverflowX: scrollState.hasOverflowX,
		isScrolling,
		isAtStartX,
		isAtStartY,
		isAtEndX,
		isAtEndY
	};
}

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