import { Button } from "../ui/button.js";
import { Card, CardContent } from "../ui/card.js";
import { Checkbox } from "../ui/checkbox.js";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "../ui/table.js";
import ConfirmDialog_default from "./ConfirmDialog.js";
import DataTableBatchAction_default from "./DataTableBatchAction.js";
import DataTableFilters_default from "./DataTableFilters.js";
import DataTablePagination_default from "./DataTablePagination.js";
import DataTableToolbar_default from "./DataTableToolbar.js";
import { c } from "react/compiler-runtime";
import { useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { ArrowDown, ArrowUp, Edit, Eye, Loader2, Trash2 } from "lucide-react";
var safeToString = (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 DataTable(Ee) {
	let r = c(102), { data: i, columns: De, totalItems: Oe, loading: ke, error: a, page: Ae, pageSize: je, onPageChange: Me, onPageSizeChange: Ne, sortBy: o, sortOrder: s, onSortChange: l, searchTerm: Pe, onSearchChange: Fe, filters: u, filterConfig: d, onFiltersChange: f, onRowClick: p, onEdit: Ie, onDelete: m, onNew: Le, newButtonLabel: Re, enableBatchActions: ze, batchActions: h, onBatchDelete: g, rowActions: Be, deleteConfirmTitle: Ve, deleteConfirmDescription: He } = Ee, Ue = ke === void 0 ? !1 : ke, _;
	r[0] === u ? _ = r[1] : (_ = u === void 0 ? {} : u, r[0] = u, r[1] = _);
	let v = _, y;
	r[2] === d ? y = r[3] : (y = d === void 0 ? {} : d, r[2] = d, r[3] = y);
	let b = y, x = ze === void 0 ? !0 : ze, S;
	r[4] === h ? S = r[5] : (S = h === void 0 ? [] : h, r[4] = h, r[5] = S);
	let We = S, Ge = Be === void 0 ? [] : Be, C;
	r[6] === Symbol.for("react.memo_cache_sentinel") ? (C = [], r[6] = C) : C = r[6];
	let [w, T] = useState(C), [E, Ke] = useState(!1), D;
	r[7] === Symbol.for("react.memo_cache_sentinel") ? (D = {
		isOpen: !1,
		type: "single"
	}, r[7] = D) : D = r[7];
	let [O, k] = useState(D), qe;
	r[8] !== i || r[9] !== w.length ? (qe = () => {
		w.length === i.length ? T([]) : T(i.map(_temp));
	}, r[8] = i, r[9] = w.length, r[10] = qe) : qe = r[10];
	let Je = qe, A;
	r[11] === Symbol.for("react.memo_cache_sentinel") ? (A = (e) => {
		T((t) => t.includes(e) ? t.filter((t) => t !== e) : [...t, e]);
	}, r[11] = A) : A = r[11];
	let Ye = A, j;
	r[12] === Symbol.for("react.memo_cache_sentinel") ? (j = () => T([]), r[12] = j) : j = r[12];
	let Xe = j, M;
	r[13] !== l || r[14] !== o || r[15] !== s ? (M = (e) => {
		o === e ? l(e, s === "ASC" ? "DESC" : "ASC") : l(e, "ASC");
	}, r[13] = l, r[14] = o, r[15] = s, r[16] = M) : M = r[16];
	let Ze = M, N;
	r[17] === k ? N = r[18] : (N = (e) => {
		k({
			isOpen: !0,
			type: "single",
			itemId: e
		});
	}, r[17] = k, r[18] = N);
	let Qe = N, P;
	r[19] === k ? P = r[20] : (P = () => {
		k({
			isOpen: !0,
			type: "batch"
		});
	}, r[19] = k, r[20] = P);
	let F = P, I;
	r[21] !== O.itemId || r[22] !== O.type || r[23] !== g || r[24] !== m || r[25] !== w || r[26] !== k ? (I = async () => {
		O.type === "single" && O.itemId ? m?.(O.itemId) : O.type === "batch" && g && (await Promise.resolve(g(w)), Xe()), k({
			isOpen: !1,
			type: "single"
		});
	}, r[21] = O.itemId, r[22] = O.type, r[23] = g, r[24] = m, r[25] = w, r[26] = k, r[27] = I) : I = r[27];
	let $e = I, L;
	r[28] !== F || r[29] !== g ? (L = g ? [{
		id: "delete",
		label: "Delete Selected",
		icon: /* @__PURE__ */ jsx(Trash2, { className: "mr-2 h-4 w-4" }),
		variant: "destructive",
		action: F
	}] : [], r[28] = F, r[29] = g, r[30] = L) : L = r[30];
	let R;
	r[31] !== We || r[32] !== L ? (R = [...L, ...We], r[31] = We, r[32] = L, r[33] = R) : R = r[33];
	let et = R, z;
	r[34] !== F || r[35] !== w ? (z = (e) => {
		e.id === "delete" ? F() : (e.action(w), Xe());
	}, r[34] = F, r[35] = w, r[36] = z) : z = r[36];
	let tt = z, B;
	r[37] !== o || r[38] !== s ? (B = (e) => o === e ? jsx(s === "ASC" ? ArrowUp : ArrowDown, { className: "h-4 w-4" }) : null, r[37] = o, r[38] = s, r[39] = B) : B = r[39];
	let nt = B, V;
	r[40] === v ? V = r[41] : (V = Object.values(v).filter(Boolean), r[40] = v, r[41] = V);
	let rt = V.length, it = [...[
		...p ? [{
			id: "view",
			label: "View",
			icon: /* @__PURE__ */ jsx(Eye, { className: "h-4 w-4" }),
			variant: "ghost",
			action: p
		}] : [],
		...Ie ? [{
			id: "edit",
			label: "Edit",
			icon: /* @__PURE__ */ jsx(Edit, { className: "h-4 w-4" }),
			variant: "ghost",
			action: Ie
		}] : [],
		...m ? [{
			id: "delete",
			label: "Delete",
			icon: /* @__PURE__ */ jsx(Trash2, { className: "h-4 w-4" }),
			variant: "ghost",
			action: (e) => Qe(e.id)
		}] : []
	], ...Ge], at = it.length > 0, H;
	r[42] === E ? H = r[43] : (H = () => Ke(!E), r[42] = E, r[43] = H);
	let ot = !!f && Object.keys(b).length > 0, st = !!Fe, U;
	r[44] !== rt || r[45] !== E || r[46] !== Re || r[47] !== Le || r[48] !== Fe || r[49] !== Pe || r[50] !== H || r[51] !== ot || r[52] !== st ? (U = /* @__PURE__ */ jsx(DataTableToolbar_default, {
		searchTerm: Pe,
		onSearchChange: Fe,
		onFilterToggle: H,
		onNew: Le,
		newButtonLabel: Re,
		filterCount: rt,
		isFilterOpen: E,
		showFilter: ot,
		showSearch: st
	}), r[44] = rt, r[45] = E, r[46] = Re, r[47] = Le, r[48] = Fe, r[49] = Pe, r[50] = H, r[51] = ot, r[52] = st, r[53] = U) : U = r[53];
	let W;
	r[54] !== b || r[55] !== v || r[56] !== E || r[57] !== f ? (W = /* @__PURE__ */ jsx(DataTableFilters_default, {
		filters: v,
		filterConfig: b,
		onFiltersChange: f,
		isOpen: E
	}), r[54] = b, r[55] = v, r[56] = E, r[57] = f, r[58] = W) : W = r[58];
	let G;
	r[59] !== et || r[60] !== x || r[61] !== tt || r[62] !== w.length ? (G = x && /* @__PURE__ */ jsx(DataTableBatchAction_default, {
		selectedCount: w.length,
		batchActions: et,
		onBatchAction: tt
	}), r[59] = et, r[60] = x, r[61] = tt, r[62] = w.length, r[63] = G) : G = r[63];
	let K;
	r[64] === a ? K = r[65] : (K = a && /* @__PURE__ */ jsx(Card, {
		className: "border-destructive/50 bg-destructive/5",
		children: /* @__PURE__ */ jsx(CardContent, {
			className: "p-4",
			children: /* @__PURE__ */ jsxs("p", {
				className: "text-destructive font-medium",
				children: ["Error: ", a.message]
			})
		})
	}), r[64] = a, r[65] = K);
	let ct = Ue ? /* @__PURE__ */ jsx("div", {
		className: "flex items-center justify-center py-8",
		children: /* @__PURE__ */ jsx(Loader2, { className: "h-8 w-8 animate-spin" })
	}) : /* @__PURE__ */ jsxs(Table, { children: [/* @__PURE__ */ jsx(TableHeader, { children: /* @__PURE__ */ jsxs(TableRow, { children: [
		x && /* @__PURE__ */ jsx(TableHead, {
			className: "w-12",
			children: /* @__PURE__ */ jsx(Checkbox, {
				checked: w.length === i.length && i.length > 0,
				onCheckedChange: Je,
				"aria-label": "Select all rows"
			})
		}),
		De.map((t) => /* @__PURE__ */ jsx(TableHead, {
			className: `${t.align === "center" ? "text-center" : t.align === "right" ? "text-right" : ""}`,
			style: { minWidth: t.minWidth },
			children: t.sortable === !1 ? /* @__PURE__ */ jsx("span", {
				className: "font-semibold",
				children: t.label
			}) : /* @__PURE__ */ jsxs(Button, {
				variant: "ghost",
				onClick: () => Ze(t.id),
				className: "h-auto p-0 font-semibold hover:bg-transparent",
				children: [t.label, nt(t.id)]
			})
		}, t.id)),
		at && /* @__PURE__ */ jsx(TableHead, {
			className: "text-center",
			children: "Actions"
		})
	] }) }), /* @__PURE__ */ jsx(TableBody, { children: i.length === 0 ? /* @__PURE__ */ jsx(TableRow, { children: /* @__PURE__ */ jsx(TableCell, {
		colSpan: De.length + (x ? 1 : 0) + (at ? 1 : 0),
		className: "text-muted-foreground py-8 text-center",
		children: "No items found"
	}) }) : i.map((t) => /* @__PURE__ */ jsxs(TableRow, {
		className: `${p ? "cursor-pointer" : ""} ${w.includes(t.id) ? "bg-muted/50" : ""}`,
		onClick: p ? () => p(t) : void 0,
		children: [
			x && /* @__PURE__ */ jsx(TableCell, {
				onClick: _temp2,
				children: /* @__PURE__ */ jsx(Checkbox, {
					checked: w.includes(t.id),
					onCheckedChange: () => Ye(t.id),
					"aria-label": `Select row ${t.id}`
				})
			}),
			De.map((e) => /* @__PURE__ */ jsx(TableCell, {
				className: e.align === "center" ? "text-center" : e.align === "right" ? "text-right" : "",
				children: e.format ? e.format(t[e.id], t) : safeToString(t[e.id])
			}, e.id)),
			at && /* @__PURE__ */ jsx(TableCell, {
				className: "text-center",
				onClick: _temp3,
				children: /* @__PURE__ */ jsx("div", {
					className: "flex items-center justify-center gap-1",
					children: it.map((n) => !n.condition || n.condition(t) ? /* @__PURE__ */ jsx(Button, {
						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)) })] }), q;
	r[66] === ct ? q = r[67] : (q = /* @__PURE__ */ jsx("div", {
		className: "w-full",
		children: ct
	}), r[66] = ct, r[67] = q);
	let J;
	r[68] !== Me || r[69] !== Ne || r[70] !== Ae || r[71] !== je || r[72] !== Oe ? (J = /* @__PURE__ */ jsx(DataTablePagination_default, {
		page: Ae,
		pageSize: je,
		totalItems: Oe,
		onPageChange: Me,
		onPageSizeChange: Ne
	}), r[68] = Me, r[69] = Ne, r[70] = Ae, r[71] = je, r[72] = Oe, r[73] = J) : J = r[73];
	let Y;
	r[74] !== O || r[75] !== k ? (Y = (e) => k({
		...O,
		isOpen: e
	}), r[74] = O, r[75] = k, r[76] = Y) : Y = r[76];
	let X;
	r[77] !== Ve || r[78] !== O.itemId || r[79] !== O.type ? (X = O.type === "single" ? O.itemId && Ve?.(O.itemId) || "Delete Item" : "Delete Items", r[77] = Ve, r[78] = O.itemId, r[79] = O.type, r[80] = X) : X = r[80];
	let Z;
	r[81] !== He || r[82] !== O.itemId || r[83] !== O.type || r[84] !== w.length ? (Z = O.type === "single" ? O.itemId && He?.(O.itemId) || "Are you sure you want to delete this item? This action cannot be undone." : `Are you sure you want to delete ${w.length} items? This action cannot be undone.`, r[81] = He, r[82] = O.itemId, r[83] = O.type, r[84] = w.length, r[85] = Z) : Z = r[85];
	let Q;
	r[86] === $e ? Q = r[87] : (Q = () => void $e(), r[86] = $e, r[87] = Q);
	let $;
	r[88] !== O.isOpen || r[89] !== Y || r[90] !== X || r[91] !== Z || r[92] !== Q ? ($ = /* @__PURE__ */ jsx(ConfirmDialog_default, {
		isOpen: O.isOpen,
		onOpenChange: Y,
		title: X,
		description: Z,
		onConfirm: Q,
		confirmText: "Delete",
		cancelText: "Cancel",
		variant: "destructive"
	}), r[88] = O.isOpen, r[89] = Y, r[90] = X, r[91] = Z, r[92] = Q, r[93] = $) : $ = r[93];
	let lt;
	return r[94] !== U || r[95] !== W || r[96] !== G || r[97] !== K || r[98] !== q || r[99] !== J || r[100] !== $ ? (lt = /* @__PURE__ */ jsxs("div", {
		className: "space-y-4",
		children: [
			U,
			W,
			G,
			K,
			q,
			J,
			$
		]
	}), r[94] = U, r[95] = W, r[96] = G, r[97] = K, r[98] = q, r[99] = J, r[100] = $, r[101] = lt) : lt = r[101], lt;
}
function _temp3(e) {
	return e.stopPropagation();
}
function _temp2(e) {
	return e.stopPropagation();
}
function _temp(e) {
	return e.id;
}
export { DataTable };
