"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Avatar = void 0;
var _react = _interopRequireDefault(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _tooltip = require("../tooltip");
var _useTranslation = require("../hooks/use-translation");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const getInitials = callerName => {
  const names = callerName.split(' ');
  let initials = names[0].substring(0, 1);
  if (names.length > 1) {
    initials += names[names.length - 1].substring(0, 1);
  }
  return initials;
};
/** Renders the placeholder for users avatar.
 * @uxr-only-supported
 * @midnight-gray-supported
 * @alpine-light-supported
 */
const Avatar = ({
  children,
  callerName,
  className = '',
  imageSrc = '',
  imageAlt,
  isTooltipHidden = false,
  tooltipText = '',
  testId = 'avatar'
}) => {
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const avatarAlt = imageAlt ?? t('avatar image');
  const initials = callerName ? getInitials(callerName) : null;
  const tooltipContent = tooltipText || callerName;

  // With Image
  if (imageSrc) {
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)("figure", {
      className: "avatar",
      children: [!isTooltipHidden && /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.Tooltip, {
        content: tooltipContent,
        placement: _tooltip.TOOLTIP_PLACEMENT_TYPES.TOP,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("img", {
          className: (0, _classnames.default)('avatar-container', className),
          "test-id": testId,
          src: imageSrc,
          alt: avatarAlt
        })
      }), isTooltipHidden && /*#__PURE__*/(0, _jsxRuntime.jsx)("img", {
        className: (0, _classnames.default)('avatar-container', className),
        "test-id": testId,
        src: imageSrc,
        alt: avatarAlt
      }), children && /*#__PURE__*/(0, _jsxRuntime.jsx)("figcaption", {
        className: "avatar-name",
        "test-id": "avatar-name",
        children: children
      })]
    });
  }

  // With Initials
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("figure", {
    className: "avatar",
    children: [callerName && !isTooltipHidden && /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.Tooltip, {
      content: tooltipContent,
      placement: _tooltip.TOOLTIP_PLACEMENT_TYPES.TOP,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: (0, _classnames.default)('avatar-container', className),
        "test-id": testId,
        children: initials
      })
    }), callerName && isTooltipHidden && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames.default)('avatar-container uppercased', className),
      "test-id": testId,
      children: initials
    }), children && /*#__PURE__*/(0, _jsxRuntime.jsx)("figcaption", {
      className: "avatar-name",
      "test-id": "avatar-name",
      children: children
    })]
  });
};
exports.Avatar = Avatar;