import { c as e, d as t, h as n, i as r } from "./misc-aGQBhywJ.js";
import { r as i } from "./grid-dI94AAbi.js";
import { t as a } from "./table-column-interactions-DzEvVuXe.js";
import { n as o } from "./context-menu-6nJkPmKV.js";
import { s, t as c } from "./sortable-BKIwlqhI.js";
import { useCallback as l, useEffect as u, useMemo as d, useRef as f, useState as p } from "react";
import { cn as m, handleKeyboardActivation as ee } from "@ryuzaki13/react-foundation-lib/utils";
import { Fragment as te, jsx as h, jsxs as g } from "react/jsx-runtime";
import { useDndSortableSensors as ne } from "@ryuzaki13/react-foundation-lib/hooks";
import { compileFormattersPipelineRuntimeFields as _, resolveValueStateClassName as v } from "@ryuzaki13/react-foundation-lib/formatters";
import { buildTableColumnLayout as y, getTableColumnFormattingMeta as b, getTableColumnMeta as x, isTableInteractiveElement as S, normalizeTableColumnOrder as C, normalizeTableColumnSizing as w, patchTableColumnWidth as re, removeTableColumnWidth as ie, resolveReorderedTableHeaderColumns as ae, resolveTableColumnOrder as T, resolveTableLength as E } from "@ryuzaki13/react-foundation-lib/table";
import { flexRender as oe, functionalUpdate as D } from "@tanstack/react-table";
import { filterAndDeduplicateIds as O, pickExistingMapValues as k } from "@ryuzaki13/react-foundation-lib/array";
import { restrictToHorizontalAxis as se } from "@dnd-kit/modifiers";
//#region src/base-table/lib/columnPinning.ts
function A(e) {
	return {
		left: Array.from(new Set((e?.left ?? []).filter((e) => typeof e == "string" && e.trim().length > 0))),
		right: []
	};
}
function j(e, t) {
	let n = t?.[e.id];
	if (typeof n == "number" && Number.isFinite(n) && n > 0) return `${Math.floor(n)}px`;
	let r = x(e.columnDef)?.width ?? 4;
	return typeof r == "number" && (r = Math.max(r, 4)), E(r);
}
function M(e, t) {
	let n = e.getAllLeafColumns().filter((e) => e.getIsVisible()), r = new Map(n.map((e) => [e.id, e])), i = n.map((e) => e.id), a = O(t.columnPinning.left, i), o = O(t.columnPinning.right, i), s = /* @__PURE__ */ new Set([...a, ...o]), c = [];
	for (let e of n) s.has(e.id) || c.push(e.id);
	let l = T({
		ids: c,
		order: t.columnOrder
	});
	return k([
		...a,
		...l,
		...o
	], r);
}
function N(e, t) {
	let n = M(e, t), r = new Map(n.map((e) => [e.id, e]));
	return y({
		ids: n.map((e) => e.id),
		getWidth: (e) => {
			let n = r.get(e);
			return n ? j(n, t.columnSizing) : "0px";
		}
	}).flatMap((e) => {
		let t = r.get(e.id);
		return t ? [{
			...e,
			column: t
		}] : [];
	});
}
function P(e, t) {
	let n = {}, r = [];
	for (let i of M(e, t)) i.getIsPinned() === "left" && (n[i.id] = F(r), r.push(j(i, t.columnSizing)));
	return n;
}
function F(e) {
	return e.length === 0 ? "0px" : e.length === 1 ? e[0] : `calc(${e.join(" + ")})`;
}
//#endregion
//#region src/base-table/lib/formatting.ts
function I(e) {
	if (typeof e.id == "string" && e.id.trim()) return e.id;
	if ("accessorKey" in e && typeof e.accessorKey == "string" && e.accessorKey.trim()) return e.accessorKey;
}
function L(e) {
	return "cell" in e && e.cell != null;
}
function R(e, t) {
	for (let n of e) {
		if ("columns" in n && Array.isArray(n.columns) && n.columns.length > 0) {
			R(n.columns, t);
			continue;
		}
		t(n);
	}
}
function z(e) {
	let t = {}, n = /* @__PURE__ */ new Set();
	return R(e, (e) => {
		let r = I(e);
		if (!r) return;
		L(e) && n.add(r);
		let i = b(e);
		i && (t[r] = {
			...i,
			id: i.id ?? r,
			role: i.role ?? "dimension",
			type: i.type ?? "string"
		});
	}), {
		runtimeByColumnId: _(t),
		customCellColumnIds: n
	};
}
function B(e) {
	return !e.customCellColumnIds.has(e.columnId) && !!e.runtimeByColumnId[e.columnId];
}
//#endregion
//#region src/base-table/lib/mergeDuplicates.ts
function V(e) {
	return e != null && e !== "";
}
function H(e, t) {
	return e.map((e) => ({
		row: e,
		boundaryKey: t(e),
		cellByColumnId: new Map(e.getVisibleCells().map((e) => [e.column.id, e]))
	}));
}
function U({ rows: e, columns: t, resolveRowBoundaryKey: n = () => "__all_rows__", excludeColumnIds: r }) {
	let i = t.filter((e) => !r?.has(e.id) && x(e.columnDef)?.mergeDuplicates === !0);
	if (e.length === 0 || i.length === 0) return /* @__PURE__ */ new Map();
	let a = H(e, n), o = /* @__PURE__ */ new Map();
	for (let e of i) {
		let t = -1, n, r, i = (i) => {
			if (t !== -1) {
				if (i - t <= 1) {
					t = -1, n = void 0, r = void 0;
					return;
				}
				for (let n = t; n < i; n += 1) {
					let r = a[n]?.cellByColumnId.get(e.id)?.id;
					r && o.set(r, {
						hideContent: n > t,
						mergeWithNext: n < i - 1
					});
				}
				t = -1, n = void 0, r = void 0;
			}
		};
		for (let o = 0; o < a.length; o += 1) {
			let s = a[o], c = s?.cellByColumnId.get(e.id), l = c?.getValue();
			if (!c || !V(l)) {
				i(o);
				continue;
			}
			if (t === -1) {
				t = o, n = l, r = s?.boundaryKey;
				continue;
			}
			(!Object.is(r, s?.boundaryKey) || !Object.is(n, l)) && (i(o), t = o, n = l, r = s?.boundaryKey);
		}
		i(a.length);
	}
	return o;
}
//#endregion
//#region src/base-table/model/useTableColumnOrder.ts
function W({ columnOrder: e, defaultColumnOrder: t, onColumnOrderChange: n }) {
	let [r, i] = p(void 0), a = d(() => C(e ?? r ?? t), [
		e,
		t,
		r
	]);
	return {
		columnOrder: a,
		onColumnOrderChange: l((t) => {
			let r = C(D(t, a));
			e === void 0 && i(r), n?.(r);
		}, [
			e,
			n,
			a
		])
	};
}
//#endregion
//#region src/base-table/model/useTableColumnPinning.ts
function G({ columnPinning: e, defaultColumnPinning: t, onColumnPinningChange: n }) {
	let [r, i] = p(void 0), a = d(() => A(e ?? r ?? t), [
		e,
		t,
		r
	]);
	return {
		columnPinning: a,
		onColumnPinningChange: l((t) => {
			let r = { left: A(D(t, a)).left };
			e === void 0 && i(r), n?.(r);
		}, [
			e,
			n,
			a
		])
	};
}
//#endregion
//#region src/base-table/model/useTableColumnSizing.ts
function K({ columnSizing: e, defaultColumnSizing: t, columnResizeMinWidth: n, onColumnSizingChange: r }) {
	let [i, a] = p(void 0), o = d(() => w(e ?? i ?? t, n), [
		n,
		e,
		t,
		i
	]);
	return {
		columnSizing: o,
		onColumnSizingChange: l((t) => {
			let i = w(D(t, o), n);
			e === void 0 && a(i), r?.(i);
		}, [
			n,
			e,
			r,
			o
		])
	};
}
//#endregion
//#region src/base-table/model/useTableColumnVisibility.ts
function q({ columnVisibility: e, defaultColumnVisibility: t, onColumnVisibilityChange: n }) {
	let [r, i] = p(void 0), a = d(() => e ?? r ?? t ?? {}, [
		e,
		t,
		r
	]);
	return {
		columnVisibility: a,
		onColumnVisibilityChange: l((t) => {
			let r = D(t, a);
			e === void 0 && i(r), n?.(r);
		}, [
			e,
			n,
			a
		])
	};
}
var J = {
	root: "_0rNBMr",
	headerContextMenuTrigger: "gp-7Db",
	table: "Ck-sXP",
	head: "-F3gli",
	pinnedLeftBoundary: "buIdkU",
	headerRow: "_2VJbvj",
	headerCell: "AD8NkG",
	headerCellContent: "qqixKn",
	headerCellDraggable: "dVDRNv",
	headerCellDragging: "klOoMz",
	headerCellPinnedLeft: "CpjV-N",
	bodyRow: "_7D8xa9",
	bodyRowInteractive: "_3j87pS",
	bodyCell: "uwojkJ",
	bodyRowSelected: "lLsmW6",
	bodyCellPinnedLeft: "xKvBFg",
	bodyCellMergedWithNext: "QIS3xA",
	feedbackCell: "a580w0",
	fetchingRow: "UOhaMF",
	noData: "DtKAgs"
};
//#endregion
//#region src/base-table/ui/BaseTableHeaderCell.tsx
function Y({ header: e, isPinnedLeft: t, left: n, isPinnedBoundary: r, isDragDisabled: i, enableColumnResizing: o, enableColumnReordering: s, columnResizeMinWidth: c, renderHeaderContent: l, onResizeColumn: u, onResetColumnWidth: d }) {
	return /* @__PURE__ */ h(a, {
		columnId: e.column.id,
		dragDisabled: !s || i,
		draggingClassName: J.headerCellDragging,
		className: m(J.headerCell, t && J.headerCellPinnedLeft, r && J.pinnedLeftBoundary),
		style: { ...t ? { left: n } : {} },
		resizeHandle: o && !e.isPlaceholder ? {
			columnId: e.column.id,
			minWidth: c,
			onResize: u,
			onReset: d
		} : void 0,
		children: ({ listeners: t, setActivatorElement: n }) => /* @__PURE__ */ h("div", {
			ref: s && !i ? n : void 0,
			className: m(J.headerCellContent, s && !i && J.headerCellDraggable),
			...s && !i ? t ?? {} : {},
			children: l(e)
		})
	});
}
//#endregion
//#region src/base-table/ui/BaseTable.tsx
function X(e, t) {
	let n = e.getLeafHeaders().map((e) => t.get(e.column.id)).filter((e) => typeof e == "number");
	return n.length ? Math.min(...n) : 2 ** 53 - 1;
}
function ce(e, t) {
	let n = e.getLeftHeaderGroups(), r = e.getCenterHeaderGroups(), i = e.getRightHeaderGroups(), a = Math.max(n.length, r.length, i.length);
	return Array.from({ length: a }, (e, a) => [
		...n[a]?.headers ?? [],
		...r[a]?.headers ?? [],
		...i[a]?.headers ?? []
	].filter((e) => e.isPlaceholder ? !0 : e.getLeafHeaders().some((e) => t.has(e.column.id))).sort((e, n) => X(e, t) - X(n, t)));
}
function le(e, t, n) {
	let r = e.getLeafHeaders().map((e) => e.column.id);
	return r.length > 0 && r.every((e) => e in t) ? {
		isPinnedLeft: !0,
		left: t[r[0]] ?? "0px",
		isPinnedBoundary: !!(n && r.includes(n))
	} : {
		isPinnedLeft: !1,
		isPinnedBoundary: !1
	};
}
function ue(e, t) {
	let n = e.getAllLeafColumns().map((e) => e.id);
	return T({
		ids: n,
		order: t.columnOrder
	});
}
function de({ table: r, columnState: a, title: u, isLoading: f, isFetching: p, className: _, minHeight: v = "15em", tableClassName: y, selectionMode: b = "none", scrollableRef: C, interactiveElementSelector: w, onActivateRow: T, isRowInteractive: E, getRowClassName: D, getCellClassName: O, getCellStyle: k, getCellLayout: A, renderCellContent: j, renderHeaderContextMenu: M, enableColumnResizing: F = !1, enableColumnReordering: I = !1, columnResizeMinWidth: L, afterTableContent: R }) {
	let z = N(r, a), B = d(() => z.map((e) => e.column), [z]), V = d(() => z.map((e) => e.id), [z]), H = d(() => new Map(V.map((e, t) => [e, t])), [V]), U = d(() => new Set(V), [V]), W = ne({ activationDistance: 8 }), G = ce(r, H), K = P(r, a), q = B.filter((e) => e.getIsPinned() === "left").at(-1)?.id, X = d(() => a.columnPinning.left ?? [], [a.columnPinning.left]), de = d(() => new Set(X), [X]), Z = r.getRowModel(), Q = !!u, $ = Z.rows.length > 0, fe = l((e) => [...e].sort((e, t) => (H.get(e.column.id) ?? 2 ** 53 - 1) - (H.get(t.column.id) ?? 2 ** 53 - 1)), [H]), pe = l((e, t) => {
		r.setColumnSizing((n) => re(n, e, t, L));
	}, [L, r]), me = l((e) => {
		r.setColumnSizing((t) => ie(t, e));
	}, [r]), he = l((e) => {
		let t = s({
			event: e,
			items: V
		});
		if (!t) return;
		let n = ue(r, a), i = ae({
			order: n,
			headerIds: V,
			pinnedIds: X,
			activeId: String(t.activeId),
			overId: String(t.overId)
		});
		i && r.setColumnOrder(i);
	}, [
		a,
		X,
		r,
		V
	]), ge = (e) => {
		if (e.isPlaceholder) return null;
		let t = oe(e.column.columnDef.header, e.getContext()), n = M?.(e);
		return n ? /* @__PURE__ */ g(o, { children: [/* @__PURE__ */ h(o.Trigger, { children: /* @__PURE__ */ h("div", {
			className: J.headerContextMenuTrigger,
			tabIndex: 0,
			children: t
		}) }), /* @__PURE__ */ h(o.Content, { children: n })] }) : t;
	}, _e = (e, t) => {
		!(E?.(t) ?? !!T) || !T || S(e.target, w) || T(t);
	}, ve = (e, t) => {
		!(E?.(t) ?? !!T) || !T || S(e.target, w) || ee(e, () => T(t));
	};
	return /* @__PURE__ */ g(i, {
		templateRows: Q ? "auto 1fr" : "1fr",
		className: m("overflowHidden w100 h100", J.root, _),
		style: { minHeight: v },
		children: [Q && /* @__PURE__ */ h("h3", {
			className: "marginBottomXs",
			children: u
		}), /* @__PURE__ */ g("div", {
			className: "surface0 overflowHidden relative w100 h100",
			children: [/* @__PURE__ */ h(e, {
				ref: C,
				className: "w100 h100",
				children: /* @__PURE__ */ g(te, { children: [/* @__PURE__ */ g("table", {
					className: m(J.table, y),
					children: [
						/* @__PURE__ */ h("colgroup", { children: z.map((e) => /* @__PURE__ */ h("col", { style: e.width ? { width: e.width } : void 0 }, e.id)) }),
						/* @__PURE__ */ h("thead", {
							className: m("stickyTop", J.head),
							children: I ? /* @__PURE__ */ h(c.Root, {
								sensors: W,
								modifiers: [se],
								onDragEnd: he,
								children: /* @__PURE__ */ h(c.Container, {
									containerId: "base-table-header",
									items: V,
									layout: "horizontal",
									children: G.map((e, t) => /* @__PURE__ */ h("tr", {
										className: J.headerRow,
										children: e.map((e) => {
											let { isPinnedLeft: t, left: n, isPinnedBoundary: r } = le(e, K, q), i = e.isPlaceholder || !U.has(e.column.id) || de.has(e.column.id);
											return /* @__PURE__ */ h(Y, {
												header: e,
												isPinnedLeft: t,
												left: n,
												isPinnedBoundary: r,
												isDragDisabled: i,
												enableColumnResizing: F,
												enableColumnReordering: I,
												columnResizeMinWidth: L,
												renderHeaderContent: ge,
												onResizeColumn: pe,
												onResetColumnWidth: me
											}, e.id);
										})
									}, `header-group-${t}`))
								})
							}) : G.map((e, t) => /* @__PURE__ */ h("tr", {
								className: J.headerRow,
								children: e.map((e) => {
									let { isPinnedLeft: t, left: n, isPinnedBoundary: r } = le(e, K, q);
									return /* @__PURE__ */ h(Y, {
										header: e,
										isPinnedLeft: t,
										left: n,
										isPinnedBoundary: r,
										isDragDisabled: !0,
										enableColumnResizing: F,
										enableColumnReordering: I,
										columnResizeMinWidth: L,
										renderHeaderContent: ge,
										onResizeColumn: pe,
										onResetColumnWidth: me
									}, e.id);
								})
							}, `header-group-${t}`))
						}),
						/* @__PURE__ */ g("tbody", { children: [Z.rows.map((e) => {
							let t = E?.(e) ?? !!T;
							return /* @__PURE__ */ h("tr", {
								className: m(J.bodyRow, t && J.bodyRowInteractive, e.getIsSelected() && J.bodyRowSelected, D?.(e)),
								"aria-selected": b === "none" ? void 0 : e.getIsSelected(),
								tabIndex: t ? 0 : void 0,
								onClick: (t) => _e(t, e),
								onKeyDown: (t) => ve(t, e),
								children: fe([
									...e.getLeftVisibleCells(),
									...e.getCenterVisibleCells(),
									...e.getRightVisibleCells()
								]).map((t) => {
									let n = x(t.column.columnDef), r = oe(t.column.columnDef.cell, t.getContext()), i = {
										row: e,
										cell: t,
										defaultContent: r,
										columnMeta: n
									}, a = A?.(i), o = t.column.getIsPinned() === "left", s = t.column.id === q, c = {
										...n?.align ? { textAlign: n.align } : {},
										...o ? { left: K[t.column.id] ?? "0px" } : {},
										...k?.(i) ?? {}
									};
									return /* @__PURE__ */ h("td", {
										className: m(J.bodyCell, o && J.bodyCellPinnedLeft, s && J.pinnedLeftBoundary, a?.mergeWithNext && J.bodyCellMergedWithNext, O?.(i)),
										style: c,
										children: a?.hideContent ? null : j ? j(i) : r
									}, t.id);
								})
							}, e.id);
						}), p && $ && /* @__PURE__ */ h("tr", {
							className: J.fetchingRow,
							children: /* @__PURE__ */ h("td", {
								colSpan: Math.max(B.length, 1),
								className: J.feedbackCell,
								children: /* @__PURE__ */ h(n, {})
							})
						})] })
					]
				}), R] })
			}), /* @__PURE__ */ g("div", {
				className: "absolute inset0 border",
				style: {
					pointerEvents: "none",
					zIndex: 20
				},
				children: [!f && !p && !$ && /* @__PURE__ */ h(t, { className: J.noData }), f && !$ && /* @__PURE__ */ h(n, { className: "h100 w100" })]
			})]
		})]
	});
}
var Z = {
	formattedCellValue: "jPPyP7",
	formattedCell: "Oa7e5P",
	formattedCellInner: "KhQtgF"
};
//#endregion
//#region src/base-table/ui/FormattedTableCell.tsx
function Q({ displayValue: e, align: t = "left" }) {
	let n = f(null), i = e.showValue, a = e.showIcon ? e.icon : void 0, o = i && e.value != null ? String(e.value) : "", s = v(e.state), c = t === "right" ? "flex-end" : t === "center" ? "center" : "flex-start", l = t === "center" ? "center" : t === "right" ? "right" : "left";
	return u(() => {
		let t = n.current;
		if (!t) return;
		let r = () => {
			if (!e.overflowTooltip || !o) {
				t.removeAttribute("title");
				return;
			}
			if (t.scrollWidth > t.clientWidth + 1) {
				t.setAttribute("title", o);
				return;
			}
			t.removeAttribute("title");
		};
		if (r(), typeof ResizeObserver > "u") return;
		let i = new ResizeObserver(r);
		return i.observe(t), () => {
			i.disconnect();
		};
	}, [e.overflowTooltip, o]), /* @__PURE__ */ h("div", {
		className: m(Z.formattedCell, s),
		children: /* @__PURE__ */ g("div", {
			className: Z.formattedCellInner,
			style: { justifyContent: c },
			children: [
				e.iconPosition === "left" ? /* @__PURE__ */ h(r, { state: a }) : null,
				i ? /* @__PURE__ */ h("span", {
					ref: n,
					className: m("textOverflow", Z.formattedCellValue),
					style: { textAlign: l },
					children: o
				}) : null,
				e.iconPosition === "right" ? /* @__PURE__ */ h(r, { state: a }) : null
			]
		})
	});
}
//#endregion
export { G as a, z as c, B as d, N as f, j as g, A as h, K as i, L as l, M as m, de as n, W as o, P as p, q as r, U as s, Q as t, I as u };

//# sourceMappingURL=base-table-CTafaxo2.js.map