import { cn } from "./index3.js";
import { Button } from "./index6.js";
import { AnimatePresence } from "./index308.js";
import { motion } from "./index359.js";
import { cva } from "class-variance-authority";
import { ArrowDown } from "lucide-react";
import * as React from "react";
import { jsx, jsxs } from "react/jsx-runtime";
//#region src/components/organisms/ai/AIScrollButton/AIScrollButton.tsx
var scrollButtonVariants = cva("fixed flex items-center justify-center shadow-lg transition-all", {
	variants: {
		variant: {
			default: "bg-background hover:bg-muted border border-border",
			primary: "bg-primary/10 hover:bg-primary/90 text-primary-foreground",
			ghost: "bg-card/80 hover:bg-background supports-backdrop-filter:backdrop-blur-sm border border-border"
		},
		size: {
			sm: "size-8 rounded-full",
			md: "size-10 rounded-full",
			lg: "size-12 rounded-xl"
		},
		position: {
			"bottom-center": "bottom-4 start-1/2 -translate-x-1/2 rtl:translate-x-1/2",
			"bottom-right": "bottom-4 end-4",
			"bottom-left": "bottom-4 start-4"
		}
	},
	defaultVariants: {
		variant: "default",
		size: "md",
		position: "bottom-center"
	}
});
function useScrollToBottom({ threshold = 50, behavior = "smooth", autoScroll: autoScrollEnabled = false } = {}) {
	const containerRef = React.useRef(null);
	const anchorRef = React.useRef(null);
	const [isAtBottom, setIsAtBottom] = React.useState(true);
	const [shouldAutoScroll, setShouldAutoScroll] = React.useState(true);
	const scrollToBottom = React.useCallback((overrideBehavior) => {
		const scrollBehavior = overrideBehavior ?? behavior;
		if (anchorRef.current) anchorRef.current.scrollIntoView({ behavior: scrollBehavior });
		else if (containerRef.current) containerRef.current.scrollTop = containerRef.current.scrollHeight;
	}, [behavior]);
	const checkIsAtBottom = React.useCallback(() => {
		const container = containerRef.current;
		if (!container) return true;
		const { scrollTop, scrollHeight, clientHeight } = container;
		return scrollHeight - scrollTop - clientHeight <= threshold;
	}, [threshold]);
	React.useEffect(() => {
		const container = containerRef.current;
		if (!container) return;
		const handleScroll = () => {
			const atBottom = checkIsAtBottom();
			setIsAtBottom((prev) => prev === atBottom ? prev : atBottom);
			if (autoScrollEnabled && atBottom) setShouldAutoScroll((prev) => prev || true);
		};
		handleScroll();
		container.addEventListener("scroll", handleScroll, { passive: true });
		return () => container.removeEventListener("scroll", handleScroll);
	}, [checkIsAtBottom, autoScrollEnabled]);
	React.useEffect(() => {
		const container = containerRef.current;
		if (!container) return;
		const resizeObserver = new ResizeObserver(() => {
			const atBottom = checkIsAtBottom();
			setIsAtBottom((prev) => prev === atBottom ? prev : atBottom);
		});
		resizeObserver.observe(container);
		return () => resizeObserver.disconnect();
	}, [checkIsAtBottom]);
	return {
		isAtBottom,
		scrollToBottom,
		containerRef,
		anchorRef,
		shouldAutoScroll,
		handleUserScroll: React.useCallback(() => {
			if (!checkIsAtBottom()) setShouldAutoScroll(false);
		}, [checkIsAtBottom]),
		triggerAutoScroll: React.useCallback(() => {
			if (autoScrollEnabled && shouldAutoScroll) requestAnimationFrame(() => {
				scrollToBottom("smooth");
			});
		}, [
			autoScrollEnabled,
			shouldAutoScroll,
			scrollToBottom
		])
	};
}
function AIScrollButton({ scrollRef, containerRef, threshold = 50, onClick, visible: controlledVisible, variant, size, position, label = "Scroll to bottom", unreadCount, className }) {
	const [internalVisible, setInternalVisible] = React.useState(false);
	React.useEffect(() => {
		if (controlledVisible !== void 0) return;
		const container = containerRef?.current;
		if (!container) return;
		const handleScroll = () => {
			const { scrollTop, scrollHeight, clientHeight } = container;
			setInternalVisible(scrollHeight - scrollTop - clientHeight > threshold);
		};
		handleScroll();
		container.addEventListener("scroll", handleScroll, { passive: true });
		return () => container.removeEventListener("scroll", handleScroll);
	}, [
		containerRef,
		threshold,
		controlledVisible
	]);
	const isVisible = controlledVisible ?? internalVisible;
	const handleClick = () => {
		if (onClick) onClick();
		else if (scrollRef?.current) scrollRef.current.scrollIntoView({ behavior: "smooth" });
		else if (containerRef?.current) containerRef.current.scrollTop = containerRef.current.scrollHeight;
	};
	const iconSize = size === "sm" ? "size-4" : size === "lg" ? "size-6" : "size-5";
	return /* @__PURE__ */ jsx(AnimatePresence, { children: isVisible && /* @__PURE__ */ jsxs(motion.div, {
		"data-slot": "ai-scroll-button",
		initial: {
			opacity: 0,
			y: 20,
			scale: .9
		},
		animate: {
			opacity: 1,
			y: 0,
			scale: 1
		},
		exit: {
			opacity: 0,
			y: 20,
			scale: .9
		},
		transition: {
			duration: .2,
			ease: [
				.4,
				0,
				.2,
				1
			]
		},
		className: cn(scrollButtonVariants({
			variant,
			size,
			position
		}), "motion-reduce:transition-none", className),
		children: [/* @__PURE__ */ jsx(Button, {
			appearance: "ghost",
			variant: "secondary",
			size: "icon",
			onClick: handleClick,
			className: "size-full rounded-full",
			"aria-label": label,
			children: /* @__PURE__ */ jsx(ArrowDown, { className: cn(iconSize, variant === "primary" ? "text-primary-foreground" : "text-muted-foreground") })
		}), unreadCount !== void 0 && unreadCount > 0 && /* @__PURE__ */ jsx(motion.div, {
			initial: { scale: 0 },
			animate: { scale: 1 },
			transition: {
				type: "spring",
				stiffness: 500,
				damping: 30
			},
			className: "absolute -top-1 -end-1 flex items-center justify-center min-w-[18px] h-[18px] px-1 rounded-full bg-destructive text-destructive-foreground text-xs font-medium motion-reduce:transition-none",
			"aria-label": `${unreadCount > 99 ? "99+" : unreadCount} new messages`,
			children: unreadCount > 99 ? "99+" : unreadCount
		})]
	}) });
}
AIScrollButton.displayName = "AIScrollButton";
var AIScrollAnchor = React.forwardRef(({ className }, ref) => {
	return /* @__PURE__ */ jsx("div", {
		ref,
		className: cn("h-px w-full", className),
		"aria-hidden": true
	});
});
AIScrollAnchor.displayName = "AIScrollAnchor";
//#endregion
export { AIScrollAnchor, AIScrollButton, scrollButtonVariants, useScrollToBottom };

//# sourceMappingURL=index420.js.map