import { cn } from "./cva.js";
import * as React from "react";
import { jsx } from "react/jsx-runtime";
import { useVirtualizer } from "@tanstack/react-virtual";
//#region src/virtual.tsx
function readDocumentDirection() {
	if (typeof document === "undefined") return "ltr";
	return document.documentElement.dir === "rtl" ? "rtl" : "ltr";
}
function VirtualList({ items, estimateSize, renderRow, overscan = 5, getRowKey, className, empty = null }) {
	const parentRef = React.useRef(null);
	const estimateSizeFn = React.useCallback((index) => typeof estimateSize === "function" ? estimateSize(index) : estimateSize, [estimateSize]);
	const virtualizer = useVirtualizer({
		count: items.length,
		getScrollElement: () => parentRef.current,
		estimateSize: estimateSizeFn,
		overscan
	});
	if (items.length === 0) return /* @__PURE__ */ jsx("div", {
		role: "grid",
		"aria-rowcount": 0,
		className,
		children: empty
	});
	const virtualItems = virtualizer.getVirtualItems();
	const direction = readDocumentDirection();
	return /* @__PURE__ */ jsx("div", {
		ref: parentRef,
		role: "grid",
		"aria-rowcount": items.length,
		dir: direction,
		className: cn("overflow-auto", className),
		children: /* @__PURE__ */ jsx("div", {
			style: {
				height: virtualizer.getTotalSize(),
				width: "100%",
				position: "relative"
			},
			children: virtualItems.map((virtualRow) => {
				const item = items[virtualRow.index];
				if (item === void 0) return null;
				const key = getRowKey ? getRowKey(item, virtualRow.index) : virtualRow.index;
				return /* @__PURE__ */ jsx("div", {
					ref: virtualizer.measureElement,
					"data-index": virtualRow.index,
					role: "row",
					"aria-rowindex": virtualRow.index + 1,
					style: {
						position: "absolute",
						top: 0,
						insetInlineStart: 0,
						insetInlineEnd: 0,
						transform: `translateY(${virtualRow.start}px)`
					},
					children: renderRow(item, virtualRow.index)
				}, key);
			})
		})
	});
}
//#endregion
export { VirtualList };
