/**
 * 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_context = require('./context.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");

//#region src/components/Coachmark/next/Coachmark/CoachmarkContent.tsx
/**
* Copyright IBM Corp. 2024, 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.
*/
var import_classnames = /* @__PURE__ */ require_runtime.__toESM(require_index.default);
const CoachmarkContent = (0, react.forwardRef)((props, ref) => {
	const { className = "", children, ...rest } = props;
	const coachmarkContentBlockClass = `${require_context.blockClass}--coachmark-content`;
	const { open, setContentRef, onClose, setOpen, triggerRef, selectorPrimaryFocus } = (0, react.useContext)(require_context.CoachmarkContext);
	const handleRef = (0, react.useRef)(null);
	const contentRef = ref || handleRef;
	(0, react.useEffect)(() => {
		if (open && "current" in contentRef && contentRef.current) {
			const popoverContainer = contentRef.current?.closest(`.${require_settings.carbon.prefix}--popover`);
			if (popoverContainer instanceof HTMLElement) setContentRef(popoverContainer);
		}
	}, [open, contentRef]);
	(0, react.useEffect)(() => {
		const handleKeyDown = (event) => {
			if (event.key === "Escape" && open) {
				onClose?.();
				setOpen(false);
				if (triggerRef?.current) triggerRef.current.focus();
			}
		};
		if (open) document.addEventListener("keydown", handleKeyDown);
		return () => {
			document.removeEventListener("keydown", handleKeyDown);
		};
	}, [
		open,
		onClose,
		setOpen,
		triggerRef
	]);
	(0, react.useEffect)(() => {
		const handleKeyDown = (event) => {
			if (event.key === "Escape" && open) {
				onClose?.();
				setOpen(false);
			}
		};
		if (open) document.addEventListener("keydown", handleKeyDown);
		return () => {
			document.removeEventListener("keydown", handleKeyDown);
		};
	}, [
		open,
		onClose,
		setOpen
	]);
	(0, react.useEffect)(() => {
		if (open && selectorPrimaryFocus) setTimeout(() => {
			requestAnimationFrame(() => {
				const elementToFocus = document.querySelector(selectorPrimaryFocus);
				if (elementToFocus) elementToFocus.focus();
			});
		}, 100);
	}, [open, selectorPrimaryFocus]);
	return /* @__PURE__ */ react.default.createElement(_carbon_react.PopoverContent, {
		ref: contentRef,
		className: (0, import_classnames.default)(coachmarkContentBlockClass, className) || "",
		...rest
	}, children);
});
CoachmarkContent.propTypes = {
	children: prop_types.default.node,
	className: prop_types.default.string
};

//#endregion
exports.default = CoachmarkContent;