import { cx as e } from "../../../theme/tools.js";
import { Checkbox as t } from "../../../atomic/atoms/checkbox/Checkbox.js";
import { DataTable as n } from "../../../components/data-table/DataTable.js";
import { BackofficeToolbar as r } from "../../molecules/backoffice_toolbar/BackofficeToolbar.js";
import { BulkActionsBar as i } from "../../molecules/bulk_actions_bar/BulkActionsBar.js";
import { selectionCell as a, wrapper as o, wrapperContained as s } from "./backofficeDataTable.css.js";
import { useCallback as c, useMemo as l } from "react";
import { jsx as u, jsxs as d } from "react/jsx-runtime";
import { useTranslation as f } from "react-i18next";
//#region src/backoffice/organisms/backoffice_data_table/BackofficeDataTable.tsx
var p = (e, t) => e.mobileRole === "action" ? "3.75rem" : e.isPrimary === !0 || t === 0 ? "minmax(16rem, 1fr)" : e.align === "right" || e.align === "center" ? "minmax(5.5rem, 7rem)" : "minmax(10rem, 0.75fr)", m = (e, t) => {
	if (e.length === 0) return;
	let n = e.map((e, t) => p(e, t)).join(" ");
	return t ? `2.75rem ${n}` : n;
}, h = (e) => {
	let { rows: n, selection: r, selectAllLabel: i, selectRowLabel: o } = e, s = [];
	for (let e of n) e.selectable && s.push(e.id);
	let c = s.filter((e) => r.selectedRowIds.has(e)).length, l = s.length > 0 && c === s.length, d = c > 0 && !l, f = l;
	d && (f = "mixed");
	let p = () => {
		if (s.length !== 0) {
			if (l) {
				r.onSelectionChange(/* @__PURE__ */ new Set());
				return;
			}
			r.onSelectionChange(new Set(s));
		}
	};
	return {
		id: "__select",
		header: /* @__PURE__ */ u("div", {
			className: a,
			children: /* @__PURE__ */ u(t, {
				checked: l,
				disabled: s.length === 0,
				indeterminate: d,
				onChange: () => {
					p();
				},
				"aria-checked": f,
				"aria-label": i
			})
		}),
		cell: (e) => {
			let { id: n, selectable: i } = e, s = r.selectedRowIds.has(n), c = o(e.row, e.index);
			return /* @__PURE__ */ u("div", {
				className: a,
				children: /* @__PURE__ */ u(t, {
					checked: s,
					disabled: !i,
					onChange: () => {
						let e = new Set(r.selectedRowIds);
						e.has(n) ? e.delete(n) : i && e.add(n), r.onSelectionChange(e);
					},
					"aria-label": c
				})
			});
		},
		className: a
	};
}, g = ({ columns: t, rows: a, getRowId: p, toolbar: g, selection: _, bulkActions: v, emptyState: y, className: b, selectAllLabel: x, selectRowLabel: S, ariaLabel: C, bodyScrollMode: w = "page" }) => {
	let { t: T } = f("ui"), E = l(() => a.map((e, t) => ({
		row: e,
		index: t,
		id: p(e, t),
		selectable: _?.isRowSelectable?.(e, t) ?? !0
	})), [
		p,
		a,
		_
	]), D = l(() => _ == null ? null : h({
		rows: E,
		selection: _,
		selectAllLabel: x ?? T("backoffice.dataTable.selectAll"),
		selectRowLabel: S ?? ((e, t) => T("backoffice.dataTable.selectRow", { index: t + 1 }))
	}), [
		x,
		S,
		_,
		T,
		E
	]), O = l(() => {
		let e = t.map((e) => ({
			...e,
			cell: (t) => e.cell(t.row)
		}));
		return D == null ? e : [D, ...e];
	}, [t, D]), k = l(() => {
		if (O.some((e) => e.isPrimary === !0) || O.length === 0) return O;
		let e = 0;
		return D != null && O.length > 1 && (e = 1), O.map((t, n) => n === e ? {
			...t,
			isPrimary: !0
		} : t);
	}, [O, D]), A = l(() => m(k, D != null), [k, D]), j = 0;
	_ != null && (j = _.selectedRowIds.size);
	let M = c(() => {
		_?.onSelectionChange(/* @__PURE__ */ new Set());
	}, [_]), N = null;
	g != null && (N = /* @__PURE__ */ u(r, { ...g }));
	let P = null;
	_ != null && (P = /* @__PURE__ */ u(i, {
		selectedCount: j,
		actions: v,
		onClearSelection: M
	}));
	let F = null;
	return w === "contained" && (F = s), /* @__PURE__ */ d("div", {
		className: e(o, F, b),
		children: [
			N,
			P,
			/* @__PURE__ */ u(n, {
				ariaLabel: C ?? T("backoffice.dataTable.label"),
				columns: k,
				rows: E,
				getRowId: (e) => e.id,
				emptyState: y,
				className: void 0,
				headerClassName: void 0,
				bodyClassName: void 0,
				rowClassName: void 0,
				gridTemplateClassName: void 0,
				gridTemplateColumns: A,
				bodyScrollMode: w
			})
		]
	});
};
//#endregion
export { g as BackofficeDataTable, g as default };

//# sourceMappingURL=BackofficeDataTable.js.map