/**
 * 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 { usePrefersReducedMotion } from "../../global/js/hooks/usePrefersReducedMotion.js";
import { getDevtoolsProps } from "../../global/js/utils/devtools.js";
import { COACHMARK_OVERLAY_KIND } from "../Coachmark/utils/enums.js";
import { CoachmarkContext } from "../Coachmark/utils/context.js";
import { CoachmarkOverlay } from "../Coachmark/CoachmarkOverlay.js";
import { CoachmarkTagline } from "../Coachmark/CoachmarkTagline.js";
import React, { useCallback, useEffect, useRef, useState } from "react";
import PropTypes from "prop-types";
import { createPortal } from "react-dom";

//#region src/components/CoachmarkFixed/CoachmarkFixed.tsx
/**
* Copyright IBM Corp. 2023, 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__ */ __toESM(require_classnames());
const coachmarkClass = `${pkg.prefix}--coachmark`;
const blockClass = `${coachmarkClass}-fixed`;
const overlayBlockClass = `${coachmarkClass}-overlay`;
const componentName = "CoachmarkFixed";
const defaults = {
	onClose: () => {},
	theme: "light",
	tagline: ""
};
/**
* Fixed 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.
* This variant allows the a coachmark overlay to be displayed by interacting with the tagline.
* @deprecated This component is deprecated.
*/
const CoachmarkFixed = React.forwardRef(({ children, className, onClose = defaults.onClose, portalTarget, tagline = defaults.tagline, theme = defaults.theme, closeIconDescription, ...rest }, ref) => {
	const overlayRef = useRef(null);
	const portalNode = useRef(null);
	const [isOpen, setIsOpen] = useState(false);
	const [shouldResetPosition, setShouldResetPosition] = useState(false);
	const [targetRect, setTargetRect] = useState();
	const [targetOffset, setTargetOffset] = useState({
		x: 0,
		y: 0
	});
	const [fixedIsVisible, setFixedIsVisible] = useState(false);
	const shouldReduceMotion = usePrefersReducedMotion();
	useIsomorphicEffect(() => {
		portalNode.current = portalTarget ? document?.querySelector(portalTarget) ?? document?.querySelector("body") : document?.querySelector("body");
	}, [portalTarget]);
	const handleClose = useCallback(() => {
		if (shouldReduceMotion) setIsOpen(false);
		else setFixedIsVisible(false);
	}, [shouldReduceMotion]);
	const handleTransitionEnd = (e) => {
		if (e.propertyName === "transform" && !fixedIsVisible) {
			setIsOpen(false);
			onClose();
		}
	};
	const handleTargetClick = (e) => {
		setTargetRect(e.target.getBoundingClientRect());
		setTargetOffset({
			x: e.target.offsetLeft,
			y: e.target.offsetTop
		});
		setIsOpen(false);
		setShouldResetPosition(true);
	};
	const escFunction = useCallback((event) => {
		if (event.key === "Escape") handleClose();
	}, [handleClose]);
	useEffect(() => {
		document.addEventListener("keydown", escFunction, false);
		return () => {
			document.removeEventListener("keydown", escFunction, false);
		};
	}, [escFunction]);
	const contextValue = {
		buttonProps: {
			"aria-expanded": isOpen,
			tabIndex: 0,
			onClick: handleTargetClick,
			onDoubleClick: handleTargetClick
		},
		closeButtonProps: { onClick: handleClose },
		targetRect,
		targetOffset,
		isOpen,
		closeIconDescription
	};
	useEffect(() => {
		if (shouldResetPosition) {
			setShouldResetPosition(false);
			setIsOpen(true);
		}
	}, [shouldResetPosition]);
	useEffect(() => {
		setFixedIsVisible(isOpen);
	}, [isOpen]);
	useEffect(() => {
		return () => setIsOpen(false);
	}, []);
	return /* @__PURE__ */ React.createElement(CoachmarkContext.Provider, { value: contextValue }, /* @__PURE__ */ React.createElement("div", {
		...rest,
		className: (0, import_classnames.default)(blockClass, `${blockClass}__${theme}`, className),
		ref,
		...getDevtoolsProps(componentName)
	}, /* @__PURE__ */ React.createElement(CoachmarkTagline, {
		title: tagline,
		onClose
	}), isOpen && portalNode?.current && createPortal(/* @__PURE__ */ React.createElement(CoachmarkOverlay, {
		ref: overlayRef,
		fixedIsVisible,
		kind: COACHMARK_OVERLAY_KIND.FIXED,
		onClose: handleClose,
		onTransitionEnd: handleTransitionEnd,
		theme,
		className: (0, import_classnames.default)(fixedIsVisible && `${overlayBlockClass}--is-visible`, overlayBlockClass)
	}, children), portalNode?.current)));
});
/**@ts-ignore*/
CoachmarkFixed.deprecated = {
	level: "warn",
	details: `${componentName} is deprecated.`
};
CoachmarkFixed.displayName = componentName;
CoachmarkFixed.propTypes = {
	children: PropTypes.node.isRequired,
	className: PropTypes.string,
	closeIconDescription: PropTypes.string,
	onClose: PropTypes.func,
	portalTarget: PropTypes.string,
	tagline: PropTypes.string.isRequired,
	theme: PropTypes.oneOf(["light", "dark"])
};

//#endregion
export { CoachmarkFixed };