import { n as e, t } from "../../selection-U41Ub3cL.js";
import { n, r } from "../../sorting-CclEKVQk.js";
import { t as i } from "../../utils-B7XIe3qq.js";
import { n as a, r as o, t as s } from "../../arrow-up-BhjinjWg.js";
import * as c from "react";
import { jsx as l, jsxs as u } from "react/jsx-runtime";
//#region src/react/primitives/data-table.tsx
function d({ className: d, columns: f, data: p, onSort: m, onRowClick: h, sort: g, emptyMessage: _ = "No data available", loading: v = !1, selectable: y = !1, selectedRows: b, onSelectionChange: x, rowClassName: S, ...C }) {
	let [w, T] = c.useState(null), E = g === void 0 ? w : g, [D, O] = c.useState(/* @__PURE__ */ new Set()), k = b ?? D, A = c.useCallback((e) => {
		b === void 0 && O(e), x?.(e);
	}, [b, x]), j = c.useCallback((e) => {
		let t = n(E?.key ?? null, E?.direction ?? null, e);
		g === void 0 && T(t), m?.(t);
	}, [
		E,
		g,
		m
	]), M = c.useMemo(() => {
		if (E === null) return p;
		let { key: e, direction: t } = E;
		return r(p, t, (t) => t[e]);
	}, [p, E]), N = p.length > 0 && k.size === p.length, P = c.useCallback(() => {
		A(t(p.map((e, t) => t), k));
	}, [
		p,
		k,
		A
	]), F = c.useCallback((t) => {
		A(e(t, k));
	}, [k, A]), [I, L] = c.useState(""), R = c.useCallback((e) => {
		j(e);
		let t = f.find((t) => t.key === e);
		if (t) {
			let r = n(E?.key ?? null, E?.direction ?? null, e);
			L(r ? `Sorted by ${t.label} ${r.direction === "asc" ? "ascending" : "descending"}` : `${t.label} sort cleared`);
		}
	}, [
		j,
		f,
		E
	]);
	return /* @__PURE__ */ u("div", {
		"data-slot": "data-table",
		className: i("relative w-full", d),
		...C,
		children: [
			/* @__PURE__ */ l("div", {
				className: "sr-only",
				role: "status",
				"aria-live": "polite",
				"aria-atomic": "true",
				children: I
			}),
			v && /* @__PURE__ */ l("div", {
				className: "absolute inset-0 z-10 flex items-center justify-center rounded-xl bg-surface-primary/60 backdrop-blur-sm",
				children: /* @__PURE__ */ l("span", {
					className: "text-foreground-muted text-sm animate-pulse",
					children: "Loading..."
				})
			}),
			/* @__PURE__ */ l("div", {
				className: "relative w-full overflow-x-auto rounded-xl border border-border-glass",
				children: /* @__PURE__ */ u("table", {
					role: "grid",
					"aria-rowcount": p.length,
					"aria-colcount": f.length,
					className: "w-full caption-bottom text-sm",
					children: [/* @__PURE__ */ l("thead", {
						className: "[&_tr]:border-b",
						children: /* @__PURE__ */ u("tr", {
							className: "border-b border-border-glass",
							children: [y && /* @__PURE__ */ l("th", {
								className: "w-10 px-3 py-3 text-left align-middle",
								children: /* @__PURE__ */ l("input", {
									type: "checkbox",
									checked: N,
									onChange: P,
									"aria-label": "Select all rows",
									className: "accent-primary"
								})
							}), f.map((e) => /* @__PURE__ */ l("th", {
								className: i("text-foreground-secondary h-11 px-4 text-left align-middle text-xs font-semibold uppercase tracking-wider", e.sortable === !0 && "cursor-pointer select-none hover:text-foreground transition-colors", e.headerClassName),
								style: e.width === void 0 ? void 0 : { width: e.width },
								role: "columnheader",
								onClick: e.sortable === !0 ? () => {
									R(e.key);
								} : void 0,
								"aria-sort": E?.key === e.key ? E.direction === "asc" ? "ascending" : "descending" : void 0,
								children: /* @__PURE__ */ u("span", {
									className: "inline-flex items-center gap-1.5",
									children: [e.label, e.sortable === !0 && /* @__PURE__ */ l("span", {
										className: "text-foreground-muted inline-flex",
										children: E?.key === e.key ? E.direction === "asc" ? /* @__PURE__ */ l(s, { className: "size-3.5" }) : /* @__PURE__ */ l(o, { className: "size-3.5" }) : /* @__PURE__ */ l(a, { className: "size-3.5 opacity-40" })
									})]
								})
							}, e.key))]
						})
					}), /* @__PURE__ */ l("tbody", {
						className: "[&_tr:last-child]:border-0",
						children: M.length === 0 ? /* @__PURE__ */ l("tr", { children: /* @__PURE__ */ l("td", {
							colSpan: f.length + (y ? 1 : 0),
							className: "px-4 py-10 text-center text-foreground-muted text-sm",
							children: _
						}) }) : M.map((e, t) => /* @__PURE__ */ u("tr", {
							role: "row",
							"aria-rowindex": t + 2,
							className: i("border-b border-border-glass transition-colors", "hover:bg-glass-hover", h !== void 0 && "cursor-pointer", k.has(t) && "bg-primary/5", S?.(e, t)),
							"data-state": k.has(t) ? "selected" : void 0,
							onClick: h === void 0 ? void 0 : () => {
								h(e, t);
							},
							children: [y && /* @__PURE__ */ l("td", {
								className: "w-10 px-3 py-3 align-middle",
								children: /* @__PURE__ */ l("input", {
									type: "checkbox",
									checked: k.has(t),
									onChange: (e) => {
										e.stopPropagation(), F(t);
									},
									onClick: (e) => {
										e.stopPropagation();
									},
									"aria-label": `Select row ${String(t + 1)}`,
									className: "accent-primary"
								})
							}), f.map((n) => {
								let r = e[n.key];
								return /* @__PURE__ */ l("td", {
									role: "gridcell",
									className: i("px-4 py-3 align-middle text-foreground", n.cellClassName),
									children: n.cell ? n.cell(r, e, t) : r == null ? "" : String(r)
								}, n.key);
							})]
						}, t))
					})]
				})
			})
		]
	});
}
//#endregion
export { d as DataTable };

//# sourceMappingURL=data-table.js.map