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 { Table as i, TableBody as ee, TableCell as a, TableHead as o, TableHeader as te, TableRow as s } from "../ui/table.js";
import ne from "./ConfirmDialog.js";
import c from "./DataTableBatchAction.js";
import l from "./DataTableFilters.js";
import u from "./DataTablePagination.js";
import d from "./DataTableToolbar.js";
import { useState as f } from "react";
import { jsx as p, jsxs as m } from "react/jsx-runtime";
import { ArrowDown as re, ArrowUp as ie, Edit as ae, Eye as h, Loader2 as g, Trash2 as _ } from "lucide-react";
//#region src/components/data-table/DataTable.tsx
var v = (e) => e == null ? "" : typeof e == "string" ? e : typeof e == "number" || typeof e == "boolean" ? String(e) : typeof e == "object" ? JSON.stringify(e) : JSON.stringify(e) ?? "";
function y({ data: y, columns: b, totalItems: oe, loading: x = !1, error: S, page: C, pageSize: w, onPageChange: T, onPageSizeChange: E, sortBy: D, sortOrder: O, onSortChange: k, searchTerm: A, onSearchChange: j, filters: M = {}, filterConfig: N = {}, onFiltersChange: P, onRowClick: F, onEdit: I, onDelete: L, onNew: R, newButtonLabel: z, enableBatchActions: B = !0, batchActions: V = [], onBatchDelete: H, rowActions: U = [], deleteConfirmTitle: W, deleteConfirmDescription: se }) {
	let [G, K] = f([]), [q, ce] = f(!1), [J, Y] = f({
		isOpen: !1,
		type: "single"
	}), le = () => {
		G.length === y.length ? K([]) : K(y.map((e) => e.id));
	}, ue = (e) => {
		K((t) => t.includes(e) ? t.filter((t) => t !== e) : [...t, e]);
	}, X = () => K([]), de = (e) => {
		D === e ? k(e, O === "ASC" ? "DESC" : "ASC") : k(e, "ASC");
	}, fe = (e) => {
		Y({
			isOpen: !0,
			type: "single",
			itemId: e
		});
	}, Z = () => {
		Y({
			isOpen: !0,
			type: "batch"
		});
	}, pe = async () => {
		J.type === "single" && J.itemId ? L?.(J.itemId) : J.type === "batch" && H && (await Promise.resolve(H(G)), X()), Y({
			isOpen: !1,
			type: "single"
		});
	}, me = [...H ? [{
		id: "delete",
		label: "Delete Selected",
		icon: /* @__PURE__ */ p(_, { className: "mr-2 h-4 w-4" }),
		variant: "destructive",
		action: Z
	}] : [], ...V], he = (e) => {
		e.id === "delete" ? Z() : (e.action(G), X());
	}, ge = (e) => D === e ? p(O === "ASC" ? ie : re, { className: "h-4 w-4" }) : null, _e = Object.values(M).filter(Boolean).length, Q = [...[
		...F ? [{
			id: "view",
			label: "View",
			icon: /* @__PURE__ */ p(h, { className: "h-4 w-4" }),
			variant: "ghost",
			action: F
		}] : [],
		...I ? [{
			id: "edit",
			label: "Edit",
			icon: /* @__PURE__ */ p(ae, { className: "h-4 w-4" }),
			variant: "ghost",
			action: I
		}] : [],
		...L ? [{
			id: "delete",
			label: "Delete",
			icon: /* @__PURE__ */ p(_, { className: "h-4 w-4" }),
			variant: "ghost",
			action: (e) => fe(e.id)
		}] : []
	], ...U], $ = Q.length > 0;
	return /* @__PURE__ */ m("div", {
		className: "space-y-4",
		children: [
			/* @__PURE__ */ p(d, {
				searchTerm: A,
				onSearchChange: j,
				onFilterToggle: () => ce(!q),
				onNew: R,
				newButtonLabel: z,
				filterCount: _e,
				isFilterOpen: q,
				showFilter: !!P && Object.keys(N).length > 0,
				showSearch: !!j
			}),
			/* @__PURE__ */ p(l, {
				filters: M,
				filterConfig: N,
				onFiltersChange: P,
				isOpen: q
			}),
			B && /* @__PURE__ */ p(c, {
				selectedCount: G.length,
				batchActions: me,
				onBatchAction: he
			}),
			S && /* @__PURE__ */ p(t, {
				className: "border-destructive/50 bg-destructive/5",
				children: /* @__PURE__ */ p(n, {
					className: "p-4",
					children: /* @__PURE__ */ m("p", {
						className: "text-destructive font-medium",
						children: ["Error: ", S.message]
					})
				})
			}),
			/* @__PURE__ */ p("div", {
				className: "w-full",
				children: x ? /* @__PURE__ */ p("div", {
					className: "flex items-center justify-center py-8",
					children: /* @__PURE__ */ p(g, { className: "h-8 w-8 animate-spin" })
				}) : /* @__PURE__ */ m(i, { children: [/* @__PURE__ */ p(te, { children: /* @__PURE__ */ m(s, { children: [
					B && /* @__PURE__ */ p(o, {
						className: "w-12",
						children: /* @__PURE__ */ p(r, {
							checked: G.length === y.length && y.length > 0,
							onCheckedChange: le,
							"aria-label": "Select all rows"
						})
					}),
					b.map((t) => /* @__PURE__ */ p(o, {
						className: `${t.align === "center" ? "text-center" : t.align === "right" ? "text-right" : ""}`,
						style: { minWidth: t.minWidth },
						children: t.sortable === !1 ? /* @__PURE__ */ p("span", {
							className: "font-semibold",
							children: t.label
						}) : /* @__PURE__ */ m(e, {
							variant: "ghost",
							onClick: () => de(t.id),
							className: "h-auto p-0 font-semibold hover:bg-transparent",
							children: [t.label, ge(t.id)]
						})
					}, t.id)),
					$ && /* @__PURE__ */ p(o, {
						className: "text-center",
						children: "Actions"
					})
				] }) }), /* @__PURE__ */ p(ee, { children: y.length === 0 ? /* @__PURE__ */ p(s, { children: /* @__PURE__ */ p(a, {
					colSpan: b.length + +!!B + +!!$,
					className: "text-muted-foreground py-8 text-center",
					children: "No items found"
				}) }) : y.map((t) => /* @__PURE__ */ m(s, {
					className: `${F ? "cursor-pointer" : ""} ${G.includes(t.id) ? "bg-muted/50" : ""}`,
					onClick: F ? () => F(t) : void 0,
					children: [
						B && /* @__PURE__ */ p(a, {
							onClick: (e) => e.stopPropagation(),
							children: /* @__PURE__ */ p(r, {
								checked: G.includes(t.id),
								onCheckedChange: () => ue(t.id),
								"aria-label": `Select row ${t.id}`
							})
						}),
						b.map((e) => /* @__PURE__ */ p(a, {
							className: e.align === "center" ? "text-center" : e.align === "right" ? "text-right" : "",
							children: e.format ? e.format(t[e.id], t) : v(t[e.id])
						}, e.id)),
						$ && /* @__PURE__ */ p(a, {
							className: "text-center",
							onClick: (e) => e.stopPropagation(),
							children: /* @__PURE__ */ p("div", {
								className: "flex items-center justify-center gap-1",
								children: Q.map((n) => !n.condition || n.condition(t) ? /* @__PURE__ */ p(e, {
									variant: n.variant || "ghost",
									size: "sm",
									onClick: () => void n.action(t),
									disabled: n.disabled?.(t),
									title: n.label,
									className: n.className,
									children: n.icon
								}, n.id) : null)
							})
						})
					]
				}, t.id)) })] })
			}),
			/* @__PURE__ */ p(u, {
				page: C,
				pageSize: w,
				totalItems: oe,
				onPageChange: T,
				onPageSizeChange: E
			}),
			/* @__PURE__ */ p(ne, {
				isOpen: J.isOpen,
				onOpenChange: (e) => Y({
					...J,
					isOpen: e
				}),
				title: J.type === "single" ? J.itemId && W?.(J.itemId) || "Delete Item" : "Delete Items",
				description: J.type === "single" ? J.itemId && se?.(J.itemId) || "Are you sure you want to delete this item? This action cannot be undone." : `Are you sure you want to delete ${G.length} items? This action cannot be undone.`,
				onConfirm: () => void pe(),
				confirmText: "Delete",
				cancelText: "Cancel",
				variant: "destructive"
			})
		]
	});
}
//#endregion
export { y as DataTable };
