require("../../../_virtual/_rolldown/runtime.js");
let react = require("react");
//#region src/components/list/hooks/useWaterfall.ts
const useWaterfall = (items, options) => {
	const [recalcVersion, setRecalcVersion] = (0, react.useState)(0);
	const itemSizes = (0, react.useRef)(/* @__PURE__ */ new Map());
	const itemRefs = (0, react.useRef)(/* @__PURE__ */ new Map());
	const registerItemRef = (0, react.useCallback)((id, element) => {
		if (element) itemRefs.current.set(id, element);
		else itemRefs.current.delete(id);
	}, []);
	const measureItemSize = (0, react.useCallback)((id) => {
		const element = itemRefs.current.get(id);
		if (element) {
			const rect = element.getBoundingClientRect();
			const size = {
				width: rect.width,
				height: rect.height
			};
			itemSizes.current.set(id, size);
			return size;
		}
		return itemSizes.current.get(id) || {
			width: 200,
			height: 200
		};
	}, []);
	const getItemSize = (0, react.useCallback)((item) => {
		const { direction } = options;
		if (direction === "vertical") {
			if (item.height) return item.height;
			const measured = itemSizes.current.get(item.id);
			return (measured === null || measured === void 0 ? void 0 : measured.height) || 200;
		} else {
			if (item.width) return item.width;
			const measured = itemSizes.current.get(item.id);
			return (measured === null || measured === void 0 ? void 0 : measured.width) || 200;
		}
	}, [options]);
	const layout = (0, react.useMemo)(() => {
		const { tracks: trackCount, trackGap, itemGap, direction } = options;
		const newTracks = Array.from({ length: trackCount }, () => ({
			items: [],
			size: 0
		}));
		items.forEach((item) => {
			const targetTrack = newTracks[newTracks.reduce((minIndex, track, index, arr) => {
				return track.size < arr[minIndex].size ? index : minIndex;
			}, 0)];
			const itemSize = getItemSize(item);
			targetTrack.items.push(item);
			if (targetTrack.items.length > 1) targetTrack.size += itemGap;
			targetTrack.size += itemSize;
		});
		return {
			tracks: newTracks,
			totalSize: Math.max(...newTracks.map((track) => track.size), 0)
		};
	}, [
		items,
		options,
		recalcVersion,
		getItemSize
	]);
	const recalculateLayout = (0, react.useCallback)(() => {
		let hasChanges = false;
		itemRefs.current.forEach((element, id) => {
			const rect = element.getBoundingClientRect();
			const newSize = {
				width: rect.width,
				height: rect.height
			};
			const oldSize = itemSizes.current.get(id);
			if (!oldSize || oldSize.width !== newSize.width || oldSize.height !== newSize.height) {
				hasChanges = true;
				itemSizes.current.set(id, newSize);
			}
		});
		if (hasChanges) setRecalcVersion((prev) => prev + 1);
	}, []);
	(0, react.useEffect)(() => {
		let timeoutId;
		const handleResize = () => {
			clearTimeout(timeoutId);
			timeoutId = setTimeout(() => {
				recalculateLayout();
			}, 100);
		};
		window.addEventListener("resize", handleResize);
		return () => {
			window.removeEventListener("resize", handleResize);
			clearTimeout(timeoutId);
		};
	}, [recalculateLayout]);
	return {
		layout,
		registerItemRef,
		measureItemSize,
		recalculateLayout
	};
};
//#endregion
exports.default = useWaterfall;
