/** @license MIT | @cube-dev/ui-kit v0.115.0 | Cube Dev Team */
import { tasty } from "../../../tasty/tasty.js";
import { mergeProps } from "../../../utils/react/mergeProps.js";
import { TooltipProvider } from "../../overlays/Tooltip/TooltipProvider.js";
import { DANGER_CLEAR_STYLES, DANGER_NEUTRAL_STYLES, DANGER_PRIMARY_STYLES, DANGER_SECONDARY_STYLES, DEFAULT_CLEAR_STYLES, DEFAULT_NEUTRAL_STYLES, DEFAULT_PRIMARY_STYLES, DEFAULT_SECONDARY_STYLES, ITEM_ACTION_BASE_STYLES, SPECIAL_CLEAR_STYLES, SPECIAL_NEUTRAL_STYLES, SPECIAL_PRIMARY_STYLES, SPECIAL_SECONDARY_STYLES, SUCCESS_CLEAR_STYLES, SUCCESS_NEUTRAL_STYLES, SUCCESS_PRIMARY_STYLES, SUCCESS_SECONDARY_STYLES } from "../../../data/item-themes.js";
import { CheckIcon } from "../../../icons/CheckIcon.js";
import { LoadingIcon } from "../../../icons/LoadingIcon.js";
import { useItemActionContext } from "../../actions/ItemActionContext.js";
import { forwardRef, useMemo } from "react";
import { jsx, jsxs } from "react/jsx-runtime";

//#region src/components/content/ItemBadge/ItemBadge.tsx
const ItemBadgeElement = tasty({
	qa: "ItemBadge",
	styles: {
		...ITEM_ACTION_BASE_STYLES,
		cursor: "default"
	},
	variants: {
		"default.primary": DEFAULT_PRIMARY_STYLES,
		"default.secondary": DEFAULT_SECONDARY_STYLES,
		"default.neutral": DEFAULT_NEUTRAL_STYLES,
		"default.clear": DEFAULT_CLEAR_STYLES,
		"danger.primary": DANGER_PRIMARY_STYLES,
		"danger.secondary": DANGER_SECONDARY_STYLES,
		"danger.neutral": DANGER_NEUTRAL_STYLES,
		"danger.clear": DANGER_CLEAR_STYLES,
		"success.primary": SUCCESS_PRIMARY_STYLES,
		"success.secondary": SUCCESS_SECONDARY_STYLES,
		"success.neutral": SUCCESS_NEUTRAL_STYLES,
		"success.clear": SUCCESS_CLEAR_STYLES,
		"special.primary": SPECIAL_PRIMARY_STYLES,
		"special.secondary": SPECIAL_SECONDARY_STYLES,
		"special.neutral": SPECIAL_NEUTRAL_STYLES,
		"special.clear": SPECIAL_CLEAR_STYLES
	}
});
const ItemBadge = forwardRef(function ItemBadge(allProps, ref) {
	const { type: contextType, theme: contextTheme } = useItemActionContext();
	const { type = contextType ?? "neutral", theme = contextTheme ?? "default", icon, children, isLoading = false, isSelected = false, tooltip, mods, ...rest } = allProps;
	const hasCheckbox = icon === "checkbox";
	const finalIcon = isLoading ? /* @__PURE__ */ jsx(LoadingIcon, {}) : hasCheckbox ? /* @__PURE__ */ jsx(CheckIcon, {}) : icon;
	const finalMods = useMemo(() => ({
		checkbox: hasCheckbox,
		selected: isSelected,
		loading: isLoading,
		"has-label": !!children,
		context: !!contextType,
		...mods
	}), [
		hasCheckbox,
		isSelected,
		isLoading,
		children,
		contextType,
		mods
	]);
	const ariaLabel = useMemo(() => {
		if (typeof tooltip === "string") return tooltip;
		if (typeof tooltip === "object" && tooltip.title) return tooltip.title;
		return rest["aria-label"];
	}, [tooltip, rest]);
	const showTooltip = !children && tooltip;
	const tooltipContent = useMemo(() => {
		if (typeof tooltip === "string") return tooltip;
		if (typeof tooltip === "object" && tooltip.title) return tooltip.title;
	}, [tooltip]);
	const tooltipProps = useMemo(() => {
		if (typeof tooltip === "object") {
			const { title, ...rest } = tooltip;
			return rest;
		}
		return {};
	}, [tooltip]);
	const finalType = useMemo(() => {
		return theme !== "default" && type === "neutral" ? "clear" : type;
	}, [theme, type]);
	const renderBadge = (tooltipTriggerProps, tooltipRef) => {
		const handleRef = (element) => {
			if (typeof ref === "function") ref(element);
			else if (ref) ref.current = element;
			if (tooltipRef) tooltipRef.current = element;
		};
		return /* @__PURE__ */ jsxs(ItemBadgeElement, {
			ref: handleRef,
			variant: `${theme}.${finalType}`,
			"data-theme": theme,
			"data-type": finalType,
			"aria-label": ariaLabel,
			mods: finalMods,
			...mergeProps(rest, tooltipTriggerProps || {}),
			children: [finalIcon && /* @__PURE__ */ jsx("div", {
				"data-element": "Icon",
				children: finalIcon
			}), children]
		});
	};
	if (showTooltip && tooltipContent) return /* @__PURE__ */ jsx(TooltipProvider, {
		title: tooltipContent,
		...tooltipProps,
		children: (triggerProps, tooltipRef) => renderBadge(triggerProps, tooltipRef)
	});
	return renderBadge();
});

//#endregion
export { ItemBadge };
//# sourceMappingURL=ItemBadge.js.map