"use client";
import { getRadius, getSize } from "../../core/utils/get-size/get-size.mjs";
import { createVarsResolver } from "../../core/styles-api/create-vars-resolver/create-vars-resolver.mjs";
import { useProps } from "../../core/MantineProvider/use-props/use-props.mjs";
import { useStyles } from "../../core/styles-api/use-styles/use-styles.mjs";
import { polymorphicFactory } from "../../core/factory/polymorphic-factory.mjs";
import { Box } from "../../core/Box/Box.mjs";
import Avatar_module_default from "./Avatar.module.mjs";
import { AvatarGroup, AvatarGroupContext } from "./AvatarGroup/AvatarGroup.mjs";
import { AvatarPlaceholderIcon } from "./AvatarPlaceholderIcon.mjs";
import { getInitialsColor } from "./get-initials-color/get-initials-color.mjs";
import { getInitials } from "./get-initials/get-initials.mjs";
import { use, useEffect, useState } from "react";
import { jsx } from "react/jsx-runtime";
//#region packages/@mantine/core/src/components/Avatar/Avatar.tsx
const varsResolver = createVarsResolver((theme, { size, radius, variant, gradient, color, autoContrast, name, allowedInitialsColors }) => {
	const _color = color === "initials" && typeof name === "string" ? getInitialsColor(name, allowedInitialsColors) : color;
	const colors = theme.variantColorResolver({
		color: _color || "gray",
		theme,
		gradient,
		variant: variant || "light",
		autoContrast
	});
	return { root: {
		"--avatar-size": getSize(size, "avatar-size"),
		"--avatar-radius": radius === void 0 ? void 0 : getRadius(radius),
		"--avatar-bg": _color || variant ? colors.background : void 0,
		"--avatar-color": _color || variant ? colors.color : void 0,
		"--avatar-bd": _color || variant ? colors.border : void 0
	} };
});
const Avatar = polymorphicFactory((_props) => {
	const props = useProps("Avatar", null, _props);
	const { classNames, className, style, styles, unstyled, vars, src, alt, radius, color, gradient, imageProps, children, autoContrast, mod, name, allowedInitialsColors, attributes, ...others } = props;
	const groupCtx = use(AvatarGroupContext);
	const [error, setError] = useState(!src);
	const getStyles = useStyles({
		name: "Avatar",
		props,
		classes: Avatar_module_default,
		className,
		style,
		classNames,
		styles,
		unstyled,
		attributes,
		vars,
		varsResolver
	});
	useEffect(() => setError(!src), [src]);
	return /* @__PURE__ */ jsx(Box, {
		...getStyles("root"),
		mod: [{ "within-group": groupCtx.withinGroup }, mod],
		...others,
		children: error || !src ? /* @__PURE__ */ jsx("span", {
			...getStyles("placeholder"),
			title: alt,
			children: children || typeof name === "string" && getInitials(name) || /* @__PURE__ */ jsx(AvatarPlaceholderIcon, {})
		}) : /* @__PURE__ */ jsx("img", {
			...imageProps,
			...getStyles("image"),
			src,
			alt,
			onError: (event) => {
				setError(true);
				imageProps?.onError?.(event);
			}
		})
	});
});
Avatar.classes = Avatar_module_default;
Avatar.varsResolver = varsResolver;
Avatar.displayName = "@mantine/core/Avatar";
Avatar.Group = AvatarGroup;
//#endregion
export { Avatar };

//# sourceMappingURL=Avatar.mjs.map