import { DataTableBody as e, DataTableEmptyRow as t, DataTableHeader as n, DataTableRoot as r, DataTableRow as i } from "./DataTable.js";
import { resolveInfiniteRootMargin as a, shouldTriggerInfiniteLoad as o } from "../../internal/infinite/infiniteLoad.js";
import { bodyViewport as s, sentinel as c, spacer as l } from "./VirtualizedConnectionTable.css.js";
import { clamp as ee, computeVirtualWindowBounds as te, isVerticallyScrollable as u, shouldTriggerVirtualizedInfiniteLoad as ne } from "../../internal/data-table/virtualization.js";
import { useCallback as d, useEffect as f, useLayoutEffect as p, useMemo as m, useRef as h, useState as re } from "react";
import { Fragment as ie, jsx as g, jsxs as _ } from "react/jsx-runtime";
//#region src/components/data-table/VirtualizedConnectionTable.tsx
var v = (e) => {
	let t = e;
	for (; t != null;) {
		if (u({
			overflowY: window.getComputedStyle(t).overflowY,
			scrollHeight: t.scrollHeight,
			clientHeight: t.clientHeight
		})) return t;
		t = t.parentElement;
	}
	return null;
}, y = ({ columns: u, rows: y, getRowId: b, emptyState: ae, className: oe, headerClassName: se, bodyClassName: ce, rowClassName: x, gridTemplateClassName: S, gridTemplateColumns: C, kind: w, density: T, headerBehavior: le, rowState: E, classes: D, bodyScrollMode: O = "page", bodyFooterNode: ue, virtualization: k, infiniteScroll: A, hasNextPage: j = !1, isLoadingMore: M = !1, onLoadMore: N, ariaLabel: de, ariaLabelledBy: fe }) => {
	let P = h(null), F = h(null), I = h(null), L = h(null), R = h(null), z = h(null), B = h(!1), V = k?.enabled === !0, H = m(() => {
		if (!V) return null;
		let e = k.rowHeightPx;
		return typeof e == "number" && Number.isFinite(e) && e > 0 ? e : 56;
	}, [V, k]), U = m(() => {
		if (!V) return 0;
		let e = 10, t = k.overscan;
		return typeof t == "number" && Number.isFinite(t) && (e = t), Math.max(0, e);
	}, [V, k]), W = A?.thresholdPx ?? 800, G = A?.autoLoad ?? !0, K = A?.enabled === !0, q = O === "contained", J = K && j && !M && N != null, Y = d(() => {
		N?.();
	}, [N]), [pe, me] = re(() => ({
		startIndex: 0,
		endIndex: Math.min(y.length, 50)
	})), X = d((e, t, n) => {
		let r = {};
		return typeof n == "number" && (r.height = n), /* @__PURE__ */ g(i, {
			row: e,
			index: t,
			columns: u,
			getRowId: b,
			kind: w,
			density: T,
			rowClassName: x,
			rowState: E,
			gridTemplateClassName: S,
			gridTemplateColumns: C,
			classes: D,
			style: r,
			cellMode: "singleLine"
		}, b(e, t));
	}, [
		D,
		u,
		T,
		b,
		S,
		C,
		w,
		x,
		E
	]), Z = d(() => {
		if (!V) return;
		let e = H;
		if (e == null) return;
		let t = I.current;
		if (t == null) return;
		let n = F.current, r = R.current, i = r?.clientHeight ?? window.innerHeight, a = 0;
		if (q && n != null) i = n.clientHeight, a = n.scrollTop;
		else {
			let e = r?.getBoundingClientRect().top ?? 0, n = t.getBoundingClientRect().top - e;
			a = Math.max(0, -n);
		}
		let s = a + i, c = y.length * e, l = ee(s, 0, c), { startIndex: u, endIndex: d } = te({
			rowCount: y.length,
			rowHeightPx: e,
			overscan: U,
			visibleTop: a,
			visibleBottom: s
		});
		me((e) => e.startIndex === u && e.endIndex === d ? e : {
			startIndex: u,
			endIndex: d
		}), o({
			enabled: K,
			hasNext: j,
			isLoading: M,
			autoLoad: G,
			isCoolingDown: B.current
		}) && J && ne({
			autoLoad: G,
			canLoadMore: J,
			totalHeight: c,
			visibleBottom: l,
			thresholdPx: W,
			isCoolingDown: B.current
		}) && (B.current = !0, Y(), window.setTimeout(() => {
			B.current = !1;
		}, 250));
	}, [
		G,
		J,
		j,
		q,
		V,
		K,
		M,
		U,
		H,
		y.length,
		W,
		Y
	]), Q = d(() => {
		z.current ??= window.requestAnimationFrame(() => {
			z.current = null, Z();
		});
	}, [Z]);
	p(() => {
		if (!V) return;
		let e = P.current;
		e != null && (q ? R.current = F.current : R.current = v(e), Q());
	}, [
		q,
		V,
		Q
	]), f(() => {
		if (!V) return;
		let e = R.current ?? window, t = () => {
			Q();
		};
		return e.addEventListener("scroll", t, { passive: !0 }), window.addEventListener("resize", t), () => {
			e.removeEventListener("scroll", t), window.removeEventListener("resize", t);
		};
	}, [V, Q]), f(() => {
		if (V || !o({
			enabled: K,
			hasNext: j,
			isLoading: M,
			autoLoad: G,
			isCoolingDown: !1
		}) || !J) return;
		let e = L.current;
		if (e == null || typeof window > "u" || typeof IntersectionObserver > "u") return;
		let t = R.current, n = new IntersectionObserver((e) => {
			let [t] = e;
			t != null && t.isIntersecting && o({
				enabled: K,
				hasNext: j,
				isLoading: M,
				autoLoad: G,
				isCoolingDown: B.current
			}) && (B.current = !0, Y(), window.setTimeout(() => {
				B.current = !1;
			}, 250));
		}, {
			root: t,
			rootMargin: a({ thresholdPx: W })
		});
		return n.observe(e), () => {
			n.disconnect();
		};
	}, [
		G,
		J,
		j,
		K,
		V,
		M,
		W,
		Y
	]), p(() => {
		if (V) return;
		let e = P.current;
		e != null && (q ? R.current = F.current : R.current = v(e));
	}, [q, V]);
	let $;
	if (y.length === 0) $ = /* @__PURE__ */ g(t, {
		emptyState: ae,
		density: T,
		classes: D
	});
	else if (V && H != null) {
		let { startIndex: e, endIndex: t } = pe, n = e * H, r = (y.length - t) * H, i = y.slice(e, t);
		$ = /* @__PURE__ */ _("div", {
			className: s,
			ref: I,
			children: [
				/* @__PURE__ */ g("div", {
					className: l,
					style: { height: n }
				}),
				i.map((t, n) => X(t, e + n, H)),
				/* @__PURE__ */ g("div", {
					className: l,
					style: { height: r }
				})
			]
		});
	} else $ = /* @__PURE__ */ g(ie, { children: y.map((e, t) => X(e, t, null)) });
	let he = null;
	return !V && K && G && (he = /* @__PURE__ */ g("div", {
		ref: L,
		className: c
	})), /* @__PURE__ */ _(r, {
		ref: P,
		className: oe,
		kind: w,
		bodyScrollMode: O,
		classes: D,
		ariaLabel: de,
		ariaLabelledBy: fe,
		children: [/* @__PURE__ */ g(n, {
			columns: u,
			kind: w,
			density: T,
			headerBehavior: le,
			headerClassName: se,
			gridTemplateClassName: S,
			gridTemplateColumns: C,
			classes: D
		}), /* @__PURE__ */ _(e, {
			ref: F,
			className: ce,
			kind: w,
			bodyScrollMode: O,
			classes: D,
			children: [
				$,
				he,
				ue
			]
		})]
	});
};
//#endregion
export { y as VirtualizedConnectionTable };

//# sourceMappingURL=VirtualizedConnectionTable.js.map