const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
let react = require("react");
react = require_runtime.__toESM(react);
//#region src/plus/clinetSearch/InfiniteScroll/InfiniteScroll.tsx
const InfiniteScroll = ({ dataLength, next, hasMore, loader = /* @__PURE__ */ react.default.createElement("div", null, "Loading..."), endMessage, height, scrollThreshold = .8, onScroll, style, className, scrollableTarget, children, inverse = false }) => {
	const [isLoading, setIsLoading] = (0, react.useState)(false);
	const containerRef = (0, react.useRef)(null);
	const lastDataLength = (0, react.useRef)(dataLength);
	const scrollTimeoutRef = (0, react.useRef)(null);
	const isMountedRef = (0, react.useRef)(true);
	const lastCallTimeRef = (0, react.useRef)(0);
	const throttle = (0, react.useCallback)((func, delay) => {
		return (...args) => {
			if (scrollTimeoutRef.current) clearTimeout(scrollTimeoutRef.current);
			scrollTimeoutRef.current = setTimeout(() => func(...args), delay);
		};
	}, []);
	const getScrollThreshold = (0, react.useCallback)(() => {
		if (typeof scrollThreshold === "string" && scrollThreshold.endsWith("px")) return parseInt(scrollThreshold.replace("px", ""), 10);
		return typeof scrollThreshold === "number" ? scrollThreshold : .8;
	}, [scrollThreshold]);
	const checkScrollPosition = (0, react.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 = (0, react.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 = (0, react.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 = (0, react.useMemo)(() => throttle(handleScrollInternal, 16), [throttle, handleScrollInternal]);
	(0, react.useEffect)(() => {
		if (dataLength !== lastDataLength.current) {
			if (isMountedRef.current) setIsLoading(false);
			lastDataLength.current = dataLength;
		}
	}, [dataLength]);
	(0, react.useEffect)(() => {
		return () => {
			isMountedRef.current = false;
		};
	}, []);
	(0, react.useEffect)(() => {
		return () => {
			if (scrollTimeoutRef.current) clearTimeout(scrollTimeoutRef.current);
		};
	}, []);
	(0, react.useEffect)(() => {
		const timeoutId = setTimeout(() => {
			checkInitialLoad();
		}, 100);
		return () => clearTimeout(timeoutId);
	}, [checkInitialLoad, dataLength]);
	(0, react.useEffect)(() => {
		if (!isLoading && hasMore) {
			const timeoutId = setTimeout(() => {
				checkInitialLoad();
			}, 100);
			return () => clearTimeout(timeoutId);
		}
	}, [
		isLoading,
		hasMore,
		checkInitialLoad
	]);
	(0, react.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 = (0, react.useMemo)(() => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_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.default.createElement("div", {
		ref: !scrollableTarget ? containerRef : void 0,
		className,
		style: containerStyle
	}, inverse ? /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, isLoading && hasMore && loader, !hasMore && endMessage, children) : /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, children, isLoading && hasMore && loader, !hasMore && endMessage));
};
//#endregion
exports.default = InfiniteScroll;
