/**
 * 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.
 */

import { __toESM } from "../../../_virtual/_rolldown/runtime.js";
import { require_classnames } from "../../../node_modules/classnames/index.js";
import { pkg } from "../../../settings.js";
import { useIsomorphicEffect } from "../../../global/js/hooks/useIsomorphicEffect.js";
import { handleOnPageSelectAllRowData, handleSelectAllRowData } from "./addons/stateReducer.js";
import React, { useCallback, useState } from "react";
import { TableSelectAll } from "@carbon/react";
//#region src/components/Datagrid/Datagrid/DatagridSelectAll.tsx
/**
* Copyright IBM Corp. 2020, 2023
*
* 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__ */ __toESM(require_classnames());
const blockClass = `${pkg.prefix}--datagrid`;
const SelectAll = (datagridState) => {
	const [windowSize, setWindowSize] = useState();
	useIsomorphicEffect(() => {
		setWindowSize(window.innerWidth);
		/* istanbul ignore next */
		function updateSize() {
			setWindowSize(window.innerWidth);
		}
		if (typeof window !== "undefined") window.addEventListener("resize", updateSize);
		return () => window.removeEventListener("resize", updateSize);
	}, []);
	const { isFetching, getToggleAllRowsSelectedProps, getToggleAllPageRowsSelectedProps, tableId, hideSelectAll, DatagridPagination, radio, columns, withStickyColumn, dispatch, rows, getRowId, toggleAllRowsSelected, withVirtualScroll, onAllRowSelect } = datagridState;
	const isFirstColumnStickyLeft = columns[0]?.sticky === "left" && withStickyColumn;
	const { onChange, ...selectProps } = (DatagridPagination ? getToggleAllPageRowsSelectedProps : getToggleAllRowsSelectedProps)();
	const { indeterminate } = selectProps;
	const handleOnPageSelectAllChange = useCallback((event) => {
		handleOnPageSelectAllRowData({
			dispatch,
			rows,
			getRowId,
			isChecked: event.target.checked,
			indeterminate
		});
		onAllRowSelect?.(rows, event);
		return onChange?.(event);
	}, [
		dispatch,
		getRowId,
		indeterminate,
		onAllRowSelect,
		onChange,
		rows
	]);
	const handleSelectAllChange = useCallback((event) => {
		if (indeterminate) {
			handleSelectAllRowData({
				dispatch,
				rows,
				getRowId,
				indeterminate: true,
				isChecked: void 0
			});
			toggleAllRowsSelected(false);
			onAllRowSelect?.(rows, event);
			return onChange?.({ target: { checked: false } });
		}
		handleSelectAllRowData({
			dispatch,
			rows,
			getRowId,
			isChecked: event.target.checked,
			indeterminate
		});
		onAllRowSelect?.(rows, event);
		return onChange?.(event);
	}, [
		dispatch,
		getRowId,
		indeterminate,
		onAllRowSelect,
		onChange,
		rows,
		toggleAllRowsSelected
	]);
	if (hideSelectAll || radio) return /* @__PURE__ */ React.createElement("th", { className: (0, import_classnames.default)(`${blockClass}__head-hidden-select-all`, { [`${blockClass}__select-all-sticky-left`]: isFirstColumnStickyLeft && Number(windowSize) > 671 }) });
	return /* @__PURE__ */ React.createElement(TableSelectAll, {
		...selectProps,
		checked: selectProps?.checked || false,
		className: (0, import_classnames.default)(`${blockClass}__head-select-all`, `${blockClass}__checkbox-cell`, { [`${blockClass}__checkbox-cell-sticky-left`]: isFirstColumnStickyLeft && Number(windowSize) > 671 }),
		name: `${tableId}-select-all-checkbox-name`,
		onSelect: withVirtualScroll ? handleSelectAllChange : handleOnPageSelectAllChange,
		disabled: isFetching || selectProps?.disabled,
		id: `${tableId}-select-all-checkbox-id`
	});
};
//#endregion
export { SelectAll };
