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 { BulkActionsBar as r } from "../../molecules/bulk_actions_bar/BulkActionsBar.js";
import { TableToolbar as i } from "../../molecules/table_toolbar/TableToolbar.js";
import { selectionCell as a, wrapper as o } from "./backofficeDataTable.css.js";
import { useCallback as s, useMemo as c } from "react";
import { jsx as l, jsxs as u } from "react/jsx-runtime";
import { useTranslation as d } from "react-i18next";
//#region src/backoffice/organisms/backoffice_data_table/BackofficeDataTable.tsx
var f = (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)", p = (e, t) => {
	if (e.length === 0) return;
	let n = e.map((e, t) => f(e, t)).join(" ");
	return t ? `2.75rem ${n}` : n;
}, m = (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, u = s.length > 0 && c === s.length, d = c > 0 && !u, f = u;
	d && (f = "mixed");
	let p = () => {
		if (s.length !== 0) {
			if (u) {
				r.onSelectionChange(/* @__PURE__ */ new Set());
				return;
			}
			r.onSelectionChange(new Set(s));
		}
	};
	return {
		id: "__select",
		header: /* @__PURE__ */ l("div", {
			className: a,
			children: /* @__PURE__ */ l(t, {
				checked: u,
				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__ */ l("div", {
				className: a,
				children: /* @__PURE__ */ l(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
	};
}, h = ({ columns: t, rows: a, getRowId: f, toolbar: h, selection: g, bulkActions: _, emptyState: v, className: y, selectAllLabel: b, selectRowLabel: x, ariaLabel: S }) => {
	let { t: C } = d("ui"), w = c(() => a.map((e, t) => ({
		row: e,
		index: t,
		id: f(e, t),
		selectable: g?.isRowSelectable?.(e, t) ?? !0
	})), [
		f,
		a,
		g
	]), T = c(() => g == null ? null : m({
		rows: w,
		selection: g,
		selectAllLabel: b ?? C("backoffice.dataTable.selectAll"),
		selectRowLabel: x ?? ((e, t) => C("backoffice.dataTable.selectRow", { index: t + 1 }))
	}), [
		b,
		x,
		g,
		C,
		w
	]), E = c(() => {
		let e = t.map((e) => ({
			...e,
			cell: (t) => e.cell(t.row)
		}));
		return T == null ? e : [T, ...e];
	}, [t, T]), D = c(() => {
		if (E.some((e) => e.isPrimary === !0) || E.length === 0) return E;
		let e = 0;
		return T != null && E.length > 1 && (e = 1), E.map((t, n) => n === e ? {
			...t,
			isPrimary: !0
		} : t);
	}, [E, T]), O = c(() => p(D, T != null), [D, T]), k = 0;
	g != null && (k = g.selectedRowIds.size);
	let A = s(() => {
		g?.onSelectionChange(/* @__PURE__ */ new Set());
	}, [g]), j = null;
	h != null && (j = /* @__PURE__ */ l(i, { ...h }));
	let M = null;
	return g != null && (M = /* @__PURE__ */ l(r, {
		selectedCount: k,
		actions: _,
		onClearSelection: A
	})), /* @__PURE__ */ u("div", {
		className: e(o, y),
		children: [
			j,
			M,
			/* @__PURE__ */ l(n, {
				ariaLabel: S ?? C("backoffice.dataTable.label"),
				columns: D,
				rows: w,
				getRowId: (e) => e.id,
				emptyState: v,
				className: void 0,
				headerClassName: void 0,
				bodyClassName: void 0,
				rowClassName: void 0,
				gridTemplateClassName: void 0,
				gridTemplateColumns: O
			})
		]
	});
};
//#endregion
export { h as BackofficeDataTable, h as default };

//# sourceMappingURL=BackofficeDataTable.js.map