/**
 * 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_useIsomorphicEffect = require("../../../../global/js/hooks/useIsomorphicEffect.js");
const require_devtools = require("../../../../global/js/utils/devtools.js");
const require_context = require("./context.js");
const require_CoachmarkContent = require("./CoachmarkContent.js");
const require_ContentHeader = require("./ContentHeader.js");
const require_ContentBody = require("./ContentBody.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/Coachmark.tsx
/**
* Copyright IBM Corp. 2024, 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__ */ require_runtime.__toESM(require_index.default);
const componentName = "Coachmark";
/**
* Coachmarks are used to call out specific functionality or concepts
* within the UI that may not be intuitive but are important for the
* user to gain understanding of the product's main value and discover new use cases.
*/
const Coachmark = (0, react.forwardRef)((props, ref) => {
	const { children, className, onClose, align = "bottom", open, position = {
		x: 0,
		y: 0
	}, floating, dropShadow, highContrast, caret, selectorPrimaryFocus, triggerRef: triggerRefProp, ...rest } = props;
	const internalTriggerRef = (0, react.useRef)(null);
	const triggerRef = triggerRefProp ?? internalTriggerRef;
	const internalRef = (0, react.useRef)(null);
	const [contentRef, setContentRef] = (0, react.useState)(null);
	const [openState, setOpenState] = (0, react.useState)(false);
	const setOpen = (value) => {
		if (!value) onClose?.();
		if (open === void 0) setOpenState(value);
	};
	const currentOpen = open ?? openState;
	const caretValue = caret !== void 0 ? caret : floating === true ? false : true;
	(0, react.useEffect)(() => {
		if (triggerRefProp?.current) return;
		const container = internalRef.current;
		if (!container) return;
		const firstFocusable = Array.from(container.querySelectorAll("*")).find((el) => el.tabIndex >= 0 && !el.hasAttribute("disabled"));
		if (firstFocusable) triggerRef.current = firstFocusable;
	}, [
		children,
		triggerRef,
		triggerRefProp
	]);
	(0, react.useEffect)(() => {
		const el = triggerRef.current;
		if (el) el.setAttribute("aria-expanded", String(!!open));
	}, [open, triggerRef]);
	(0, react.useEffect)(() => {
		if (!open && contentRef && floating) {
			contentRef.style.transform = "none";
			contentRef.style.left = "0px";
			contentRef.style.top = "0px";
		}
	}, [
		open,
		contentRef,
		floating
	]);
	require_useIsomorphicEffect.useIsomorphicEffect(() => {
		const { x = 0, y = 0 } = position ?? {};
		const el = internalRef.current;
		if (el && (x !== 0 || y !== 0)) el.style.transform = `translate(${x}px, ${y}px)`;
	}, [position]);
	const setRef = (node) => {
		internalRef.current = node;
		if (typeof ref === "function") ref(node);
		else if (ref) ref.current = node;
	};
	const handleRequestClose = (event) => {
		if (floating) return;
		onClose?.();
		setOpen(false);
	};
	return /* @__PURE__ */ react.default.createElement(require_context.CoachmarkContext.Provider, { value: {
		onClose,
		open: currentOpen,
		setOpen,
		align,
		triggerRef,
		position,
		contentRef,
		setContentRef,
		floating,
		selectorPrimaryFocus
	} }, /* @__PURE__ */ react.default.createElement("div", {
		...rest,
		ref: setRef,
		className: (0, import_classnames.default)(require_context.blockClass, className, { [`${require_context.blockClass}--floating`]: floating }),
		...require_devtools.getDevtoolsProps(componentName)
	}, /* @__PURE__ */ react.default.createElement(_carbon_react.Popover, {
		open: currentOpen,
		onRequestClose: handleRequestClose,
		align,
		caret: caretValue,
		highContrast: highContrast ?? true,
		dropShadow
	}, triggerRefProp?.current ? /* @__PURE__ */ react.default.createElement("span", {
		"aria-hidden": "true",
		style: { display: "contents" },
		ref: (node) => {
			if (node && triggerRefProp.current) node.replaceWith(triggerRefProp.current);
		}
	}) : null, children)));
});
Coachmark.Content = require_CoachmarkContent.default;
Coachmark.Content.Header = require_ContentHeader.ContentHeader;
Coachmark.Content.Body = require_ContentBody.default;
Coachmark.displayName = componentName;
Coachmark.propTypes = {
	/**
	* Where to render the Coachmark relative to its target.
	*/
	align: prop_types.default.string,
	/**
	* Specify whether a caret should be rendered on the popover. This is intended to use only for coachmark patterns.
	*/
	caret: prop_types.default.bool,
	/**
	* Provide the contents of the CoachmarkV2.
	*/
	children: prop_types.default.node.isRequired,
	/**
	* Provide an optional class to be applied to the containing node.
	*/
	className: prop_types.default.string,
	/**
	* Specify whether a drop shadow should be rendered on the popover.
	*/
	dropShadow: prop_types.default.bool,
	/**
	* Specifies whether the component is floating or not.
	*/
	floating: prop_types.default.bool,
	/**
	* Specify whether the component should be rendered on high-contrast.
	*/
	highContrast: prop_types.default.bool,
	/**
	* Function to call when the close button is clicked.
	*/
	onClose: prop_types.default.func,
	/**
	* Specifies whether the component is currently open.
	*/
	open: prop_types.default.bool,
	/**
	* Fine tune the position of the target in pixels. Applies only to Beacons.
	*/
	position: prop_types.default.shape({
		x: prop_types.default.number,
		y: prop_types.default.number
	}),
	/**
	* CSS selector for the element that should receive focus when the coachmark opens.
	*/
	selectorPrimaryFocus: prop_types.default.string,
	/**
	* Optional ref for an external trigger element, used when the trigger is not part of the coachmark.
	*/
	triggerRef: prop_types.default.shape({ current: prop_types.default.any })
};
//#endregion
exports.Coachmark = Coachmark;
