"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

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

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

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

var _emojiRegex = _interopRequireDefault(require("emoji-regex"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _isString = _interopRequireDefault(require("lodash/isString"));

// Style

/* istanbul ignore next */
if (typeof window !== 'undefined') {}

var Avatar = function Avatar(props) {
  var children = props.children,
      circular = props.circular,
      className = props.className,
      hexColor = props.hexColor,
      size = props.size,
      imgSrc = props.imgSrc,
      attrs = (0, _objectWithoutProperties2["default"])(props, ["children", "circular", "className", "hexColor", "size", "imgSrc"]);
  var text;
  if (!imgSrc) text = (0, _isString["default"])(children) ? children.substring(0, 2) : children.props.children;

  var renderChildren = function renderChildren() {
    if (imgSrc) return /*#__PURE__*/_react["default"].createElement("img", {
      alt: "",
      className: "Avatar-Img",
      src: imgSrc
    });
    return /*#__PURE__*/_react["default"].createElement("span", {
      className: "Avatar-Text"
    }, text);
  };

  var isEmoji = function isEmoji() {
    if (imgSrc) return false;
    return (0, _emojiRegex["default"])().test(text);
  };

  var AvatarClasses = ['Avatar', "Avatar_".concat(size), className, circular && 'Avatar_circular', isEmoji() && 'Avatar_emoji'].filter(function (e) {
    return e;
  }).join(' ');
  return /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({}, attrs, {
    className: AvatarClasses,
    style: {
      background: hexColor
    }
  }), renderChildren());
};

Avatar.propTypes = {
  circular: _propTypes["default"].bool,
  className: _propTypes["default"].string,
  hexColor: _propTypes["default"].string,
  imgSrc: _propTypes["default"].string,
  size: _propTypes["default"].oneOf(['sm', 'md', 'lg'])
};
Avatar.defaultProps = {
  hexColor: '#f0f0f0',
  size: 'md'
};
var _default = Avatar;
exports["default"] = _default;