/**
 * 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_useControllableState = require("../../global/js/hooks/useControllableState.js");
const require_usePrefersReducedMotion = require("../../global/js/hooks/usePrefersReducedMotion.js");
const require_devtools = require("../../global/js/utils/devtools.js");
const require_uuidv4 = require("../../global/js/utils/uuidv4.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");
let _carbon_motion = require("@carbon/motion");
_carbon_motion = require_runtime.__toESM(_carbon_motion);
let _carbon_utilities_react = require("@carbon/utilities-react");
//#region src/components/OptionsTile/OptionsTile.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}--options-tile`;
const componentName = "OptionsTile";
const defaults = { size: "lg" };
const OptionsTile = react.default.forwardRef((props, ref) => {
	const { children, className, enabled, invalid, invalidText, locked, lockedText, onChange, onToggle, open: userOpen, size = defaults.size, summary, title, titleId: userDefinedTitleId, warn, warnText, ...rest } = props;
	const [closing, setClosing] = (0, react.useState)(false);
	const [open, setOpen] = require_useControllableState.useControllableState(userOpen ?? false, onChange);
	const detailsRef = (0, react.useRef)(null);
	const contentRef = (0, react.useRef)(null);
	const headingRef = (0, react.useRef)(null);
	(0, _carbon_utilities_react.useNoInteractiveChildren)(headingRef);
	const titleId = userDefinedTitleId ?? `${require_uuidv4.default()}-title`;
	const isExpandable = children !== void 0;
	const isLocked = !invalid && !(!invalid && warn) && locked;
	const shouldReduceMotion = require_usePrefersReducedMotion.usePrefersReducedMotion();
	const expand = () => {
		if (detailsRef.current && contentRef.current && !shouldReduceMotion) {
			setOpen(true);
			detailsRef.current.open = true;
			const { paddingTop, paddingBottom, height } = getComputedStyle(contentRef.current);
			contentRef.current.animate([{
				paddingTop: 0,
				paddingBottom: 0,
				height: 0,
				opacity: 0,
				overflow: "hidden"
			}, {
				paddingTop,
				paddingBottom,
				height,
				opacity: 1,
				overflow: "hidden"
			}], {
				duration: Number(_carbon_motion.moderate01.replace("ms", "")),
				easing: _carbon_motion.easings.entrance.productive
			});
		} else setOpen(true);
	};
	const collapse = () => {
		if (contentRef.current && !shouldReduceMotion) {
			setClosing(true);
			const { paddingTop, paddingBottom, height } = getComputedStyle(contentRef.current);
			const animationDuration = Number(_carbon_motion.moderate01.replace("ms", ""));
			const animation = contentRef.current.animate([{
				paddingTop,
				paddingBottom,
				height,
				opacity: 1
			}, {
				paddingTop: 0,
				paddingBottom: 0,
				height: 0,
				opacity: 0
			}], {
				duration: animationDuration,
				easing: _carbon_motion.easings.entrance.productive
			});
			const callback = () => {
				setOpen(false);
				setClosing(false);
			};
			setTimeout(() => {
				callback();
			}, animationDuration * .9);
			animation.oncancel = callback;
		} else setOpen(false);
	};
	const handleSummaryClick = (evt) => {
		if (evt.target.closest(`.${blockClass}__toggle-container`)) {
			evt.preventDefault();
			evt.stopPropagation();
			return;
		}
		evt.preventDefault();
		if (open) collapse();
		else expand();
	};
	const renderTitle = () => {
		let Icon = null;
		let text = summary;
		if (invalid) {
			Icon = _carbon_react_icons.WarningFilled;
			text = invalidText;
		} else if (warn) {
			Icon = _carbon_react_icons.WarningAltFilled;
			text = warnText;
		} else if (locked) {
			Icon = _carbon_react_icons.Locked;
			if (!text) text = lockedText;
		}
		const summaryHidden = !(invalid || warn || locked) && enabled === false;
		const summaryClasses = (0, import_classnames.default)(`${blockClass}__summary`, {
			[`${blockClass}__summary--closing`]: closing,
			[`${blockClass}__summary--open`]: open,
			[`${blockClass}__summary--invalid`]: invalid,
			[`${blockClass}__summary--warn`]: warn,
			[`${blockClass}__summary--locked`]: locked,
			[`${blockClass}__summary--hidden`]: summaryHidden
		});
		return /* @__PURE__ */ react.default.createElement("div", { className: `${blockClass}__heading` }, /* @__PURE__ */ react.default.createElement(_carbon_react.Heading, {
			ref: headingRef,
			id: titleId,
			className: `${blockClass}__title`
		}, title), text && /* @__PURE__ */ react.default.createElement("span", {
			className: summaryClasses,
			"aria-hidden": summaryHidden
		}, Icon && /* @__PURE__ */ react.default.createElement(Icon, { size: 16 }), /* @__PURE__ */ react.default.createElement("span", { className: `${blockClass}__summary-text` }, text)));
	};
	return /* @__PURE__ */ react.default.createElement(_carbon_react.Section, {
		...rest,
		className: (0, import_classnames.default)(blockClass, className, `${blockClass}--${size}`, { [`${blockClass}--closing`]: closing }),
		ref,
		...require_devtools.getDevtoolsProps(componentName)
	}, isExpandable ? /* @__PURE__ */ react.default.createElement("details", {
		className: `${blockClass}__details`,
		open,
		ref: detailsRef
	}, /* @__PURE__ */ react.default.createElement("summary", {
		className: (0, import_classnames.default)(`${blockClass}__header`, { [`${blockClass}__header--has-toggle`]: enabled !== void 0 }),
		onClick: handleSummaryClick,
		"data-testid": "options-tile-header"
	}, enabled !== void 0 && /* @__PURE__ */ react.default.createElement("div", {
		className: `${blockClass}__toggle-container`,
		"data-testid": "options-tile-toggle-container",
		onMouseDown: (evt) => {
			evt.preventDefault();
		}
	}, /* @__PURE__ */ react.default.createElement(_carbon_react.Toggle, {
		id: `${titleId}-toggle`,
		className: `${blockClass}__toggle`,
		toggled: enabled,
		"aria-labelledby": titleId,
		hideLabel: true,
		onToggle,
		size: "sm",
		disabled: isLocked
	})), /* @__PURE__ */ react.default.createElement(_carbon_react_icons.ChevronDown, {
		size: 16,
		className: (0, import_classnames.default)(`${blockClass}__chevron`, {
			[`${blockClass}__chevron--open`]: open,
			[`${blockClass}__chevron--closing`]: closing
		})
	}), renderTitle()), /* @__PURE__ */ react.default.createElement("div", {
		className: `${blockClass}__content`,
		ref: contentRef,
		"data-testid": "options-tile-content"
	}, /* @__PURE__ */ react.default.createElement(_carbon_react.Layer, null, isLocked && /* @__PURE__ */ react.default.createElement("p", { className: `${blockClass}__locked-text` }, /* @__PURE__ */ react.default.createElement(_carbon_react_icons.Locked, { size: 16 }), lockedText), children))) : /* @__PURE__ */ react.default.createElement("div", { className: `${blockClass}__static-content` }, renderTitle()));
});
OptionsTile.displayName = componentName;
OptionsTile.propTypes = {
	/**
	* Provide content to render as expandable OptionsTile. If no children
	* are present, the OptionsTile will render as its variant.
	*/
	children: prop_types.default.node,
	/**
	* Provide an optional class to be applied to the containing node.
	*/
	className: prop_types.default.string,
	/**
	* Whether the toggle is enabled or disabled. If nothing is passed,
	* no toggle will be rendered.
	*/
	enabled: prop_types.default.bool,
	/**
	* Whether the OptionsTile is in invalid validation state.
	*/
	invalid: prop_types.default.bool,
	/**
	* Provide a text explaining why the OptionsTile is in invalid state.
	*/
	invalidText: prop_types.default.string,
	/**
	* Whether the OptionsTile is in locked validation state.
	*/
	locked: prop_types.default.bool,
	/**
	* Provide a text explaining why the OptionsTile is in locked state.
	*/
	lockedText: prop_types.default.string,
	/**
	* A handler for managing the controlled state of open prop. If not passed the open prop will not be honored and an uncontrolled state will be used.
	*/
	onChange: prop_types.default.func,
	/**
	* Provide a function which will be called each time the user
	* interacts with the toggle.
	*/
	onToggle: prop_types.default.func,
	/**
	* For controlled usage of the tile open state. This prop only works when an onChange prop is also passed, otherwise an uncontrolled state is used.
	*/
	open: prop_types.default.bool,
	/**
	* Define the size of the OptionsTile.
	*/
	size: prop_types.default.oneOf(["lg", "xl"]),
	/**
	* Optionally provide a text summarizing the current state of the content.
	*/
	summary: prop_types.default.string,
	/**
	* Provide the title for this OptionsTile. Must not contain any interactive elements.
	*/
	title: prop_types.default.oneOfType([prop_types.default.string, prop_types.default.node]).isRequired,
	/**
	* Optionally provide an id which should be used for the title.
	*/
	titleId: prop_types.default.string,
	/**
	* Whether the OptionsTile is in warning validation state.
	*/
	warn: prop_types.default.bool,
	/**
	* Provide a text explaining why the OptionsTile is in warning state.
	*/
	warnText: prop_types.default.string
};
//#endregion
exports.OptionsTile = OptionsTile;
