"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Avatar = exports.AvatarBadge = void 0;

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _react = _interopRequireDefault(require("react"));

var _Box = require("../Box");

var _Image = require("../Image");

var _styles = _interopRequireWildcard(require("./styles"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var __jsx = _react["default"].createElement;

var AvatarBadge = function AvatarBadge(props) {
  var avatarBadgeProps = (0, _styles.useAvatarBadgeStyle)({
    color: props.color,
    borderColor: props.borderColor
  });
  return __jsx(_Box.Box, (0, _extends2["default"])({}, avatarBadgeProps, props));
};

exports.AvatarBadge = AvatarBadge;

var getInitials = function getInitials(name) {
  var _name$split = name.split(' '),
      _name$split2 = (0, _slicedToArray2["default"])(_name$split, 2),
      firstName = _name$split2[0],
      lastName = _name$split2[1];

  if (firstName && lastName) {
    return "".concat(firstName.charAt(0)).concat(lastName.charAt(0));
  }

  return firstName.charAt(0);
};

var AvatarName = function AvatarName(_ref) {
  var name = _ref.name,
      props = (0, _objectWithoutProperties2["default"])(_ref, ["name"]);
  return __jsx(_Box.Box, (0, _extends2["default"])({
    textAlign: "center",
    textTransform: "uppercase",
    fontWeight: "medium",
    "aria-label": name
  }, props), name ? getInitials(name) : null);
};

var DefaultAvatar = function DefaultAvatar(props) {
  return __jsx(_Box.Box, (0, _extends2["default"])({
    size: "100%"
  }, props), __jsx("svg", {
    fill: "#fff",
    viewBox: "0 0 128 128",
    role: "img"
  }, __jsx("g", null, __jsx("path", {
    d: "M103,102.1388 C93.094,111.92 79.3504,118 64.1638,118 C48.8056,118 34.9294,111.768 25,101.7892 L25,95.2 C25,86.8096 31.981,80 40.6,80 L87.4,80 C96.019,80 103,86.8096 103,95.2 L103,102.1388 Z"
  }), __jsx("path", {
    d: "M63.9961647,24 C51.2938136,24 41,34.2938136 41,46.9961647 C41,59.7061864 51.2938136,70 63.9961647,70 C76.6985159,70 87,59.7061864 87,46.9961647 C87,34.2938136 76.6985159,24 63.9961647,24"
  }))));
};
/**
 * The Avatar component is used to represent user, and displays the profile
 * picture, initials or fallback icon.
 */


var Avatar = function Avatar(_ref2) {
  var _ref2$size = _ref2.size,
      size = _ref2$size === void 0 ? 'md' : _ref2$size,
      showBorder = _ref2.showBorder,
      name = _ref2.name,
      src = _ref2.src,
      borderColor = _ref2.borderColor,
      children = _ref2.children,
      rest = (0, _objectWithoutProperties2["default"])(_ref2, ["size", "showBorder", "name", "src", "borderColor", "children"]);
  var avatarStyleProps = (0, _styles["default"])({
    name: name,
    size: size,
    showBorder: showBorder,
    borderColor: borderColor
  });
  var hasLoaded = (0, _Image.useHasImageLoaded)({
    src: src
  });
  var _size = null;

  var renderChildren = function renderChildren() {
    if (src && hasLoaded) {
      return __jsx(_Box.Box, {
        as: "img",
        size: "100%",
        rounded: "full",
        objectFit: "cover",
        src: src,
        alt: name
      });
    }

    if (src && !hasLoaded) {
      if (name) {
        return __jsx(AvatarName, {
          size: _size,
          name: name
        });
      }

      return __jsx(DefaultAvatar, {
        "aria-label": name
      });
    }

    if (!src && name) {
      return __jsx(AvatarName, {
        size: _size,
        name: name
      });
    }

    return __jsx(DefaultAvatar, {
      "aria-label": name
    });
  };

  return __jsx(_Box.Box, (0, _extends2["default"])({}, avatarStyleProps, rest), renderChildren(), children);
};

exports.Avatar = Avatar;
//# sourceMappingURL=Avatar.js.map