/**
 * 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 { useIsomorphicEffect } from "../../global/js/hooks/useIsomorphicEffect.js";
import { getDevtoolsProps } from "../../global/js/utils/devtools.js";
import { COACHMARK_OVERLAY_KIND } from "./utils/enums.js";
import { useClickOutsideElement, useWindowEvent } from "./utils/hooks.js";
import { CoachmarkContext } from "./utils/context.js";
import { CoachmarkOverlay } from "./CoachmarkOverlay.js";
import { throttle } from "../../global/js/utils/throttle.js";
import React, { forwardRef, useCallback, useEffect, useRef, useState } from "react";
import PropTypes from "prop-types";
import { Popover, PopoverContent } from "@carbon/react";
import { createPortal } from "react-dom";

//#region src/components/Coachmark/Coachmark.tsx
/**
* Copyright IBM Corp. 2023, 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}--coachmark`;
const overlayBlockClass = `${blockClass}-overlay`;
const componentName = "Coachmark";
const defaults = {
	align: "bottom",
	onClose: () => {},
	overlayKind: "tooltip",
	theme: "light",
	isOpenByDefault: false,
	closeIconDescription: ""
};
/**
* 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.
* @deprecated This component is deprecated.
*/
const Coachmark = forwardRef(({ align = defaults.align, autoAlign, children, className, onClose = defaults.onClose, overlayClassName, overlayKind = defaults.overlayKind, overlayRef, positionTune, portalTarget, target, theme = defaults.theme, isOpenByDefault = defaults.isOpenByDefault, closeIconDescription = defaults.closeIconDescription, ...rest }, ref) => {
	const isBeacon = overlayKind === COACHMARK_OVERLAY_KIND.TOOLTIP;
	const isStacked = overlayKind === COACHMARK_OVERLAY_KIND.STACKED;
	const [isOpen, setIsOpen] = useState(isStacked || isOpenByDefault);
	const [shouldResetPosition, setShouldResetPosition] = useState(false);
	const [targetRect, setTargetRect] = useState();
	const [targetOffset, setTargetOffset] = useState({
		x: 0,
		y: 0
	});
	const overlayBackupRef = useRef(void 0);
	const backupRef = useRef(void 0);
	const _coachmarkRef = ref || backupRef;
	const _overlayRef = overlayRef || overlayBackupRef;
	const portalNode = useRef(null);
	const popoverRef = useRef(null);
	let targetName;
	if (React.isValidElement(target) && typeof target.type !== "string") targetName = target.type;
	useIsomorphicEffect(() => {
		portalNode.current = portalTarget ? document?.querySelector(portalTarget) ?? document?.querySelector("body") : document?.querySelector("body");
	}, [portalTarget]);
	const closeOverlay = () => {
		setIsOpen(false);
	};
	const handleClose = useCallback(() => {
		if (isStacked) onClose();
		else {
			setIsOpen(false);
			onClose();
		}
	}, [isStacked, onClose]);
	const escFunction = useCallback((event) => {
		if (event.key === "Escape") handleClose();
	}, [handleClose]);
	useEffect(() => {
		document.addEventListener("keydown", escFunction, false);
		return () => {
			document.removeEventListener("keydown", escFunction, false);
		};
	}, [escFunction]);
	const handleTargetClick = (e) => {
		setTargetRect(e.target.getBoundingClientRect());
		setTargetOffset({
			x: e.target.offsetLeft,
			y: e.target.offsetTop
		});
		if (isBeacon) setIsOpen((prevIsOpen) => !prevIsOpen);
		else {
			setIsOpen(false);
			setShouldResetPosition(true);
		}
	};
	const contextValue = {
		buttonProps: {
			"aria-expanded": isOpen,
			tabIndex: 0,
			onClick: handleTargetClick,
			onDoubleClick: handleTargetClick
		},
		closeButtonProps: { onClick: handleClose },
		targetRect,
		targetOffset,
		align,
		positionTune,
		isOpen,
		closeIconDescription
	};
	const handleResize = throttle(() => {
		closeOverlay();
	}, 2e3);
	useEffect(() => {
		if (shouldResetPosition) {
			setShouldResetPosition(false);
			setIsOpen(true);
		}
	}, [shouldResetPosition]);
	useIsomorphicEffect(() => {
		const overlayPositionStyle = {
			top: `${(positionTune?.y ?? 0) - 16}px`,
			left: `${(positionTune?.x ?? 0) - 16}px`
		};
		if (popoverRef.current && popoverRef.current.style && overlayPositionStyle) {
			const combinedStyle = {
				position: "absolute",
				...overlayPositionStyle
			};
			Object.assign(popoverRef.current.style, combinedStyle);
		}
	}, [popoverRef, positionTune]);
	useEffect(() => {
		return () => setIsOpen(false);
	}, []);
	useClickOutsideElement(_coachmarkRef, _overlayRef, overlayKind, closeOverlay);
	useWindowEvent("resize", handleResize);
	return /* @__PURE__ */ React.createElement(CoachmarkContext.Provider, { value: contextValue }, /* @__PURE__ */ React.createElement("div", {
		className: (0, import_classnames.default)(blockClass, `${blockClass}__${theme}`, className),
		ref: _coachmarkRef,
		...rest,
		...getDevtoolsProps(componentName)
	}, overlayKind !== "tooltip" ? /* @__PURE__ */ React.createElement(React.Fragment, null, targetName?.displayName === "CoachmarkBeacon" ? React.cloneElement(target, { buttonProps: contextValue.buttonProps }) : target, isOpen && portalNode?.current && createPortal(/* @__PURE__ */ React.createElement(CoachmarkOverlay, {
		ref: _overlayRef,
		fixedIsVisible: false,
		kind: overlayKind,
		onClose: handleClose,
		theme,
		className: (0, import_classnames.default)(overlayClassName, `${overlayBlockClass}--is-visible`)
	}, children), portalNode?.current)) : /* @__PURE__ */ React.createElement(Popover, {
		highContrast: true,
		caret: true,
		ref: popoverRef,
		align,
		autoAlign,
		open: isOpen
	}, targetName?.displayName === "CoachmarkBeacon" ? React.cloneElement(target, { buttonProps: contextValue.buttonProps }) : target, /* @__PURE__ */ React.createElement(PopoverContent, null, isOpen && /* @__PURE__ */ React.createElement(CoachmarkOverlay, {
		ref: _overlayRef,
		fixedIsVisible: false,
		kind: overlayKind,
		onClose: handleClose,
		theme,
		className: (0, import_classnames.default)(overlayClassName, { [`${overlayBlockClass}--is-visible`]: isOpen })
	}, children)))));
});
const overlayRefType = typeof HTMLElement === "undefined" ? PropTypes.object : PropTypes.instanceOf(HTMLElement);
/**@ts-ignore*/
Coachmark.deprecated = {
	level: "warn",
	details: `${componentName} is deprecated.`
};
Coachmark.displayName = componentName;
const deprecatedProps = {
	overlayClassName: PropTypes.string,
	overlayKind: PropTypes.oneOf([
		"tooltip",
		"floating",
		"stacked"
	]),
	overlayRef: PropTypes.shape({ current: overlayRefType }),
	portalTarget: PropTypes.string,
	target: PropTypes.node,
	theme: PropTypes.oneOf(["light", "dark"])
};
Coachmark.propTypes = {
	align: PropTypes.oneOf([
		"bottom",
		"bottom-left",
		"bottom-right",
		"left",
		"left-top",
		"left-bottom",
		"right",
		"right-top",
		"right-bottom",
		"top",
		"top-left",
		"top-right"
	]),
	autoAlign: PropTypes.bool,
	children: PropTypes.node.isRequired,
	className: PropTypes.string,
	closeIconDescription: PropTypes.string,
	isOpenByDefault: PropTypes.bool,
	onClose: PropTypes.func,
	positionTune: PropTypes.shape({
		x: PropTypes.number,
		y: PropTypes.number
	}),
	...deprecatedProps
};

//#endregion
export { Coachmark };