import { c as e, h as t } from "../chunks/misc-aGQBhywJ.js";
import { t as n } from "../chunks/grid-dI94AAbi.js";
import r, { Children as i, useLayoutEffect as a, useReducer as o, useRef as s, useState as c } from "react";
import { cn as l } from "@ryuzaki13/react-foundation-lib/utils";
import { jsx as u } from "react/jsx-runtime";
import { flushSync as d } from "react-dom";
import { useFetchNextPageEffect as f } from "@ryuzaki13/react-foundation-lib/virtualizer";
import { Virtualizer as p, elementScroll as m, observeElementOffset as h, observeElementRect as g } from "@tanstack/react-virtual";
//#region src/list/useListVirtualizer.ts
function _(e) {
	"use no memo";
	let t = o((e) => e + 1, 0)[1], [n] = c(() => new p({
		...e,
		observeElementRect: g,
		observeElementOffset: h,
		scrollToFn: m,
		onChange: (e, n) => {
			if (n) {
				d(t);
				return;
			}
			t();
		}
	}));
	return n.setOptions({
		...e,
		observeElementRect: g,
		observeElementOffset: h,
		scrollToFn: m,
		onChange: (e, n) => {
			if (n) {
				d(t);
				return;
			}
			t();
		}
	}), a(() => n._didMount(), [n]), a(() => n._willUpdate()), n;
}
//#endregion
//#region src/list/List.tsx
var v = ({ children: e, className: t }) => i.count(e) === 0 ? /* @__PURE__ */ u("div", {}) : /* @__PURE__ */ u(n.Container, {
	gap: "sm",
	className: l("paddingSm borderBottom", t),
	children: e
}), y = ({ children: e, className: t }) => i.count(e) === 0 ? /* @__PURE__ */ u("div", {}) : /* @__PURE__ */ u(n.Container, {
	gap: "sm",
	className: l("paddingSm borderTop", t),
	children: e
});
function b({ item: e, render: t, separated: n }) {
	return /* @__PURE__ */ u(r.Fragment, { children: /* @__PURE__ */ u("li", {
		className: l(n && "borderBottom"),
		children: t(e)
	}) });
}
function x({ items: t, getKey: n, render: r, separated: i, className: a }) {
	return /* @__PURE__ */ u(e, {
		className: a,
		children: /* @__PURE__ */ u("ul", {
			className: "",
			children: t.map((e, t) => /* @__PURE__ */ u(b, {
				item: e,
				render: r,
				separated: i
			}, n(e, t)))
		})
	});
}
function S({ items: n, getKey: i, render: a, separated: o, className: c, isLoading: d, hasNextPage: p, fetchNextPage: m }) {
	"use no memo";
	let h = s(null), g = _({
		count: p ? n.length + 1 : n.length,
		getScrollElement: () => h.current,
		measureElement: (e) => Math.ceil(e.getBoundingClientRect().height),
		estimateSize: () => 120,
		overscan: 5
	}), v = g.getVirtualItems();
	return f({
		virtualItems: v,
		currentItemsCount: n.length,
		hasNextPage: p,
		fetchNextPage: m
	}), /* @__PURE__ */ u(e, {
		ref: h,
		className: c,
		children: d ? /* @__PURE__ */ u(t, {}) : /* @__PURE__ */ u("ul", {
			style: { height: g.getTotalSize() },
			className: "relative w100",
			children: v.map((e) => {
				let s = e.index > n.length - 1, c = n[e.index];
				return /* @__PURE__ */ u(r.Fragment, { children: /* @__PURE__ */ u("li", {
					ref: g.measureElement,
					style: {
						position: "absolute",
						top: 0,
						left: 0,
						width: "100%",
						transform: `translateY(${Math.round(e.start)}px)`
					},
					"data-index": e.index,
					className: l(o && "borderBottom"),
					children: s ? /* @__PURE__ */ u(t, {}) : a(c)
				}) }, s ? "loader" : i(c, e.index));
			})
		})
	});
}
var C = ({ className: e, children: t }) => /* @__PURE__ */ u(n.Container, {
	templateRows: "auto 1fr auto",
	className: l("h100", e),
	children: t
});
C.Toolbar = v, C.Content = x, C.Footer = y, C.VirtualizedContent = S;
//#endregion
export { C as List };

//# sourceMappingURL=index.js.map