'use strict';

var Badge = require('../badge/Badge.js');
var BadgeAssets = require('../badge/BadgeAssets.js');
var NotificationDot = require('./NotificationDot.js');
var Circle = require('../common/circle/Circle.js');
var Image = require('../image/Image.js');
var React = require('react');
var clsx = require('clsx');
require('../common/theme.js');
require('../common/direction.js');
require('../common/propsValues/control.js');
require('../common/propsValues/breakpoint.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');
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');
var icons = require('@transferwise/icons');
require('react-intl');
require('../common/closeButton/CloseButton.messages.js');
var jsxRuntime = require('react/jsx-runtime');

function AvatarView({
  children = undefined,
  size = 48,
  selected,
  notification,
  badge,
  interactive = false,
  className,
  style,
  imgSrc,
  type,
  name,
  ...restProps
}) {
  return /*#__PURE__*/jsxRuntime.jsx(Circle, {
    size: size,
    fixedSize: true,
    className: clsx.clsx('np-avatar-view', interactive ? 'np-avatar-view-interactive' : 'np-avatar-view-non-interactive', {
      'np-avatar-view-selected': selected
    }, className),
    ...restProps,
    children: /*#__PURE__*/jsxRuntime.jsx(Badges, {
      avatar: {
        size,
        notification,
        selected
      },
      ...badge,
      children: /*#__PURE__*/jsxRuntime.jsx(Circle, {
        size: size,
        fixedSize: true,
        className: clsx.clsx('np-avatar-view-content'),
        enableBorder: !interactive || selected,
        style: {
          ...(selected && {
            '--circle-border-color': 'var(--color-interactive-primary)'
          }),
          ...style
        },
        children: children === undefined ? /*#__PURE__*/jsxRuntime.jsx(Profile, {
          imgSrc,
          type,
          name
        }) : children
      })
    })
  });
}
/** Size of badge depends on size of avatar */
const MAP_BADGE_ASSET_SIZE = {
  16: 'sm',
  24: 'sm',
  32: 'sm',
  40: 'sm',
  48: 'sm',
  56: 'lg',
  72: 'lg'
};
/** Certain sizes of AvatarView has a custom offset of Badge */
const MAP_BADGE_POSITION = {
  24: -6,
  32: -4
};
/**
 * Adds build-in badges to AvatarView
 */
function Badges({
  children,
  avatar,
  ...badge
}) {
  const {
    size: size$1 = 48,
    selected,
    notification
  } = avatar;
  const anyCustomBadge = Object.keys(badge).length > 0;
  if ((anyCustomBadge || selected) && size$1 > 16) {
    const badgeSize = MAP_BADGE_ASSET_SIZE[size$1] ?? size.Size.SMALL;
    return /*#__PURE__*/jsxRuntime.jsx(Badge, {
      size: badgeSize,
      badge: /*#__PURE__*/jsxRuntime.jsx(BadgeAssets, {
        ...(selected ? {
          status: 'positive'
        } : badge),
        size: badgeSize
      }),
      style: {
        // @ts-expect-error CSS custom props allowed
        '--badge-content-position': `${MAP_BADGE_POSITION[size$1] ?? 0}px`
      },
      children: children
    });
  }
  if (notification) {
    return /*#__PURE__*/jsxRuntime.jsx(NotificationDot, {
      avatarSize: size$1,
      children: children
    });
  }
  return children;
}
/**
 * Common logic for entities (personal & business profiles or recipients)
 */
function Profile({
  imgSrc,
  name,
  type
}) {
  const [tryLoadImage, setTryLoadImage] = React.useState(true);
  if (imgSrc && tryLoadImage) {
    return /*#__PURE__*/jsxRuntime.jsx(Image.default, {
      src: imgSrc,
      alt: "",
      onError: () => setTryLoadImage(false)
    });
  }
  if (type) {
    return type.toLowerCase() === 'business' ? /*#__PURE__*/jsxRuntime.jsx(icons.Briefcase, {}) : /*#__PURE__*/jsxRuntime.jsx(icons.Profile, {});
  }
  if (name) {
    return initials.getInitials(name);
  }
  return /*#__PURE__*/jsxRuntime.jsx(icons.Profile, {});
}

module.exports = AvatarView;
//# sourceMappingURL=AvatarView.js.map
