/**
 * 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 { carbon } from "../../../../settings.js";
import { CoachmarkContext, blockClass } from "./context.js";
import React, { forwardRef, useContext, useEffect, useRef } from "react";
import PropTypes from "prop-types";
import { PopoverContent } from "@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__ */ __toESM(require_classnames());
const CoachmarkContent = forwardRef((props, ref) => {
	const { className = "", children, ...rest } = props;
	const coachmarkContentBlockClass = `${blockClass}--coachmark-content`;
	const { open, setContentRef, onClose, setOpen, triggerRef, selectorPrimaryFocus } = useContext(CoachmarkContext);
	const handleRef = useRef(null);
	const contentRef = ref || handleRef;
	useEffect(() => {
		if (open && "current" in contentRef && contentRef.current) {
			const popoverContainer = contentRef.current?.closest(`.${carbon.prefix}--popover`);
			if (popoverContainer instanceof HTMLElement) setContentRef(popoverContainer);
		}
	}, [open, contentRef]);
	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
	]);
	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
	]);
	useEffect(() => {
		if (open && selectorPrimaryFocus) setTimeout(() => {
			requestAnimationFrame(() => {
				const elementToFocus = document.querySelector(selectorPrimaryFocus);
				if (elementToFocus) elementToFocus.focus();
			});
		}, 100);
	}, [open, selectorPrimaryFocus]);
	return /* @__PURE__ */ React.createElement(PopoverContent, {
		ref: contentRef,
		className: (0, import_classnames.default)(coachmarkContentBlockClass, className) || "",
		...rest
	}, children);
});
CoachmarkContent.propTypes = {
	/**
	* This is a required callback that has to return the content to render in the body section.
	* It can be a single child or an array of children depending on your need
	*/
	children: PropTypes.node,
	/**
	* Provide an optional class to be applied to the containing node.
	*/
	className: PropTypes.string
};
//#endregion
export { CoachmarkContent as default };
