import { cx as e } from "../../theme/tools.js";
import { useUiTranslation as t } from "../../i18n/useUiTranslation.js";
import { alignRecipe as n, body as r, cell as i, container as a, emptyRow as o, header as s, headerCell as c, headerRow as l, hideBelowRecipe as u, primaryCell as d, primaryHeaderCell as ee, row as te, rowEven as ne, rowOdd as re } from "./DataTable.css.js";
import { bodyViewport as ie, sentinel as ae, singleLineCell as oe, spacer as f } from "./VirtualizedConnectionTable.css.js";
import { useCallback as p, useEffect as m, useLayoutEffect as se, useMemo as h, useRef as g, useState as ce } from "react";
import { jsx as _, jsxs as v } from "react/jsx-runtime";
//#region src/components/data-table/VirtualizedConnectionTable.tsx
var y = "minmax(0, 1fr)", b = (e, t) => typeof t == "string" && t !== "" ? { gridTemplateColumns: t } : { gridTemplateColumns: `repeat(${e}, ${y})` }, x = (e) => e == null ? null : u({ minVisibleAt: e }), S = ({ overflowY: e, scrollHeight: t, clientHeight: n }) => e !== "auto" && e !== "scroll" && e !== "overlay" ? !1 : t > n + 1, C = (e) => {
	let t = e;
	for (; t != null;) {
		if (S({
			overflowY: window.getComputedStyle(t).overflowY,
			scrollHeight: t.scrollHeight,
			clientHeight: t.clientHeight
		})) return t;
		t = t.parentElement;
	}
	return null;
}, w = (e, t, n) => Math.min(n, Math.max(t, e)), T = ({ autoLoad: e, canLoadMore: t, totalHeight: n, visibleBottom: r, thresholdPx: i, isCoolingDown: a }) => !e || !t || a ? !1 : n - r <= i, E = ({ rowCount: e, rowHeightPx: t, overscan: n, visibleTop: r, visibleBottom: i }) => {
	let a = e * t, o = w(i, 0, a), s = w(r, 0, a), c = Math.floor(s / t) - n, l = Math.ceil(o / t) + n, u = w(c, 0, e);
	return {
		startIndex: u,
		endIndex: w(l, u, e)
	};
}, D = ({ columns: u, rows: y, getRowId: S, emptyState: D, className: O, gridTemplateColumns: le, kind: ue, classes: k, virtualization: A, infiniteScroll: j, hasNextPage: de = !1, isLoadingMore: fe = !1, onLoadMore: M, ariaLabel: pe, ariaLabelledBy: N }) => {
	let { t: P } = t(), F = ue ?? "default", I = pe ?? P("common.table.label");
	N != null && (I = void 0);
	let L = g(null), R = g(null), z = g(null), B = g(null), V = g(null), H = g(!1), U = A?.enabled === !0, W = h(() => {
		if (!U) return null;
		let e = A.rowHeightPx;
		return typeof e == "number" && Number.isFinite(e) && e > 0 ? e : 56;
	}, [U, A]), G = h(() => {
		if (!U) return 0;
		let e = 10, t = A.overscan;
		return typeof t == "number" && Number.isFinite(t) && (e = t), Math.max(0, e);
	}, [U, A]), K = j?.thresholdPx ?? 800, q = j?.autoLoad ?? !0, J = j?.enabled === !0, Y = J && de && !fe && M != null, X = p(() => {
		M?.();
	}, [M]), [me, he] = ce(() => ({
		startIndex: 0,
		endIndex: Math.min(y.length, 50)
	})), Z = h(() => b(u.length, le), [u.length, le]), ge = p((t, r, a) => {
		let o = S(t, r), s = [te({ kind: F }), k?.row];
		r % 2 == 0 ? s.push(ne({ kind: F }), k?.rowEven) : s.push(re({ kind: F }), k?.rowOdd);
		let c = { ...Z };
		return typeof a == "number" && (c.height = a), /* @__PURE__ */ _("div", {
			role: "row",
			className: e(...s),
			style: c,
			children: u.map((r) => {
				let a = [
					i({ kind: F }),
					oe,
					k?.cell
				], o = x(r.minVisibleAt);
				return o != null && a.push(o), r.className != null && a.push(r.className), r.isPrimary && a.push(d, k?.primaryCell), r.align != null && a.push(n({ align: r.align })), /* @__PURE__ */ _("div", {
					role: "cell",
					className: e(...a),
					children: r.cell(t)
				}, r.id);
			})
		}, o);
	}, [
		k,
		u,
		S,
		Z,
		F
	]), _e = p(() => {
		if (!U) return;
		let e = W;
		if (e == null) return;
		let t = R.current;
		if (t == null) return;
		let n = B.current, r = n?.clientHeight ?? window.innerHeight, i = n?.getBoundingClientRect().top ?? 0, a = t.getBoundingClientRect().top - i, o = Math.max(0, -a), s = o + r, c = y.length * e, l = w(s, 0, c), { startIndex: u, endIndex: d } = E({
			rowCount: y.length,
			rowHeightPx: e,
			overscan: G,
			visibleTop: o,
			visibleBottom: s
		});
		he((e) => e.startIndex === u && e.endIndex === d ? e : {
			startIndex: u,
			endIndex: d
		}), T({
			autoLoad: q,
			canLoadMore: Y,
			totalHeight: c,
			visibleBottom: l,
			thresholdPx: K,
			isCoolingDown: H.current
		}) && (H.current = !0, X(), window.setTimeout(() => {
			H.current = !1;
		}, 250));
	}, [
		q,
		Y,
		U,
		G,
		W,
		y.length,
		K,
		X
	]), Q = p(() => {
		V.current ??= window.requestAnimationFrame(() => {
			V.current = null, _e();
		});
	}, [_e]);
	se(() => {
		if (!U) return;
		let e = L.current;
		e != null && (B.current = C(e), Q());
	}, [U, Q]), m(() => {
		if (!U) return;
		let e = B.current ?? window, t = () => {
			Q();
		};
		return e.addEventListener("scroll", t, { passive: !0 }), window.addEventListener("resize", t), () => {
			e.removeEventListener("scroll", t), window.removeEventListener("resize", t);
		};
	}, [U, Q]), m(() => {
		if (U || !J || !q || !Y) return;
		let e = z.current;
		if (e == null) return;
		let t = B.current, n = new IntersectionObserver((e) => {
			let [t] = e;
			t != null && t.isIntersecting && !H.current && (H.current = !0, X(), window.setTimeout(() => {
				H.current = !1;
			}, 250));
		}, {
			root: t,
			rootMargin: `0px 0px ${K}px 0px`
		});
		return n.observe(e), () => {
			n.disconnect();
		};
	}, [
		q,
		Y,
		J,
		U,
		K,
		X
	]), se(() => {
		if (U) return;
		let e = L.current;
		e != null && (B.current = C(e));
	}, [U]);
	let ve = /* @__PURE__ */ _("div", {
		className: e(s({ kind: F }), k?.header),
		role: "rowgroup",
		children: /* @__PURE__ */ _("div", {
			className: e(l, k?.headerRow),
			role: "row",
			style: Z,
			children: u.map((t) => {
				let r = [c({ kind: F }), k?.headerCell], i = x(t.minVisibleAt);
				return i != null && r.push(i), t.className != null && r.push(t.className), t.isPrimary && r.push(ee, k?.primaryHeaderCell), t.align != null && r.push(n({ align: t.align })), /* @__PURE__ */ _("div", {
					role: "columnheader",
					className: e(...r),
					children: t.header
				}, t.id);
			})
		})
	}), $;
	if (y.length === 0) $ = /* @__PURE__ */ _("div", {
		className: e(o, k?.emptyRow),
		role: "row",
		children: /* @__PURE__ */ _("div", {
			role: "cell",
			children: D ?? /* @__PURE__ */ _("div", {
				className: e("ysn84621 txvbqb97 txvbqbva1 txvbqbt6y", k?.emptyCell),
				children: P("common.table.empty")
			})
		})
	});
	else if (U && W != null) {
		let { startIndex: e, endIndex: t } = me, n = e * W, r = (y.length - t) * W, i = y.slice(e, t);
		$ = /* @__PURE__ */ v("div", {
			className: ie,
			ref: R,
			children: [
				/* @__PURE__ */ _("div", {
					className: f,
					style: { height: n }
				}),
				i.map((t, n) => ge(t, e + n, W)),
				/* @__PURE__ */ _("div", {
					className: f,
					style: { height: r }
				})
			]
		});
	} else $ = /* @__PURE__ */ _("div", {
		ref: R,
		children: y.map((e, t) => ge(e, t, null))
	});
	let ye = null;
	return !U && J && q && (ye = /* @__PURE__ */ _("div", {
		ref: z,
		className: ae
	})), /* @__PURE__ */ v("div", {
		ref: L,
		className: e(a({ kind: F }), k?.container, O),
		role: "table",
		"aria-label": I,
		"aria-labelledby": N,
		children: [ve, /* @__PURE__ */ v("div", {
			className: e(r({ kind: F }), k?.body),
			role: "rowgroup",
			children: [$, ye]
		})]
	});
}, O = {
	buildInlineTemplate: b,
	getVisibilityClass: x,
	isVerticallyScrollable: S,
	findScrollParent: C,
	clamp: w,
	shouldTriggerInfiniteLoad: T,
	computeVirtualWindowBounds: E
};
//#endregion
export { D as VirtualizedConnectionTable, O as __test };

//# sourceMappingURL=VirtualizedConnectionTable.js.map