import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { useRecordBoardContext } from "../../hooks/useRecordBoardContext.js";
import PisellBatchActionBar from "../../../pisellBatchActionBar/PisellBatchActionBar.js";
import React, { useCallback, useMemo } from "react";
//#region src/components/pisellRecordBoard/shellFrame/BatchActionBar/index.tsx
function getRowKey(record, rowKey) {
	var _ref, _record$id;
	if (typeof rowKey === "function") return rowKey(record);
	if (typeof rowKey === "string" && record != null && rowKey in record) return record[rowKey];
	return (_ref = (_record$id = record === null || record === void 0 ? void 0 : record.id) !== null && _record$id !== void 0 ? _record$id : record === null || record === void 0 ? void 0 : record.key) !== null && _ref !== void 0 ? _ref : record;
}
/**
* RecordBoard 下的多选操作栏：从 context 取选中态与数据，渲染 PisellBatchActionBar。
* 仅在有选中项时展示；全选/反选/清空基于当前页 data。
*/
const RecordBoardBatchActionBar = (props) => {
	var _ctx$childComponentPr, _ctx$childComponentPr2;
	const ctx = useRecordBoardContext({ displayName: "batchActionBar" });
	const { data, total, selectedKeys, selectedRows, onSelectionChange, rowKey: rowKeyFromContext } = ctx;
	const rowKey = rowKeyFromContext !== null && rowKeyFromContext !== void 0 ? rowKeyFromContext : "id";
	const currentPageKeys = useMemo(() => data.map((record) => getRowKey(record, rowKey)), [data, rowKey]);
	const currentPageTotal = data.length;
	const barProps = _objectSpread2(_objectSpread2({
		selectedKeys,
		selectedRows,
		total,
		currentPageTotal,
		currentPageKeys,
		onSelectAll: useCallback(() => {
			onSelectionChange(currentPageKeys, data);
		}, [
			currentPageKeys,
			data,
			onSelectionChange
		]),
		onClear: useCallback(() => {
			onSelectionChange([], []);
		}, [onSelectionChange]),
		onInvert: useCallback(() => {
			const currentSet = new Set(currentPageKeys);
			const selectedSet = new Set(selectedKeys);
			const nextKeys = [];
			const nextRows = [];
			currentPageKeys.forEach((k, i) => {
				if (!selectedSet.has(k)) {
					nextKeys.push(k);
					nextRows.push(data[i]);
				}
			});
			const restSelectedKeys = selectedKeys.filter((k) => !currentSet.has(k));
			const restSelectedRows = selectedRows.filter((_, i) => !currentSet.has(selectedKeys[i]));
			onSelectionChange([...restSelectedKeys, ...nextKeys], [...restSelectedRows, ...nextRows]);
		}, [
			currentPageKeys,
			data,
			selectedKeys,
			selectedRows,
			onSelectionChange
		])
	}, (_ctx$childComponentPr = (_ctx$childComponentPr2 = ctx.childComponentProps) === null || _ctx$childComponentPr2 === void 0 ? void 0 : _ctx$childComponentPr2.batchActionBar) !== null && _ctx$childComponentPr !== void 0 ? _ctx$childComponentPr : {}), props);
	if (selectedKeys.length === 0) return null;
	return /* @__PURE__ */ React.createElement(PisellBatchActionBar, barProps);
};
RecordBoardBatchActionBar.displayName = "RecordBoard.BatchActionBar";
//#endregion
export { RecordBoardBatchActionBar as default };
