/**
 * 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 React, { useEffect, useState } from "react";
import PropTypes from "prop-types";
import { Checkbox, OverflowMenu, OverflowMenuItem } from "@carbon/react";
import { CaretDown } from "@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__ */ __toESM(require_classnames());
const blockClass = `${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] = useState(SELECT_ALL_PAGE_ROWS);
	useEffect(() => {
		if (onSelectAllRows) onSelectAllRows(isAllRowsSelected && selectAllMode === SELECT_ALL_ROWS);
	}, [
		isAllRowsSelected,
		selectAllMode,
		onSelectAllRows
	]);
	const [windowSize, setWindowSize] = useState();
	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.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.createElement("span", null, /* @__PURE__ */ React.createElement(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.createElement(OverflowMenu, {
		renderIcon: (props) => /* @__PURE__ */ React.createElement(CaretDown, {
			size: 16,
			...props
		}),
		size: "sm",
		"aria-label": allRowsLabel,
		menuOptionsClass: `${blockClass}__select-all-toggle-overflow`
	}, /* @__PURE__ */ React.createElement(OverflowMenuItem, {
		itemText: allPageRowsLabel,
		requireTitle: true,
		disabled,
		onClick: () => {
			setSelectAllMode(SELECT_ALL_PAGE_ROWS);
			getToggleAllRowsSelectedProps?.()?.onChange({ target: { checked: false } });
			getToggleAllPageRowsSelectedProps().onChange({ target: { checked: true } });
		}
	}), /* @__PURE__ */ React.createElement(OverflowMenuItem, {
		itemText: allRowsLabel,
		requireTitle: true,
		disabled,
		onClick: () => {
			setSelectAllMode(SELECT_ALL_ROWS);
			getToggleAllRowsSelectedProps().onChange({ target: { checked: true } });
		}
	})));
};
SelectAllWithToggle.propTypes = {
	allPageRowsLabel: PropTypes.string,
	allRowsLabel: PropTypes.string,
	columns: PropTypes.arrayOf(PropTypes.object),
	getToggleAllPageRowsSelectedProps: PropTypes.func.isRequired,
	getToggleAllRowsSelectedProps: PropTypes.func.isRequired,
	isAllRowsSelected: PropTypes.bool.isRequired,
	isFetching: PropTypes.bool,
	selectAllToggle: PropTypes.object,
	tableId: PropTypes.string.isRequired,
	withStickyColumn: PropTypes.bool
};

//#endregion
export { SelectAllWithToggle as default };