import { a as e, c as t, d as n, i as r, m as i, n as ee, o as te, r as ne, s as re, t as ie } from "../chunks/base-table-Dv9i3CAT.js";
import { useEffect as a, useEffectEvent as o, useMemo as s, useRef as c, useState as ae } from "react";
import { cn as oe } from "@ryuzaki13/react-foundation-lib/utils";
import { ChevronDownIcon as l, ChevronRightIcon as u } from "lucide-react";
import { jsx as d, jsxs as f } from "react/jsx-runtime";
import { formatPipelineDisplayValue as se } from "@ryuzaki13/react-foundation-lib/formatters";
import { resolveTableLength as p, useTableRowSelection as ce } from "@ryuzaki13/react-foundation-lib/table";
import { getCoreRowModel as m, getExpandedRowModel as le, useReactTable as ue } from "@tanstack/react-table";
import { buildTreeTableRows as de } from "@ryuzaki13/react-foundation-lib/tree-table";
var h = {
	treeColumnCell: "jOS9rT",
	selectedIndicatorCell: "zHOAf7",
	treeCell: "j48cXg",
	treeCellContent: "mf4-Kr",
	expanderButton: "p8pb0F",
	expanderSpacer: "KIudLf",
	sentinel: "bJsPGU"
};
//#endregion
//#region src/tree-table/TreeTable.tsx
function g(e) {
	return Object.fromEntries(e.map((e) => [e, !0]));
}
function _(e) {
	return typeof e == "boolean" ? [] : Object.keys(e).filter((t) => e[t]);
}
function v(e, t) {
	return t.get(e.id) ?? e.original;
}
function fe(e, t, n, r) {
	return e.cell.column.id === n ? /* @__PURE__ */ f("div", {
		className: h.treeCell,
		style: { paddingInlineStart: `calc(${e.row.depth} * ${p(r) ?? "0em"})` },
		children: [e.row.getCanExpand() ? /* @__PURE__ */ d("button", {
			type: "button",
			className: h.expanderButton,
			"aria-label": e.row.getIsExpanded() ? "Свернуть строку" : "Развернуть строку",
			"data-tree-table-interactive": "true",
			"data-ui": "tree-table-expander",
			"data-action": e.row.getIsExpanded() ? "collapse-tree-table-row" : "expand-tree-table-row",
			onClick: (t) => {
				t.stopPropagation(), e.row.toggleExpanded();
			},
			children: e.row.getIsExpanded() ? /* @__PURE__ */ d(l, { size: 16 }) : /* @__PURE__ */ d(u, { size: 16 })
		}) : /* @__PURE__ */ d("span", {
			className: h.expanderSpacer,
			"aria-hidden": "true"
		}), /* @__PURE__ */ d("div", {
			className: h.treeCellContent,
			children: t
		})]
	}) : t;
}
function y({ title: l, isLoading: u, isFetching: f, data: p, columns: y, hierarchy: b, selectionMode: x = "none", expandFirstLevel: S = !1, defaultExpandedRowIds: C, treeColumnId: pe, indentSize: me = 1, className: he, tableClassName: ge, columnVisibility: _e, defaultColumnVisibility: ve, columnOrder: ye, defaultColumnOrder: be, columnSizing: xe, defaultColumnSizing: Se, columnPinning: w, defaultColumnPinning: T, enableColumnResizing: E = !1, enableColumnReordering: D = !1, columnResizeMinWidth: O, onColumnVisibilityChange: k, onColumnOrderChange: A, onColumnSizingChange: j, onColumnPinningChange: Ce, onRowClick: M, onRowSelectionChange: we, onExpandedRowIdsChange: Te, onReachEnd: N, getRowCanSelect: P, getRowClassName: Ee, renderHeaderContextMenu: De, renderCellContent: F }) {
	let I = s(() => de(p ?? [], b), [p, b]), L = s(() => t(y), [y]), R = s(() => {
		let e = new Set(C ?? []);
		if (S) for (let t of I.rootRowIds) e.add(t);
		return Array.from(e);
	}, [
		C,
		S,
		I.rootRowIds
	]), [z, B] = ae({}), V = c(!1), H = c(null), U = c(null), { rowSelection: Oe, activateRowSelection: ke } = ce({
		availableRowIds: I.allRowIds,
		rowById: I.rowById,
		selectionMode: x,
		onRowSelectionChange: we
	}), { columnVisibility: W, onColumnVisibilityChange: Ae } = ne({
		columnVisibility: _e,
		defaultColumnVisibility: ve,
		onColumnVisibilityChange: k
	}), { columnOrder: G, onColumnOrderChange: je } = te({
		columnOrder: ye,
		defaultColumnOrder: be,
		onColumnOrderChange: A
	}), { columnSizing: K, onColumnSizingChange: Me } = r({
		columnSizing: xe,
		defaultColumnSizing: Se,
		columnResizeMinWidth: O,
		onColumnSizingChange: j
	}), { columnPinning: q, onColumnPinningChange: J } = e({
		columnPinning: w,
		defaultColumnPinning: T,
		onColumnPinningChange: Ce
	}), Y = s(() => ({
		columnOrder: G,
		columnPinning: q,
		columnSizing: K,
		columnVisibility: W
	}), [
		G,
		q,
		K,
		W
	]), Ne = o((e, t) => {
		if (e.length === 0) {
			V.current = !1, B({});
			return;
		}
		let n = new Set(e);
		B((e) => {
			let r = _(e).filter((e) => n.has(e));
			return V.current ? g(r) : (V.current = !0, g(Array.from(/* @__PURE__ */ new Set([...r, ...t.filter((e) => n.has(e))]))));
		});
	}), Pe = o((e) => {
		Te?.(_(e));
	}), Fe = o(() => {
		N?.();
	});
	a(() => {
		Ne(I.allRowIds, R);
	}, [R, I.allRowIds]), a(() => {
		Pe(z);
	}, [z]), a(() => {
		if (!N || !H.current || !U.current || u) return;
		let e = new IntersectionObserver((e) => {
			e[0]?.isIntersecting && Fe();
		}, {
			root: H.current,
			threshold: .1
		});
		return e.observe(U.current), () => e.disconnect();
	}, [
		u,
		N,
		I.rows.length
	]);
	let X = ue({
		data: I.rows,
		columns: y,
		state: {
			columnOrder: G,
			columnPinning: q,
			columnSizing: K,
			columnVisibility: W,
			expanded: z,
			rowSelection: Oe
		},
		getRowId: (e) => b.getRowId(e),
		getSubRows: (e) => e.children,
		getCoreRowModel: m(),
		getExpandedRowModel: le(),
		onColumnOrderChange: je,
		onColumnPinningChange: J,
		onColumnSizingChange: Me,
		onColumnVisibilityChange: Ae,
		onExpandedChange: B,
		enableMultiRowSelection: x === "multi",
		enableRowSelection: (e) => {
			if (x === "none") return !1;
			let t = v(e, I.rowById);
			return P ? P(t) : !0;
		}
	}), Z = i(X, Y), Q = pe ?? Z[0]?.id, $ = X.getRowModel().rows, Ie = s(() => re({
		rows: $,
		columns: Z,
		resolveRowBoundaryKey: (e) => e.depth,
		excludeColumnIds: Q ? /* @__PURE__ */ new Set([Q]) : void 0
	}), [
		$,
		Z,
		Q
	]);
	return /* @__PURE__ */ d(ee, {
		table: X,
		columnState: Y,
		title: l,
		isLoading: u,
		isFetching: f,
		className: he,
		tableClassName: ge,
		selectionMode: x,
		scrollableRef: H,
		interactiveElementSelector: "[data-tree-table-interactive=\"true\"]",
		onActivateRow: x !== "none" || M ? (e) => {
			let t = v(e, I.rowById);
			ke(e.id, e.getCanSelect()), M?.(t);
		} : void 0,
		isRowInteractive: (e) => !!(M || x !== "none" && e.getCanSelect()),
		getRowClassName: (e) => {
			let t = v(e, I.rowById);
			return Ee?.(t);
		},
		getCellClassName: ({ row: e, cell: t }) => oe(t.column.id === Q && h.treeColumnCell, t.column.id === Q && e.getIsSelected() && h.selectedIndicatorCell),
		getCellLayout: ({ cell: e }) => Ie.get(e.id),
		renderHeaderContextMenu: De,
		enableColumnResizing: E,
		enableColumnReordering: D,
		columnResizeMinWidth: O,
		renderCellContent: (e) => {
			let t = v(e.row, I.rowById), r = se({
				field: L.runtimeByColumnId[e.cell.column.id],
				rawValue: e.cell.getValue(),
				rowData: t,
				rowKind: "tree"
			}), i = n({
				columnId: e.cell.column.id,
				runtimeByColumnId: L.runtimeByColumnId,
				customCellColumnIds: L.customCellColumnIds
			}) ? /* @__PURE__ */ d(ie, {
				displayValue: r,
				align: e.columnMeta?.align
			}) : e.defaultContent;
			return fe(e, F ? F({
				...e,
				defaultContent: i,
				displayValue: r
			}) : i, Q, me);
		},
		afterTableContent: N ? /* @__PURE__ */ d("div", {
			ref: U,
			className: h.sentinel,
			"aria-hidden": "true"
		}) : null
	});
}
//#endregion
export { y as TreeTable };

//# sourceMappingURL=index.js.map