import { Briefcase, Person } from '@transferwise/icons';
import { useState, useEffect } from 'react';
import { AvatarType } from '../avatar/avatarTypes.mjs';
import Avatar from '../avatar/Avatar.mjs';
import Badge from '../badge/Badge.mjs';
import StatusIcon from '../statusIcon/StatusIcon.mjs';
import { jsx, Fragment } from 'react/jsx-runtime';
import { ProfileType } from '../common/propsValues/profileType.mjs';
import { getInitials } from '../common/initials.mjs';
import { Size } from '../common/propsValues/size.mjs';

const OptionalBadge = ({
  url,
  altText,
  statusIcon,
  children,
  ariaLabel,
  ...rest
}) => {
  if (url) {
    return /*#__PURE__*/jsx(Badge, {
      "aria-label": ariaLabel,
      badge: /*#__PURE__*/jsx("img", {
        src: url,
        alt: altText
      }),
      ...rest,
      children: children
    });
  }
  if (statusIcon) {
    return /*#__PURE__*/jsx(Badge, {
      "aria-label": ariaLabel,
      badge: /*#__PURE__*/jsx(StatusIcon, {
        sentiment: statusIcon,
        size: Size.SMALL
      }),
      ...rest,
      children: children
    });
  }
  return /*#__PURE__*/jsx(Fragment, {
    children: children
  });
};
const AvatarWrapper = ({
  url,
  'aria-label': ariaLabel,
  profileType,
  profileId,
  badgeUrl,
  badgeAltText,
  badgeStatusIcon,
  name,
  avatarProps,
  badgeProps
}) => {
  const [hasImageLoadError, setImageLoadError] = useState(false);
  const isBusinessProfile = profileType === ProfileType.BUSINESS;
  // Reset the errored state when url changes
  useEffect(() => setImageLoadError(false), [url]);
  const getAvatarProps = () => {
    let updatedAvatarProps = avatarProps;
    if (!badgeUrl && !badgeStatusIcon && ariaLabel) {
      updatedAvatarProps = {
        ...updatedAvatarProps,
        'aria-label': ariaLabel
      };
    }
    if (url && !hasImageLoadError) {
      return {
        type: AvatarType.THUMBNAIL,
        children: /*#__PURE__*/jsx("img", {
          src: url,
          alt: "",
          onError: () => setImageLoadError(true)
        }),
        ...updatedAvatarProps
      };
    }
    if (profileType) {
      return {
        type: AvatarType.ICON,
        children: isBusinessProfile ? /*#__PURE__*/jsx(Briefcase, {
          size: "24"
        }) : /*#__PURE__*/jsx(Person, {
          size: "24"
        }),
        ...updatedAvatarProps
      };
    }
    if (name) {
      return {
        type: AvatarType.INITIALS,
        children: /*#__PURE__*/jsx(Fragment, {
          children: getInitials(name)
        }),
        backgroundColorSeed: profileId?.toString(),
        ...updatedAvatarProps
      };
    }
    return {
      type: AvatarType.ICON,
      children: /*#__PURE__*/jsx(Person, {
        size: "24"
      }),
      ...updatedAvatarProps
    };
  };
  return /*#__PURE__*/jsx(OptionalBadge, {
    url: badgeUrl,
    ariaLabel: ariaLabel,
    altText: badgeAltText,
    statusIcon: badgeStatusIcon,
    ...badgeProps,
    children: /*#__PURE__*/jsx(Avatar, {
      size: Size.MEDIUM,
      ...getAvatarProps()
    })
  });
};

export { AvatarWrapper as default };
//# sourceMappingURL=AvatarWrapper.mjs.map
