/**
 * 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 RowSizeRadioGroup from "./RowSizeRadioGroup.js";
import React, { useEffect, useRef } from "react";
import PropTypes from "prop-types";
import { IconButton, Layer, Popover, PopoverContent, usePrefix } from "@carbon/react";
import { Settings } from "@carbon/react/icons";

//#region src/components/Datagrid/Datagrid/addons/RowSize/RowSizeDropdown.jsx
/**
* Copyright IBM Corp. 2021, 2025
*
* 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__row-size`;
const RowSizeDropdown = ({ align = "bottom-right", legendText = "Row settings", autoAlign, ...props }) => {
	const radioGroupRef = useRef(void 0);
	const [isOpen, setIsOpen] = React.useState(false);
	const carbonPrefix = usePrefix();
	const onCloseHandler = () => {
		setIsOpen(false);
	};
	const onKeyHandler = (e) => {
		if (e.key === "Escape") onCloseHandler();
	};
	const onBlurHandler = (e) => {
		if (!e.currentTarget.contains(e.relatedTarget)) onCloseHandler();
	};
	useEffect(() => {
		if (isOpen) ((radioGroupRef?.current)?.querySelector(`.${carbonPrefix}--radio-button:checked`))?.focus();
	}, [isOpen]);
	const onClickHandler = () => {
		setIsOpen((prev) => !prev);
	};
	return /* @__PURE__ */ React.createElement(Popover, {
		tabIndex: -1,
		isTabTip: true,
		align: "bottom-right",
		open: isOpen,
		onRequestClose: onCloseHandler,
		className: `${blockClass}-options-container`,
		onKeyDown: onKeyHandler,
		onBlur: onBlurHandler,
		autoAlign
	}, /* @__PURE__ */ React.createElement(IconButton, {
		align,
		kind: "ghost",
		onClick: onClickHandler,
		label: legendText,
		className: (0, import_classnames.default)(`${blockClass}-button`, { [`${blockClass}__row-settings-trigger--open`]: isOpen })
	}, /* @__PURE__ */ React.createElement(Settings, { size: 16 })), /* @__PURE__ */ React.createElement(PopoverContent, null, /* @__PURE__ */ React.createElement(Layer, null, /* @__PURE__ */ React.createElement(RowSizeRadioGroup, {
		ref: radioGroupRef,
		...props,
		legendText
	}))));
};
RowSizeDropdown.propTypes = {
	align: IconButton.propTypes.align,
	autoAlign: PropTypes.bool,
	datagridName: PropTypes.string,
	legendText: PropTypes.string,
	light: PropTypes.bool,
	onChange: PropTypes.func.isRequired,
	selectedOption: PropTypes.string
};

//#endregion
export { RowSizeDropdown as default };