'use strict';

exports.__esModule = true;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _emojiRegex = require('emoji-regex');

var _emojiRegex2 = _interopRequireDefault(_emojiRegex);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

require('./style.scss');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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 = _objectWithoutProperties(props, ['children', 'circular', 'className', 'hexColor', 'size', 'imgSrc']);

  var text = void 0;
  if (!imgSrc) text = _lodash2.default.isString(children) ? children.substring(0, 2) : children.props.children;

  var renderChildren = function renderChildren() {
    if (imgSrc) return _react2.default.createElement('img', { alt: '', className: 'Avatar-Img', src: imgSrc });
    return _react2.default.createElement(
      'span',
      { className: 'Avatar-Text' },
      text
    );
  };

  var isEmoji = function isEmoji() {
    if (imgSrc) return false;
    return (0, _emojiRegex2.default)().test(text);
  };

  var AvatarClasses = ['Avatar', 'Avatar_' + size, className, circular && 'Avatar_circular', isEmoji() && 'Avatar_emoji'].filter(function (e) {
    return e;
  }).join(' ');

  return _react2.default.createElement(
    'div',
    _extends({}, attrs, { className: AvatarClasses, style: { background: hexColor } }),
    renderChildren()
  );
};

Avatar.propTypes = process.env.NODE_ENV !== "production" ? {
  circular: _propTypes2.default.bool,
  className: _propTypes2.default.string,
  hexColor: _propTypes2.default.string,
  imgSrc: _propTypes2.default.string,
  size: _propTypes2.default.oneOf(['sm', 'md', 'lg'])
} : {};

Avatar.defaultProps = {
  hexColor: '#f0f0f0',
  size: 'md'
};

exports.default = Avatar;
module.exports = exports['default'];