"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _image = _interopRequireDefault(require("../image/image"));
var _box = _interopRequireDefault(require("../../atoms/box/box"));
var _text = _interopRequireDefault(require("../../atoms/text/text"));
var _useAccessibleColor = require("../../../hooks/useAccessibleColor");
var _pearl = require("../../../pearl");
var _useAvatarGroup = require("./useAvatarGroup");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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); }
/**
 * 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.default.memo( /*#__PURE__*/_react.default.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 = (0, _react.useRef)(pickRandomColor()).current;
  // Determine the text color based on the background color to ensure accessibility
  const accessibleTextColor = (0, _useAccessibleColor.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.default.createElement(_text.default, _extends({
        color: accessibleTextColor
      }, atoms.text), nameInitials);
    }
    if (fallbackComponent) return /*#__PURE__*/_react.default.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.default.createElement(_image.default, _extends({
      ref: ref,
      source: finalSource
    }, otherBoxProps));
  }

  // If no source is provided, render the fallback
  return /*#__PURE__*/_react.default.createElement(_box.default, _extends({}, otherBoxProps, {
    backgroundColor: otherBoxProps.backgroundColor ?? otherBoxProps.bgColor ?? randomColor,
    justifyContent: "center",
    alignItems: "center"
  }), renderFallBack());
}));
const StyledAvatar = (0, _pearl.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.default.forwardRef(({
  children,
  ...rest
}, ref) => {
  let {
    size,
    variant
  } = (0, _useAvatarGroup.useAvatarGroup)();

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

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