'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var icons = require('@transferwise/icons');
var React = require('react');
var avatarTypes = require('../avatar/avatarTypes.js');
var Avatar = require('../avatar/Avatar.js');
var Badge = require('../badge/Badge.js');
require('@wise/art');
var StatusIcon = require('../statusIcon/StatusIcon.js');
require('clsx');
require('../common/propsValues/breakpoint.js');
var jsxRuntime = require('react/jsx-runtime');
require('@transferwise/neptune-validation');
require('../provider/direction/DirectionProvider.js');
require('../common/theme.js');
require('../common/direction.js');
require('../common/propsValues/control.js');
var size = require('../common/propsValues/size.js');
require('../common/propsValues/typography.js');
require('../common/propsValues/width.js');
require('../common/propsValues/type.js');
require('../common/propsValues/dateMode.js');
require('../common/propsValues/monthFormat.js');
require('../common/propsValues/position.js');
require('../common/propsValues/layouts.js');
require('../common/propsValues/status.js');
require('../common/propsValues/sentiment.js');
var profileType = require('../common/propsValues/profileType.js');
require('../common/propsValues/variant.js');
require('../common/propsValues/scroll.js');
require('../common/propsValues/markdownNodeType.js');
require('../common/fileType.js');
var initials = require('../common/initials.js');
require('react-intl');
require('../common/closeButton/CloseButton.messages.js');

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

exports.default = AvatarWrapper;
//# sourceMappingURL=AvatarWrapper.js.map
