function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import React, { useRef } from "react";
import Image from "../image/image";
import Box from "../../atoms/box/box";
import Text from "../../atoms/text/text";
import { useAccessibleColor } from "../../../hooks/useAccessibleColor";
import { pearl } from "../../../pearl";
import { useAvatarGroup } from "./useAvatarGroup";
/**
 * A function that generates initials from a name
 * @param name The name to generate initials from
 * @returns The initials generated from the name
 */
const defaultGetInitials = name => name.split(" ").map(el => el[0]).join("").toUpperCase();
const DEFAULT_AVATAR_BACKGROUND = ["#c8e6d0ff", "#e0ecbaff", "#fbb991ff", "#f27e81ff", "#8bc7d3ff", "#bcdfd8ff", "#fae3d9ff", "#f8b4b9ff", "#c2b0d7ff", "#eeef99ff", "#a5d7c4ff", "#fef5aeff", "#f7bdd6ff", "#d6a5cbff", "#c38788ff", "#FA8072", "#FFD700", "#FFA500"];
/**
 * CustomAvatar is a component that renders an avatar image, initials or a fallback component.
 * It uses the name, src, getInitials, atoms, and fallbackComponent props from the parent Avatar component.
 * If an image source is provided, it will render the image.
 * If no image source is provided but a name is provided, it will render the initials of the name.
 * If neither image source nor name is provided, it will render the fallbackComponent.
 * If no fallbackComponent is provided, it will render nothing.
 */
const CustomAvatar = /*#__PURE__*/React.memo( /*#__PURE__*/React.forwardRef(({
  atoms
}, ref) => {
  const {
    name,
    src,
    getInitials,
    fallbackComponent,
    ...otherBoxProps
  } = atoms.box;

  // Function to pick a random color from the namedColors object
  const pickRandomColor = () => DEFAULT_AVATAR_BACKGROUND[Math.floor(Math.random() * DEFAULT_AVATAR_BACKGROUND.length)];

  // Store the random color in a ref to prevent it from changing on re-renders
  const randomColor = useRef(pickRandomColor()).current;
  // Determine the text color based on the background color to ensure accessibility
  const accessibleTextColor = useAccessibleColor(typeof otherBoxProps.backgroundColor === "string" ? otherBoxProps.backgroundColor : typeof otherBoxProps.bgColor === "string" ? otherBoxProps.bgColor : typeof otherBoxProps.style?.backgroundColor === "string" ? otherBoxProps.style?.backgroundColor : randomColor, {
    light: "neutral.50",
    dark: "neutral.900"
  });

  // Function to render the fallback component (initials or fallbackComponent prop)
  const renderFallBack = () => {
    if (name) {
      const initialComputeFunction = getInitials ?? defaultGetInitials;
      const nameInitials = initialComputeFunction(name);
      return /*#__PURE__*/React.createElement(Text, _extends({
        color: accessibleTextColor
      }, atoms.text), nameInitials);
    }
    if (fallbackComponent) return /*#__PURE__*/React.cloneElement(fallbackComponent);
    return null;
  };

  // Determine the final source of the image
  const finalSource = src && typeof src === "string" ? {
    uri: src
  } : src;

  // If a source is provided, render the image
  if (finalSource) {
    return /*#__PURE__*/React.createElement(Image, _extends({
      ref: ref,
      source: finalSource
    }, otherBoxProps));
  }

  // If no source is provided, render the fallback
  return /*#__PURE__*/React.createElement(Box, _extends({}, otherBoxProps, {
    backgroundColor: otherBoxProps.backgroundColor ?? otherBoxProps.bgColor ?? randomColor,
    justifyContent: "center",
    alignItems: "center"
  }), renderFallBack());
}));
const StyledAvatar = pearl(CustomAvatar, {
  componentName: "Avatar",
  type: "molecule",
  animatable: true
}, undefined, {
  partForOverridenStyleProps: "box",
  partForOverridenNativeProps: "box",
  partForOverridenAnimationProps: "box"
});

/** The Avatar component is used to represent a user, and displays the profile picture, initials or fallback icon. */
const Avatar = /*#__PURE__*/React.forwardRef(({
  children,
  ...rest
}, ref) => {
  let {
    size,
    variant
  } = useAvatarGroup();

  // Overwrite props from avatar group
  rest.size = size ?? rest.size;
  rest.variant = variant ?? rest.variant;

  // Render a fallback
  return /*#__PURE__*/React.createElement(StyledAvatar, _extends({}, rest, {
    ref: ref
  }), children);
});
Avatar.displayName = "Avatar";
export default Avatar;
//# sourceMappingURL=avatar.js.map