import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import React, { useState } from 'react';
import { AvatarWrap } from "./AvatarWrap";
import redError from "../../../styles/assets/icon/redError.svg";
import { jsx as _jsx } from "react/jsx-runtime";
var Avatar = function Avatar(_ref) {
  var imgSrc = _ref.imgSrc,
    _onClick = _ref.onClick,
    _ref$fallback = _ref.fallback,
    fallback = _ref$fallback === void 0 ? redError : _ref$fallback,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? 'default' : _ref$size,
    _ref$borderless = _ref.borderless,
    borderless = _ref$borderless === void 0 ? false : _ref$borderless;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    imageLoadedError = _useState2[0],
    setImageLoadedError = _useState2[1];
  var sizeNumber = 40;
  if (typeof size === 'string') {
    if (parseInt(size, 10).toString() === size) {
      sizeNumber = parseInt(size, 10);
    } else {
      switch (size) {
        case 'default':
          sizeNumber = 40;
          break;
        case 'large':
          sizeNumber = 64;
          break;
        case 'small':
          sizeNumber = 32;
          break;
        default:
          sizeNumber = 40;
          break;
      }
    }
  } else {
    sizeNumber = size;
  }
  return /*#__PURE__*/_jsx(AvatarWrap, {
    size: sizeNumber,
    borderless: borderless,
    children: /*#__PURE__*/_jsx("div", {
      className: "avatarContainer",
      onClick: function onClick(e) {
        e.stopPropagation();
        _onClick === null || _onClick === void 0 ? void 0 : _onClick();
      },
      children: /*#__PURE__*/_jsx("img", {
        className: "avatar",
        src: imageLoadedError ? fallback : imgSrc,
        onError: function onError() {
          setImageLoadedError(true);
        }
      })
    })
  });
};
export default Avatar;