import { Briefcase, Person } from '@transferwise/icons';
import { useState } from 'react';
import { AvatarType } from '../avatar/avatarTypes.mjs';
import Avatar from '../avatar/Avatar.mjs';
import Badge from '../badge/Badge.mjs';
import '@wise/art';
import StatusIcon from '../statusIcon/StatusIcon.mjs';
import 'clsx';
import '@transferwise/neptune-tokens';
import { jsx, Fragment } from 'react/jsx-runtime';
import '@transferwise/neptune-validation';
import '../provider/direction/DirectionProvider.mjs';
import '../common/theme.mjs';
import '../common/direction.mjs';
import '../common/propsValues/control.mjs';
import { Size } from '../common/propsValues/size.mjs';
import '../common/propsValues/typography.mjs';
import '../common/propsValues/width.mjs';
import '../common/propsValues/type.mjs';
import '../common/propsValues/dateMode.mjs';
import '../common/propsValues/monthFormat.mjs';
import '../common/propsValues/position.mjs';
import '../common/propsValues/layouts.mjs';
import '../common/propsValues/status.mjs';
import '../common/propsValues/sentiment.mjs';
import { ProfileType } from '../common/propsValues/profileType.mjs';
import '../common/propsValues/variant.mjs';
import '../common/propsValues/scroll.mjs';
import '../common/propsValues/markdownNodeType.mjs';
import '../common/fileType.mjs';
import '@transferwise/formatting';
import { getInitials } from '../common/initials.mjs';
import 'react-intl';
import '../common/closeButton/CloseButton.messages.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
  });
};
/**
 * @deprecated Use `AvatarView` component instead
 */
const AvatarWrapper = ({
  url,
  'aria-label': ariaLabel,
  profileType,
  profileId,
  badgeUrl,
  badgeAltText,
  badgeStatusIcon,
  name,
  avatarProps,
  badgeProps
}) => {
  const [errorUrl, setErrorUrl] = useState(undefined);
  const hasImageLoadError = errorUrl === url;
  const isBusinessProfile = profileType === ProfileType.BUSINESS;
  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: () => setErrorUrl(url)
        }),
        ...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
