import { Button as e } from "../ui/button.js";
import { Card as t, CardContent as n } from "../ui/card.js";
import { Checkbox as r } from "../ui/checkbox.js";
import i from "./ConfirmDialog.js";
import a from "./DataTableBatchAction.js";
import o from "./DataTableFilters.js";
import s from "./DataTablePagination.js";
import c from "./DataTableToolbar.js";
import { useState as l } from "react";
import { jsx as u, jsxs as d } from "react/jsx-runtime";
import { Loader2 as f, Trash2 as p } from "lucide-react";
//#region src/components/data-table/DataList.tsx
function m({ data: m, totalItems: h, loading: g = !1, error: _, page: v, pageSize: y, onPageChange: b, onPageSizeChange: x, searchTerm: S, onSearchChange: C, filters: w = {}, filterConfig: T = {}, onFiltersChange: E, onRowClick: D, onNew: O, enableBatchActions: k = !0, batchActions: A = [], onBatchDelete: j, rowActions: M = [], itemRenderer: N, newButtonLabel: P }) {
	let [F, I] = l([]), [L, R] = l(!1), [z, B] = l({
		isOpen: !1,
		type: "single"
	}), V = () => {
		F.length === m.length ? I([]) : I(m.map((e) => e.id));
	}, H = (e) => {
		I((t) => t.includes(e) ? t.filter((t) => t !== e) : [...t, e]);
	}, U = () => I([]), W = () => {
		B({
			isOpen: !0,
			type: "batch"
		});
	}, G = async () => {
		z.type === "single" && z.itemId || z.type === "batch" && (await j?.(F), U()), B({
			isOpen: !1,
			type: "single"
		});
	}, K = [...j ? [{
		id: "delete",
		label: "Delete Selected",
		icon: /* @__PURE__ */ u(p, { className: "mr-2 h-4 w-4" }),
		variant: "destructive",
		action: W
	}] : [], ...A], q = (e) => {
		e.id === "delete" ? W() : (e.action(F), U());
	}, J = Object.values(w).filter(Boolean).length, Y = [...[], ...M], X = Y.length > 0;
	return /* @__PURE__ */ d("div", {
		className: "space-y-4",
		children: [
			/* @__PURE__ */ d(c, {
				searchTerm: S,
				onSearchChange: C,
				onFilterToggle: () => R(!L),
				onNew: O,
				filterCount: J,
				isFilterOpen: L,
				newButtonLabel: P,
				showFilter: !!E && Object.keys(T).length > 0,
				showSearch: !!C,
				children: [/* @__PURE__ */ d("div", {
					className: "text-sm text-gray-400",
					children: [/* @__PURE__ */ u(r, {
						checked: F.length === m.length && m.length > 0,
						onCheckedChange: V,
						"aria-label": "Select all rows",
						className: "mr-4"
					}), "Select All"]
				}), /* @__PURE__ */ d("div", { children: [k && /* @__PURE__ */ u(a, {
					selectedCount: F.length,
					batchActions: K,
					onBatchAction: q
				}), /* @__PURE__ */ u(o, {
					filters: w,
					filterConfig: T,
					onFiltersChange: E,
					isOpen: L
				})] })]
			}),
			_ && /* @__PURE__ */ u(t, {
				className: "border-destructive/50 bg-destructive/5",
				children: /* @__PURE__ */ u(n, {
					className: "p-4",
					children: /* @__PURE__ */ d("p", {
						className: "text-destructive font-medium",
						children: ["Error: ", _.message]
					})
				})
			}),
			/* @__PURE__ */ u("div", {
				className: "space-y-2",
				children: g ? /* @__PURE__ */ u("div", {
					className: "flex items-center justify-center py-8",
					children: /* @__PURE__ */ u(f, { className: "h-8 w-8 animate-spin" })
				}) : /* @__PURE__ */ u("div", { children: m.length === 0 ? /* @__PURE__ */ u("span", { children: "No items found" }) : m.map((i, a) => /* @__PURE__ */ d("div", {
					className: "flex items-center",
					children: [
						k && /* @__PURE__ */ u("div", {
							className: "flex items-center justify-center pr-4",
							onClick: (e) => e.stopPropagation(),
							children: /* @__PURE__ */ u(r, {
								checked: F.includes(i.id),
								onCheckedChange: () => H(i.id),
								"aria-label": `Select row ${i.id}`
							})
						}),
						/* @__PURE__ */ u(t, {
							className: "my-2 grow transition-shadow hover:shadow-md",
							children: /* @__PURE__ */ u(n, {
								className: "px-6",
								children: /* @__PURE__ */ u("div", {
									className: "flex items-center justify-between",
									children: /* @__PURE__ */ u("div", {
										className: `flex-1 ${D ? "cursor-pointer" : ""} ${F.includes(i.id) ? "bg-muted/50" : ""}`,
										onClick: D ? () => D(i) : void 0,
										children: /* @__PURE__ */ u("div", { children: N && N(i) })
									})
								})
							})
						}, i.id),
						X && /* @__PURE__ */ u("div", {
							className: "flex items-center justify-center",
							onClick: (e) => e.stopPropagation(),
							children: /* @__PURE__ */ u("div", {
								className: "flex items-center justify-center gap-1",
								children: Y.map((t) => !t.condition || t.condition(i) ? /* @__PURE__ */ u(e, {
									variant: t.variant || "ghost",
									size: "sm",
									onClick: () => void t.action(i),
									disabled: t.disabled?.(i),
									title: t.label,
									className: t.className,
									children: t.icon
								}, t.id) : null)
							})
						})
					]
				}, `data-list-item-${a}`)) })
			}),
			/* @__PURE__ */ u(s, {
				page: v,
				pageSize: y,
				totalItems: h,
				onPageChange: b,
				onPageSizeChange: x
			}),
			/* @__PURE__ */ u(i, {
				isOpen: z.isOpen,
				onOpenChange: (e) => B({
					...z,
					isOpen: e
				}),
				title: z.type === "single" ? "Delete Item" : "Delete Items",
				description: z.type === "single" ? "Are you sure you want to delete this item? This action cannot be undone." : `Are you sure you want to delete ${F.length} items? This action cannot be undone.`,
				onConfirm: () => void G(),
				confirmText: "Delete",
				cancelText: "Cancel",
				variant: "destructive"
			})
		]
	});
}
//#endregion
export { m as DataList };
