/**
 * 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 { makeDraggable } from "../../../../global/js/utils/makeDraggable/makeDraggable.js";
import React, { forwardRef, useContext, useEffect, useRef } from "react";
import PropTypes from "prop-types";
import { Button } from "@carbon/react";
import { Close, Draggable } from "@carbon/react/icons";

//#region src/components/Coachmark/next/Coachmark/ContentHeader.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 ContentHeader = forwardRef((props, ref) => {
	const { className = "", closeIconDescription, dragIconDescription, children, ...rest } = props;
	const { setOpen, onClose, contentRef, floating } = useContext(CoachmarkContext);
	const headerRef = useRef(null);
	const dragRef = useRef(null);
	const handleRef = ref || headerRef;
	const contentHeaderBlockClass = `${blockClass}--content-header`;
	const closeBubble = (e) => {
		e?.stopPropagation();
		onClose?.();
		setOpen(false);
	};
	useEffect(() => {
		if (floating && contentRef && typeof handleRef === "object" && handleRef !== null && "current" in handleRef && handleRef.current && dragRef.current) {
			const dragStyleContainer = contentRef.querySelector(`.${carbon.prefix}--popover-content`);
			contentRef.style.transform = "none";
			contentRef.style.left = "0px";
			contentRef.style.top = "0px";
			const draggable = makeDraggable({
				el: contentRef,
				dragHandle: handleRef.current,
				focusableDragHandle: dragRef.current
			});
			const onDragStart = () => {
				if (dragStyleContainer) {
					dragStyleContainer.classList.add(`${contentHeaderBlockClass}--is-dragging`);
					dragStyleContainer.setAttribute("aria-label", "Coachmark is being dragged");
				}
			};
			const onDragEnd = () => {
				if (dragStyleContainer) {
					dragStyleContainer.classList.remove(`${contentHeaderBlockClass}--is-dragging`);
					dragStyleContainer.removeAttribute("aria-label");
				}
			};
			contentRef.addEventListener("dragstart", onDragStart);
			contentRef.addEventListener("dragend", onDragEnd);
			return () => {
				contentRef.removeEventListener("dragstart", onDragStart);
				contentRef.removeEventListener("dragend", onDragEnd);
				draggable.cleanup();
			};
		}
	}, [
		floating,
		contentRef,
		handleRef,
		dragRef,
		contentHeaderBlockClass
	]);
	return /* @__PURE__ */ React.createElement("div", {
		ref: handleRef,
		className: (0, import_classnames.default)(contentHeaderBlockClass, className),
		...rest
	}, floating && /* @__PURE__ */ React.createElement(Button, {
		kind: "ghost",
		size: "sm",
		ref: dragRef,
		renderIcon: Draggable,
		iconDescription: dragIconDescription,
		hasIconOnly: true,
		className: `${contentHeaderBlockClass}--drag-icon`
	}), children, /* @__PURE__ */ React.createElement(Button, {
		kind: "ghost",
		size: "sm",
		renderIcon: Close,
		iconDescription: closeIconDescription,
		hasIconOnly: true,
		onClick: closeBubble
	}));
});
ContentHeader.propTypes = {
	children: PropTypes.node,
	className: PropTypes.string,
	closeIconDescription: PropTypes.string,
	dragIconDescription: PropTypes.string
};

//#endregion
export { ContentHeader };