"use strict";

require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.object.define-property.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.weak-map.js");
require("core-js/modules/esnext.weak-map.delete-all.js");
require("core-js/modules/esnext.weak-map.emplace.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FcrAvatar = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
require("core-js/modules/es.array.concat.js");
require("core-js/modules/es.string.trim.js");
var _react = _interopRequireWildcard(require("react"));
require("./index.css");
var _helper = require("./helper");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var FcrAvatar = exports.FcrAvatar = function FcrAvatar(_ref) {
  var size = _ref.size,
    textSize = _ref.textSize,
    nickName = _ref.nickName,
    icon = _ref.icon,
    backgroundColor = _ref.backgroundColor,
    onClick = _ref.onClick,
    isShowFirstLetter = _ref.isShowFirstLetter,
    borderRadius = _ref.borderRadius,
    avatarImage = _ref.avatarImage;
  var name = (0, _react.useMemo)(function () {
    return nickName ? nickName.trim() : nickName;
  }, [nickName]);
  var color = backgroundColor || (0, _helper.getNameColor)(name);
  var _splitName = (0, _helper.splitName)(name),
    _splitName2 = (0, _slicedToArray2["default"])(_splitName, 2),
    first = _splitName2[0],
    last = _splitName2[1];
  var _useState = (0, _react.useState)(false),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    imgLoaded = _useState2[0],
    setImgLoaded = _useState2[1];
  (0, _react.useEffect)(function () {
    if (avatarImage) {
      var img = new Image();
      img.src = avatarImage;
      img.onload = function () {
        setImgLoaded(true);
      };
    }
  }, [avatarImage]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    onClick: onClick,
    className: "fcr-avatar",
    style: {
      width: size,
      height: size,
      background: color,
      fontSize: textSize,
      borderRadius: borderRadius || ''
    },
    children: !!icon ? icon : avatarImage && imgLoaded ? /*#__PURE__*/(0, _jsxRuntime.jsx)("img", {
      className: "fcr-avatar-img",
      src: avatarImage,
      alt: "avatar"
    }) : /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      children: isShowFirstLetter ? first : "".concat(first).concat(last)
    })
  });
};