import { a as IconSize } from "./constants2.js";
import { t as Icon_default } from "./Icon.js";
import { n as AvatarSize, t as Avatar_default } from "./Avatar.js";
import { t as Badge_default } from "./Badge.js";
import { t as LazyIcon_default } from "./LazyIcon.js";
import { n as isComponentWithChildren, t as isComponent } from "./component.js";
import { n as useBreakpoint, t as Breakpoint } from "./useBreakpoint.js";
import { t as StatusDot_default } from "./StatusDot.js";
import { t as Highlighter_default } from "./Highlighter.js";
import { n as ElementHeaderText, r as ElementHeaderText_default } from "./ElementHeaderText.js";
import { n as StatusDotList, t as StatusDotList_default } from "./StatusDotList.js";
import classNames from "classnames";
import React from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import styles from "./components/ElementHeader/styles.module.scss";
const mapChildren = (children, isJsxChild = false) => {
	let avatarChild;
	const badgeChildren = [];
	const statusDotChildren = [];
	let statusDotMCChild;
	const elementHeaderTextChildren = [];
	const stringChildren = [];
	const remainingChildren = [];
	React.Children.forEach(children, (child) => {
		if (child === null || typeof child === "undefined") return;
		if (isComponent(child, Avatar_default)) avatarChild = child;
		else if (isComponent(child, ElementHeaderText)) elementHeaderTextChildren.push(child);
		else if (isComponent(child, Badge_default)) badgeChildren.push(child);
		else if (isComponent(child, StatusDotList)) statusDotMCChild = child;
		else if (isComponent(child, StatusDot_default)) statusDotChildren.push(child);
		else if (typeof child === "string") stringChildren.push(child);
		else remainingChildren.push(child);
	});
	const hasSpecialChildren = avatarChild !== void 0 || elementHeaderTextChildren.length > 0 || statusDotChildren.length > 0 || statusDotMCChild !== void 0 || badgeChildren !== void 0 && stringChildren.length > 0;
	const noRemainingRecursiveChildren = remainingChildren.length === 0 || isComponentWithChildren(remainingChildren[0]) && typeof remainingChildren[0]?.props?.children === "undefined";
	if (isJsxChild || hasSpecialChildren || noRemainingRecursiveChildren) return {
		avatarChild,
		elementHeaderTextChildren,
		badgeChildren,
		statusDotChildren,
		statusDotMCChild,
		stringChildren,
		remainingChildren
	};
	if (isComponentWithChildren(remainingChildren[0])) return mapChildren(remainingChildren[0]?.props?.children, true);
};
const renderElementHeader = (element, options) => {
	const { titleHtmlMarkup, isHovered, size, parentType, chevronIcon, icon, highlightText, closeButton } = options;
	if (isComponent(element, ElementHeader_default)) return React.cloneElement(element, {
		chevronIcon,
		icon,
		isHovered,
		size,
		parentType,
		highlightText,
		closeButton
	});
	if (element) return /* @__PURE__ */ jsx(ElementHeader_default, {
		highlightText,
		titleHtmlMarkup,
		parentType,
		chevronIcon,
		icon,
		isHovered,
		size,
		closeButton,
		children: element
	});
};
const ElementHeaderRoot = (props) => {
	const { className = "", titleHtmlMarkup = "h2", parentType = "linklist", chevronIcon, closeButton, children, icon, isHovered, size, testId, highlightText } = props;
	const breakpoint = useBreakpoint();
	const showIcon = size !== "small" && !!icon;
	const contentIsString = typeof children === "string";
	const mappedChildren = mapChildren(children);
	const hasStatusDots = !!mappedChildren?.statusDotChildren?.length || !!mappedChildren?.statusDotMCChild;
	const listLabelClasses = classNames(styles["element-header"], { [styles["element-header--compact"]]: size === "compact" }, className);
	const badgeContainerClasses = classNames(styles["element-header__badge-container"]);
	const badgeClasses = classNames(styles["element-header__badge"]);
	const statusdotContainerClasses = classNames(styles["element-header__statusdot-container"]);
	const chevronClasses = classNames(styles["element-header__chevron"]);
	const contentClasses = classNames(styles["element-header__content"], {
		[styles["element-header__content--element"]]: !contentIsString,
		[styles["element-header__content--compact"]]: size === "compact"
	});
	const iconClasses = classNames(styles["element-header__icon"], { [styles["element-header__icon--with-statusdot"]]: hasStatusDots });
	const avatarClasses = classNames(styles["element-header__avatar"], {});
	const CustomTag = titleHtmlMarkup;
	const iconPropIsIconComponent = isComponent(icon, Icon_default);
	const iconPropIsLazyIconComponent = !iconPropIsIconComponent && isComponent(icon, LazyIcon_default);
	let iconComponent;
	if (iconPropIsIconComponent) iconComponent = React.cloneElement(icon, {
		size: breakpoint < Breakpoint.md ? IconSize.XSmall : IconSize.Small,
		isHovered
	});
	else if (iconPropIsLazyIconComponent) iconComponent = React.cloneElement(icon, {
		size: breakpoint < Breakpoint.md ? IconSize.XSmall : IconSize.Small,
		isHovered
	});
	else iconComponent = icon;
	return /* @__PURE__ */ jsxs("span", {
		"data-testid": testId,
		className: listLabelClasses,
		children: [
			showIcon && icon && /* @__PURE__ */ jsx("span", {
				className: iconClasses,
				children: iconComponent
			}),
			size !== "small" && mappedChildren?.avatarChild && /* @__PURE__ */ jsx("span", {
				className: avatarClasses,
				children: React.cloneElement(mappedChildren.avatarChild, { size: AvatarSize.xsmall })
			}),
			/* @__PURE__ */ jsxs("span", {
				className: contentClasses,
				"data-hasstatusdots": hasStatusDots,
				children: [
					mappedChildren?.elementHeaderTextChildren,
					!!mappedChildren?.stringChildren.length && /* @__PURE__ */ jsx(CustomTag, {
						className: styles["element-header__title"],
						children: /* @__PURE__ */ jsx(Highlighter_default, {
							searchText: highlightText,
							children: mappedChildren.stringChildren
						})
					}),
					/* @__PURE__ */ jsx(Highlighter_default, {
						searchText: highlightText,
						children: mappedChildren?.remainingChildren
					})
				]
			}),
			hasStatusDots && /* @__PURE__ */ jsxs("span", {
				className: statusdotContainerClasses,
				children: [!!mappedChildren?.statusDotChildren && /* @__PURE__ */ jsx(StatusDotList_default, { children: mappedChildren.statusDotChildren }), !!mappedChildren?.statusDotMCChild && mappedChildren?.statusDotMCChild]
			}),
			/* @__PURE__ */ jsx("span", {
				className: badgeContainerClasses,
				children: mappedChildren?.badgeChildren && mappedChildren.badgeChildren.map((badgeChild, index) => {
					return /* @__PURE__ */ jsx("span", {
						className: badgeClasses,
						"data-parenttype": parentType,
						children: badgeChild
					}, index);
				})
			}),
			chevronIcon && typeof closeButton === "undefined" && /* @__PURE__ */ jsx("span", {
				className: chevronClasses,
				"data-parenttype": parentType,
				children: /* @__PURE__ */ jsx(Icon_default, {
					svgIcon: chevronIcon,
					isHovered,
					size: IconSize.XSmall
				})
			}),
			closeButton && /* @__PURE__ */ jsx("span", {
				className: chevronClasses,
				"data-parenttype": parentType,
				children: closeButton
			})
		]
	});
};
ElementHeaderRoot.displayName = "ElementHeader";
var ElementHeader = ElementHeaderRoot;
ElementHeader.Text = ElementHeaderText_default;
ElementHeader.Text.displayName = "ElementHeader.Text";
ElementHeader.StatusDotList = StatusDotList_default;
ElementHeader.StatusDotList.displayName = "ElementHeader.StatusDotList";
var ElementHeader_default = ElementHeader;
export { renderElementHeader as i, ElementHeader_default as n, mapChildren as r, ElementHeaderRoot as t };

//# sourceMappingURL=ElementHeader.js.map