/**
 * Copyright IBM Corp. 2020, 2026
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_index = require("../../../node_modules/classnames/index.js");
const require_settings = require("../../../settings.js");
const require_useResizeObserver = require("../../../global/js/hooks/useResizeObserver.js");
const require_stateReducer = require("./addons/stateReducer.js");
let react = require("react");
react = require_runtime.__toESM(react);
let prop_types = require("prop-types");
prop_types = require_runtime.__toESM(prop_types);
let _carbon_react = require("@carbon/react");
//#region src/components/Datagrid/Datagrid/DatagridToolbar.tsx
/**
* Copyright IBM Corp. 2021, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
var import_classnames = /* @__PURE__ */ require_runtime.__toESM(require_index.default);
const blockClass = `${require_settings.pkg.prefix}--datagrid__table-toolbar`;
const DatagridBatchActionsToolbar = (datagridState, width, ref) => {
	const [displayAllInMenu, setDisplayAllInMenu] = (0, react.useState)(false);
	const [initialListWidth, setInitialListWidth] = (0, react.useState)(null);
	const [receivedInitialWidth, setReceivedInitialWidth] = (0, react.useState)(false);
	const { onSelectAllRows, state: { selectedRowIds }, toggleAllRowsSelected, toolbarBatchActions, toolbarBatchActionsDisplayMin, setGlobalFilter, rows, dispatch, getRowId, batchActionMenuButtonLabel, translateWithIdBatchActions } = datagridState;
	const carbonPrefix = (0, _carbon_react.usePrefix)();
	const [availableRowsCount, setAvailableRowsCount] = (0, react.useState)(rows.length);
	const batchActionMenuButtonLabelText = batchActionMenuButtonLabel ?? "More";
	const selectedKeys = Object.keys(selectedRowIds || {});
	const totalSelected = selectedKeys.length;
	(0, react.useEffect)(() => {
		const countDisabledRows = (rows.find((row) => row.getRowProps)?.getRowProps?.())?.nonselectablerows?.length || 0;
		if (rows) setAvailableRowsCount(rows.length - countDisabledRows);
	}, [rows]);
	(0, react.useEffect)(() => {
		if (totalSelected === 1 && !receivedInitialWidth) {
			const batchActionListWidth = ref?.current?.querySelector(`.${carbonPrefix}--action-list`).offsetWidth;
			setInitialListWidth(batchActionListWidth);
			setReceivedInitialWidth(true);
		}
	}, [
		totalSelected,
		receivedInitialWidth,
		ref,
		carbonPrefix
	]);
	(0, react.useEffect)(() => {
		if (width < ref?.current.querySelector(`.${carbonPrefix}--batch-summary`).offsetWidth + initialListWidth + 32) setDisplayAllInMenu(true);
		else setDisplayAllInMenu(false);
	}, [
		width,
		ref,
		initialListWidth,
		carbonPrefix
	]);
	const getSelectedRowData = () => {
		if (selectedKeys.length === 0) return [];
		return selectedKeys.map((rowIndex) => {
			const filteredRow = rows.filter((row) => row.index === parseInt(rowIndex));
			return filteredRow.length ? filteredRow[0] : [];
		});
	};
	const renderBatchActionOverflow = () => {
		const menuClass = `${blockClass}__button-menu`;
		const minWidthBeforeOverflowIcon = 380;
		if (toolbarBatchActions && !displayAllInMenu && (!toolbarBatchActionsDisplayMin && toolbarBatchActions?.length <= 3 || toolbarBatchActionsDisplayMin !== void 0 && toolbarBatchActions?.length <= toolbarBatchActionsDisplayMin)) return;
		const renderItem = (batchAction, index) => /* @__PURE__ */ react.default.createElement(_carbon_react.MenuItem, {
			key: `${batchAction.label}-${index}`,
			label: batchAction.label,
			onClick: (event) => onClickHandler(event, batchAction)
		});
		return /* @__PURE__ */ react.default.createElement(_carbon_react.MenuButton, {
			label: batchActionMenuButtonLabelText,
			className: (0, import_classnames.default)([menuClass, { [`${menuClass}-icon-only`]: width <= minWidthBeforeOverflowIcon }]),
			tabIndex: totalSelected > 0 ? 0 : -1,
			menuAlignment: "bottom"
		}, toolbarBatchActions?.map((batchAction, index) => {
			let hidden = toolbarBatchActionsDisplayMin === void 0 && index < 2 && !displayAllInMenu;
			if (toolbarBatchActionsDisplayMin !== void 0 && index < toolbarBatchActionsDisplayMin && !displayAllInMenu) hidden = true;
			if (!hidden) return renderItem(batchAction, index);
		}));
	};
	const onClickHandler = (event, batchAction) => {
		batchAction.onClick(getSelectedRowData(), event);
		if (batchAction.type === "select_all") toggleAllRowsSelected(true);
	};
	const onCancelHandler = () => {
		require_stateReducer.handleSelectAllRowData({
			dispatch,
			rows: [],
			getRowId,
			isChecked: false,
			indeterminate: void 0
		});
		toggleAllRowsSelected(false);
		setGlobalFilter?.(null);
	};
	const onSelectAllHandler = () => {
		toggleAllRowsSelected(true);
		onSelectAllRows?.(true);
		require_stateReducer.handleSelectAllRowData({
			dispatch,
			rows,
			getRowId,
			indeterminate: void 0,
			isChecked: void 0
		});
	};
	return /* @__PURE__ */ react.default.createElement(_carbon_react.TableBatchActions, {
		shouldShowBatchActions: totalSelected > 0,
		totalSelected,
		onCancel: onCancelHandler,
		onSelectAll: onSelectAllHandler,
		totalCount: availableRowsCount,
		translateWithId: translateWithIdBatchActions
	}, !displayAllInMenu && toolbarBatchActions && toolbarBatchActions?.map((batchAction, index) => {
		if (!toolbarBatchActionsDisplayMin && index < 2 && toolbarBatchActions.length > 3 || index < 3 && toolbarBatchActions.length <= 3 || toolbarBatchActionsDisplayMin !== void 0 && index < toolbarBatchActionsDisplayMin) return /* @__PURE__ */ react.default.createElement(_carbon_react.TableBatchAction, {
			key: `${batchAction.label}-${index}`,
			renderIcon: batchAction.renderIcon,
			onClick: (event) => onClickHandler(event, batchAction),
			className: (0, import_classnames.default)({ [`${carbonPrefix}--noLabel`]: !batchAction.label || batchAction.label === "" }),
			iconDescription: batchAction.label,
			tabIndex: totalSelected > 0 ? 0 : -1
		}, batchAction.label);
	}), renderBatchActionOverflow());
};
const DatagridToolbar = ({ ariaToolbarLabel, ...datagridState }) => {
	const ref = (0, react.useRef)(null);
	const { width } = require_useResizeObserver.useResizeObserver(ref);
	const { DatagridActions, DatagridBatchActions, batchActions, rowSize } = datagridState;
	const getRowHeight = rowSize || "lg";
	return batchActions && DatagridActions ? /* @__PURE__ */ react.default.createElement("div", {
		ref,
		className: (0, import_classnames.default)([blockClass, `${blockClass}--${getRowHeight}`])
	}, /* @__PURE__ */ react.default.createElement(_carbon_react.TableToolbar, { "aria-label": ariaToolbarLabel }, DatagridActions && /* @__PURE__ */ react.default.createElement(DatagridActions, datagridState), DatagridBatchActionsToolbar?.(datagridState, width, ref))) : DatagridActions ? /* @__PURE__ */ react.default.createElement("div", { className: blockClass }, /* @__PURE__ */ react.default.createElement(_carbon_react.TableToolbar, { "aria-label": ariaToolbarLabel }, DatagridActions && /* @__PURE__ */ react.default.createElement(DatagridActions, datagridState), DatagridBatchActions?.(datagridState))) : null;
};
DatagridToolbar.propTypes = { ariaToolbarLabel: prop_types.default.string };
//#endregion
exports.default = DatagridToolbar;
