'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var classNames = require('clsx');
var Icon = require('@prom-ui/core/Icon');
var User = require('@prom-ui/icons/User');

function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }

function _interopNamespaceCompat(e) {
  if (e && typeof e === 'object' && 'default' in e) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);
var classNames__default = /*#__PURE__*/_interopDefaultCompat(classNames);

function _defineProperty(obj, key, value) {
  key = _toPropertyKey(key);
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }
  return obj;
}
function _extends() {
  _extends = Object.assign ? Object.assign.bind() : 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;
  };
  return _extends.apply(this, arguments);
}
function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}
function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}
function _toPrimitive(input, hint) {
  if (typeof input !== "object" || input === null) return input;
  var prim = input[Symbol.toPrimitive];
  if (prim !== undefined) {
    var res = prim.call(input, hint || "default");
    if (typeof res !== "object") return res;
    throw new TypeError("@@toPrimitive must return a primitive value.");
  }
  return (hint === "string" ? String : Number)(input);
}
function _toPropertyKey(arg) {
  var key = _toPrimitive(arg, "string");
  return typeof key === "symbol" ? key : String(key);
}

function stringToColor(string) {
  var hash = 0;
  var i;
  for (i = 0; i < string.length; i += 1) {
    hash = string.charCodeAt(i) + ((hash << 5) - hash);
  }
  var color = '#';
  for (i = 0; i < 3; i += 1) {
    var value = hash >> i * 8 & 0xff;
    color += "00".concat(value.toString(16)).slice(-2);
  }
  return color;
}
function stringAvatar(name) {
  var splitName = name.split(' ');
  return splitName.slice(0, 2).map(function (value) {
    return value[0];
  }).join('');
}

var css = {"root":"Avatar__root___ap8QD","image":"Avatar__image___52SeG","text":"Avatar__text___-Fg0p","icon":"Avatar__icon___-JCN2","theme_white":"Avatar__theme_white___VDWp7","theme_black-100":"Avatar__theme_black-100___AQ1tT","theme_violet-300":"Avatar__theme_violet-300___yK-3d","theme_yellow-500":"Avatar__theme_yellow-500___O2My9","theme_multicolor":"Avatar__theme_multicolor___xV5U3","objectFit_fill":"Avatar__objectFit_fill___MyUzF","objectFit_contain":"Avatar__objectFit_contain___Us7n4","objectFit_cover":"Avatar__objectFit_cover___fwC58","objectFit_scale-down":"Avatar__objectFit_scale-down___s0abY","objectFit_none":"Avatar__objectFit_none___uABHG"};

var _excluded = ["image", "name", "size", "loading", "theme", "objectFit", "className"];
var Avatar = /*#__PURE__*/React__namespace.forwardRef(function (_ref, ref) {
  var _classNames;
  var image = _ref.image,
    name = _ref.name,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? 40 : _ref$size,
    _ref$loading = _ref.loading,
    loading = _ref$loading === void 0 ? 'eager' : _ref$loading,
    _ref$theme = _ref.theme,
    theme = _ref$theme === void 0 ? 'violet-300' : _ref$theme,
    _ref$objectFit = _ref.objectFit,
    objectFit = _ref$objectFit === void 0 ? 'cover' : _ref$objectFit,
    className = _ref.className,
    props = _objectWithoutProperties(_ref, _excluded);
  var isImage = !!image;
  var bgColor = undefined;
  var shortName = null;
  if (!isImage) {
    var trimName = !!name ? name.trim() : '';
    if (theme === 'multicolor') {
      bgColor = stringToColor(trimName);
    }
    shortName = stringAvatar(trimName);
  }
  return /*#__PURE__*/React__namespace.createElement("div", _extends({
    className: classNames__default.default(css.root, className, (_classNames = {}, _defineProperty(_classNames, css["theme_".concat(theme)], !isImage), _defineProperty(_classNames, css["objectFit_".concat(objectFit)], true), _classNames)),
    "data-qaid": "avatar",
    "data-testid": "avatar"
  }, props, {
    style: {
      background: bgColor,
      width: size,
      height: size,
      fontSize: size / 2.2
    },
    ref: ref
  }), isImage ? /*#__PURE__*/React__namespace.createElement("img", {
    className: css.image,
    src: image,
    alt: name,
    width: size,
    height: size,
    loading: loading,
    "data-qaid": "avatar_image",
    "data-testid": "avatar_image"
  }) : /*#__PURE__*/React__namespace.createElement(React__namespace.Fragment, null, !!shortName ? /*#__PURE__*/React__namespace.createElement("span", {
    className: css.text,
    "data-qaid": "avatar_short_name",
    "data-testid": "avatar_short_name"
  }, shortName) : /*#__PURE__*/React__namespace.createElement(Icon.Icon, {
    className: css.icon,
    "icon-as": User.SvgUser,
    "data-qaid": "avatar_user_icon",
    "data-testid": "avatar_user_icon"
  })));
});
Avatar.displayName = 'Avatar';

exports.Avatar = Avatar;

require('./style.development.css');
