"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Avatar = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactImage = require("react-image");

var _all = require("react-icons/all");

var _clsx = _interopRequireDefault(require("clsx"));

var _jsxRuntime = require("react/jsx-runtime");

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var Avatar = function Avatar(_ref) {
  var user = _ref.user,
      size = _ref.size,
      unload = _ref.unload,
      alt = _ref.alt,
      containerProps = _ref.containerProps,
      imageProps = _ref.imageProps;
  var srcList = user === null || user === void 0 ? void 0 : user.avatar;
  var fallbackInitials = (user === null || user === void 0 ? void 0 : user.name) || "?";

  var _useImage = (0, _reactImage.useImage)({
    srcList: Array.isArray(srcList) ? srcList : [srcList],
    useSuspense: false
  }),
      src = _useImage.src,
      isLoading = _useImage.isLoading;

  var sizeClass = (0, _react.useMemo)(function () {
    switch (size) {
      case "sm":
        return "w-6 h-6 text-xs";

      case "md":
      default:
        return "w-8 h-8 text-base";
    }
  }, [size]);
  var initials = (0, _react.useMemo)(function () {
    return (fallbackInitials || "").split(" ").map(function (i) {
      return i.charAt(0).toUpperCase();
    }).slice(0, 2).join("");
  }, [fallbackInitials]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", _objectSpread(_objectSpread({}, containerProps), {}, {
    className: (0, _clsx.default)("rounded-full overflow-hidden flex justify-center items-center leading-none", sizeClass, containerProps.className, !src && initials ? "bg-dark-blue text-white" : "bg-lighter-gray"),
    children: src ? /*#__PURE__*/(0, _jsxRuntime.jsx)("img", _objectSpread(_objectSpread({
      src: src,
      alt: alt
    }, imageProps), {}, {
      className: (0, _clsx.default)("w-full h-full rounded-full overflow-hidden", imageProps.className)
    })) : initials || (!isLoading && unload ? unload : /*#__PURE__*/(0, _jsxRuntime.jsx)(_all.BsFillPersonFill, {
      className: "text-warmer-gray"
    }))
  }));
};

exports.Avatar = Avatar;
Avatar.propTypes = {
  size: _propTypes.default.string,
  user: _propTypes.default.shape({}),
  unload: _propTypes.default.node,
  alt: _propTypes.default.string,
  containerProps: _propTypes.default.shape({}),
  imageProps: _propTypes.default.shape({})
};
Avatar.defaultProps = {
  size: "md",
  unload: undefined,
  user: null,
  alt: "",
  containerProps: {},
  imageProps: {}
};