/** @license MIT | @cube-dev/ui-kit v0.142.7 | Cube Dev Team */
import { mergeProps } from "../../../utils/react/mergeProps.js";
import { TooltipProvider } from "../../overlays/Tooltip/TooltipProvider.js";
import { DANGER_CLEAR_STYLES, DANGER_OUTLINE_STYLES, DANGER_PRIMARY_STYLES, DEFAULT_CLEAR_STYLES, DEFAULT_OUTLINE_STYLES, DEFAULT_PRIMARY_STYLES, ITEM_ACTION_BASE_STYLES, SPECIAL_CLEAR_STYLES, SPECIAL_OUTLINE_STYLES, SPECIAL_PRIMARY_STYLES, SUCCESS_CLEAR_STYLES, SUCCESS_OUTLINE_STYLES, SUCCESS_PRIMARY_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 { mergeStyles, tasty } from "@tenphi/tasty";
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: mergeStyles(ITEM_ACTION_BASE_STYLES, {
		cursor: "default",
		border: { "type=primary": "#clear" }
	}),
	variants: {
		"default.primary": DEFAULT_PRIMARY_STYLES,
		"default.outline": DEFAULT_OUTLINE_STYLES,
		"default.clear": DEFAULT_CLEAR_STYLES,
		"danger.primary": DANGER_PRIMARY_STYLES,
		"danger.outline": DANGER_OUTLINE_STYLES,
		"danger.clear": DANGER_CLEAR_STYLES,
		"success.primary": SUCCESS_PRIMARY_STYLES,
		"success.outline": SUCCESS_OUTLINE_STYLES,
		"success.clear": SUCCESS_CLEAR_STYLES,
		"special.primary": SPECIAL_PRIMARY_STYLES,
		"special.outline": SPECIAL_OUTLINE_STYLES,
		"special.clear": SPECIAL_CLEAR_STYLES
	}
});
const ItemBadge = forwardRef(function ItemBadge(allProps, ref) {
	const { type: contextType, theme: contextTheme } = useItemActionContext();
	const { type = contextType ?? "clear", theme = contextTheme ?? "default", icon, children, isLoading = false, isSelected = false, tooltip, mods, ...rest } = allProps;
	const hasCheckmark = icon === "checkmark";
	const finalIcon = isLoading ? /* @__PURE__ */ jsx(LoadingIcon, {}) : hasCheckmark ? /* @__PURE__ */ jsx(CheckIcon, {}) : icon;
	const finalMods = useMemo(() => ({
		checkmark: hasCheckmark,
		selected: isSelected,
		loading: isLoading,
		"has-label": !!children,
		context: !!contextType,
		...mods
	}), [
		hasCheckmark,
		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 = 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