/**
 * 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_common_column_ids = require('./common-column-ids.js');
const require_DatagridSelectAllWithToggle = require('./Datagrid/DatagridSelectAllWithToggle.js');
let react = require("react");
react = require_runtime.__toESM(react);

//#region src/components/Datagrid/useSelectAllToggle.tsx
/**
* Copyright IBM Corp. 2021, 2024
*
* 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__select-all`;
const useSelectAllToggle = (hooks) => {
	useSelectAllWithToggleComponent(hooks);
	useAddClassNameToSelectRow(hooks);
};
const useSelectAllWithToggleComponent = (hooks) => {
	const useInstance = (instance) => {
		const { headers, DatagridPagination } = instance;
		const headersWithSelectAllToggle = headers?.map((header) => {
			if (header.id === require_common_column_ids.selectionColumnId && DatagridPagination) Object.assign(header, { Header });
			return header;
		});
		Object.assign(instance, { headers: headersWithSelectAllToggle });
	};
	hooks.useInstance.push(useInstance);
};
const useAddClassNameToSelectRow = (hooks) => {
	const [windowSize, setWindowSize] = (0, react.useState)(0);
	require_useIsomorphicEffect.useIsomorphicEffect(() => {
		setWindowSize(window.innerWidth);
		function updateSize() {
			setWindowSize(window.innerWidth);
		}
		window.addEventListener("resize", updateSize);
		return () => window.removeEventListener("resize", updateSize);
	}, []);
	hooks.getCellProps.push((props, data) => {
		const { column } = data.cell;
		const { DatagridPagination, columns, withStickyColumn } = data.instance;
		const isFirstColumnStickyLeft = columns[0]?.sticky === "left" && withStickyColumn;
		if (column.id === require_common_column_ids.selectionColumnId && DatagridPagination) return [props, { className: (0, import_classnames.default)(`${blockClass}-toggle-on`, { [`${blockClass}-sticky-left`]: isFirstColumnStickyLeft && windowSize > 671 }) }];
		return [props];
	});
};
const Header = (gridState) => {
	const { tableId, isFetching, selectAllToggle, getToggleAllPageRowsSelectedProps, getToggleAllRowsSelectedProps, isAllRowsSelected, withStickyColumn, columns } = gridState;
	const props = {
		tableId,
		isFetching,
		selectAllToggle,
		getToggleAllPageRowsSelectedProps,
		getToggleAllRowsSelectedProps,
		isAllRowsSelected,
		withStickyColumn,
		columns
	};
	return /* @__PURE__ */ react.default.createElement(require_DatagridSelectAllWithToggle.default, props);
};

//#endregion
exports.default = useSelectAllToggle;