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; };

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; }

import React from 'react';
import EmojiRegex from 'emoji-regex';
import PropTypes from 'prop-types';
import _ from 'lodash';

import './style.scss';

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 = _.isString(children) ? children.substring(0, 2) : children.props.children;

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

  var isEmoji = function isEmoji() {
    if (imgSrc) return false;
    return EmojiRegex().test(text);
  };

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

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

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

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

export default Avatar;