import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
//#region src/plus/clinetSearch/InfiniteScroll/InfiniteScroll.tsx
const InfiniteScroll = ({ dataLength, next, hasMore, loader = /* @__PURE__ */ React.createElement("div", null, "Loading..."), endMessage, height, scrollThreshold = .8, onScroll, style, className, scrollableTarget, children, inverse = false }) => {
	const [isLoading, setIsLoading] = useState(false);
	const containerRef = useRef(null);
	const lastDataLength = useRef(dataLength);
	const scrollTimeoutRef = useRef(null);
	const isMountedRef = useRef(true);
	const lastCallTimeRef = useRef(0);
	const throttle = useCallback((func, delay) => {
		return (...args) => {
			if (scrollTimeoutRef.current) clearTimeout(scrollTimeoutRef.current);
			scrollTimeoutRef.current = setTimeout(() => func(...args), delay);
		};
	}, []);
	const getScrollThreshold = useCallback(() => {
		if (typeof scrollThreshold === "string" && scrollThreshold.endsWith("px")) return parseInt(scrollThreshold.replace("px", ""), 10);
		return typeof scrollThreshold === "number" ? scrollThreshold : .8;
	}, [scrollThreshold]);
	const checkScrollPosition = useCallback((element) => {
		if (!element || !hasMore || isLoading) return;
		const threshold = getScrollThreshold();
		const { scrollTop, scrollHeight, clientHeight } = element;
		if (scrollHeight <= 0 || clientHeight <= 0) {
			console.warn("InfiniteScroll: 检测到无效的滚动容器尺寸");
			return;
		}
		const normalizedScrollTop = Math.max(0, scrollTop);
		let shouldLoadMore = false;
		if (inverse) if (typeof threshold === "number" && threshold < 1) shouldLoadMore = normalizedScrollTop / scrollHeight <= 1 - threshold;
		else shouldLoadMore = normalizedScrollTop <= threshold;
		else if (typeof threshold === "number" && threshold < 1) shouldLoadMore = (normalizedScrollTop + clientHeight) / scrollHeight >= threshold;
		else shouldLoadMore = scrollHeight - normalizedScrollTop - clientHeight <= threshold;
		if (shouldLoadMore) {
			const now = Date.now();
			if (now - lastCallTimeRef.current < 500) return;
			lastCallTimeRef.current = now;
			setIsLoading(true);
			try {
				next();
			} catch (error) {
				console.error("InfiniteScroll: 加载更多数据时发生错误:", error);
				if (isMountedRef.current) setIsLoading(false);
			}
		}
	}, [
		hasMore,
		isLoading,
		next,
		getScrollThreshold,
		inverse
	]);
	const checkInitialLoad = useCallback(() => {
		const element = scrollableTarget ? document.getElementById(scrollableTarget) : containerRef.current;
		if (!element || !hasMore || isLoading) return;
		const { scrollHeight, clientHeight } = element;
		if (clientHeight <= 0) {
			console.warn("InfiniteScroll: 容器高度无效，可能容器尚未渲染完成");
			return;
		}
		if (scrollHeight <= clientHeight || dataLength === 0) {
			const now = Date.now();
			if (now - lastCallTimeRef.current < 500) return;
			lastCallTimeRef.current = now;
			setIsLoading(true);
			try {
				next();
			} catch (error) {
				console.error("InfiniteScroll: 初始加载时发生错误:", error);
				if (isMountedRef.current) setIsLoading(false);
			}
		}
	}, [
		hasMore,
		isLoading,
		next,
		scrollableTarget,
		dataLength
	]);
	const handleScrollInternal = useCallback((event) => {
		try {
			const element = event.target;
			if (!element || typeof element.scrollTop === "undefined") {
				console.warn("InfiniteScroll: 无效的滚动目标元素");
				return;
			}
			checkScrollPosition(element);
			if (onScroll) onScroll(event);
		} catch (error) {
			console.error("InfiniteScroll: 滚动事件处理时发生错误:", error);
		}
	}, [checkScrollPosition, onScroll]);
	const handleScroll = useMemo(() => throttle(handleScrollInternal, 16), [throttle, handleScrollInternal]);
	useEffect(() => {
		if (dataLength !== lastDataLength.current) {
			if (isMountedRef.current) setIsLoading(false);
			lastDataLength.current = dataLength;
		}
	}, [dataLength]);
	useEffect(() => {
		return () => {
			isMountedRef.current = false;
		};
	}, []);
	useEffect(() => {
		return () => {
			if (scrollTimeoutRef.current) clearTimeout(scrollTimeoutRef.current);
		};
	}, []);
	useEffect(() => {
		const timeoutId = setTimeout(() => {
			checkInitialLoad();
		}, 100);
		return () => clearTimeout(timeoutId);
	}, [checkInitialLoad, dataLength]);
	useEffect(() => {
		if (!isLoading && hasMore) {
			const timeoutId = setTimeout(() => {
				checkInitialLoad();
			}, 100);
			return () => clearTimeout(timeoutId);
		}
	}, [
		isLoading,
		hasMore,
		checkInitialLoad
	]);
	useEffect(() => {
		const element = scrollableTarget ? document.getElementById(scrollableTarget) : containerRef.current;
		if (!element) {
			console.warn("InfiniteScroll: 无法找到滚动容器元素");
			return;
		}
		const scrollHandler = handleScroll;
		element.addEventListener("scroll", scrollHandler, { passive: true });
		return () => {
			if (element) element.removeEventListener("scroll", scrollHandler);
		};
	}, [handleScroll, scrollableTarget]);
	const containerStyle = useMemo(() => _objectSpread2(_objectSpread2(_objectSpread2({}, style), height && {
		height: typeof height === "number" ? `${height}px` : height,
		overflowY: "auto"
	}), inverse && {
		display: "flex",
		flexDirection: "column-reverse"
	}), [
		style,
		height,
		inverse
	]);
	return /* @__PURE__ */ React.createElement("div", {
		ref: !scrollableTarget ? containerRef : void 0,
		className,
		style: containerStyle
	}, inverse ? /* @__PURE__ */ React.createElement(React.Fragment, null, isLoading && hasMore && loader, !hasMore && endMessage, children) : /* @__PURE__ */ React.createElement(React.Fragment, null, children, isLoading && hasMore && loader, !hasMore && endMessage));
};
//#endregion
export { InfiniteScroll as default };
