/**
 * 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');
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");
let _carbon_react_icons = require("@carbon/react/icons");

//#region src/components/Datagrid/Datagrid/DatagridSelectAllWithToggle.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 SELECT_ALL_PAGE_ROWS = "pageRows";
const SELECT_ALL_ROWS = "allRows";
const SelectAllWithToggle = ({ tableId, isFetching, isAllRowsSelected, selectAllToggle, getToggleAllPageRowsSelectedProps, getToggleAllRowsSelectedProps, allPageRowsLabel = "Select all on page", allRowsLabel = "Select all", columns, withStickyColumn }) => {
	const { onSelectAllRows, labels } = selectAllToggle || {};
	const [selectAllMode, setSelectAllMode] = (0, react.useState)(SELECT_ALL_PAGE_ROWS);
	(0, react.useEffect)(() => {
		if (onSelectAllRows) onSelectAllRows(isAllRowsSelected && selectAllMode === SELECT_ALL_ROWS);
	}, [
		isAllRowsSelected,
		selectAllMode,
		onSelectAllRows
	]);
	const [windowSize, setWindowSize] = (0, react.useState)();
	require_useIsomorphicEffect.useIsomorphicEffect(() => {
		setWindowSize(window.innerWidth);
		function updateSize() {
			setWindowSize(window.innerWidth);
		}
		window.addEventListener("resize", updateSize);
		return () => window.removeEventListener("resize", updateSize);
	}, []);
	if (labels) {
		allPageRowsLabel = labels?.allPageRows || allPageRowsLabel;
		allRowsLabel = labels.allRows || allRowsLabel;
	}
	const { onChange, ...selectProps } = (selectAllMode === SELECT_ALL_PAGE_ROWS ? getToggleAllPageRowsSelectedProps : getToggleAllRowsSelectedProps)();
	const disabled = isFetching || selectProps.disabled;
	const isFirstColumnStickyLeft = columns?.[0]?.sticky === "left" && withStickyColumn;
	return /* @__PURE__ */ react.default.createElement("th", {
		scope: "col",
		className: (0, import_classnames.default)(`${blockClass}__select-all-toggle-on`, { [`${blockClass}__select-all-sticky-left`]: isFirstColumnStickyLeft && (windowSize ?? 0) > 671 })
	}, /* @__PURE__ */ react.default.createElement("span", null, /* @__PURE__ */ react.default.createElement(_carbon_react.Checkbox, {
		...selectProps,
		name: `${tableId}-select-all-checkbox-name`,
		onClick: (e) => {
			onChange?.(e);
		},
		disabled,
		id: `${tableId}-select-all-checkbox-id`,
		labelText: allRowsLabel,
		hideLabel: true
	})), /* @__PURE__ */ react.default.createElement(_carbon_react.OverflowMenu, {
		renderIcon: (props) => /* @__PURE__ */ react.default.createElement(_carbon_react_icons.CaretDown, {
			size: 16,
			...props
		}),
		size: "sm",
		"aria-label": allRowsLabel,
		menuOptionsClass: `${blockClass}__select-all-toggle-overflow`
	}, /* @__PURE__ */ react.default.createElement(_carbon_react.OverflowMenuItem, {
		itemText: allPageRowsLabel,
		requireTitle: true,
		disabled,
		onClick: () => {
			setSelectAllMode(SELECT_ALL_PAGE_ROWS);
			getToggleAllRowsSelectedProps?.()?.onChange({ target: { checked: false } });
			getToggleAllPageRowsSelectedProps().onChange({ target: { checked: true } });
		}
	}), /* @__PURE__ */ react.default.createElement(_carbon_react.OverflowMenuItem, {
		itemText: allRowsLabel,
		requireTitle: true,
		disabled,
		onClick: () => {
			setSelectAllMode(SELECT_ALL_ROWS);
			getToggleAllRowsSelectedProps().onChange({ target: { checked: true } });
		}
	})));
};
SelectAllWithToggle.propTypes = {
	allPageRowsLabel: prop_types.default.string,
	allRowsLabel: prop_types.default.string,
	columns: prop_types.default.arrayOf(prop_types.default.object),
	getToggleAllPageRowsSelectedProps: prop_types.default.func.isRequired,
	getToggleAllRowsSelectedProps: prop_types.default.func.isRequired,
	isAllRowsSelected: prop_types.default.bool.isRequired,
	isFetching: prop_types.default.bool,
	selectAllToggle: prop_types.default.object,
	tableId: prop_types.default.string.isRequired,
	withStickyColumn: prop_types.default.bool
};

//#endregion
exports.default = SelectAllWithToggle;