/**
 * 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_useIsomorphicEffect = require("../../../global/js/hooks/useIsomorphicEffect.js");
const require_stateReducer = require("./addons/stateReducer.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _carbon_react = require("@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__ */ require_runtime.__toESM(require_index.default);
const blockClass = `${require_settings.pkg.prefix}--datagrid`;
const SelectAll = (datagridState) => {
	const [windowSize, setWindowSize] = (0, react.useState)();
	require_useIsomorphicEffect.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 = (0, react.useCallback)((event) => {
		require_stateReducer.handleOnPageSelectAllRowData({
			dispatch,
			rows,
			getRowId,
			isChecked: event.target.checked,
			indeterminate
		});
		onAllRowSelect?.(rows, event);
		return onChange?.(event);
	}, [
		dispatch,
		getRowId,
		indeterminate,
		onAllRowSelect,
		onChange,
		rows
	]);
	const handleSelectAllChange = (0, react.useCallback)((event) => {
		if (indeterminate) {
			require_stateReducer.handleSelectAllRowData({
				dispatch,
				rows,
				getRowId,
				indeterminate: true,
				isChecked: void 0
			});
			toggleAllRowsSelected(false);
			onAllRowSelect?.(rows, event);
			return onChange?.({ target: { checked: false } });
		}
		require_stateReducer.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.default.createElement("th", { className: (0, import_classnames.default)(`${blockClass}__head-hidden-select-all`, { [`${blockClass}__select-all-sticky-left`]: isFirstColumnStickyLeft && Number(windowSize) > 671 }) });
	return /* @__PURE__ */ react.default.createElement(_carbon_react.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
exports.SelectAll = SelectAll;
