"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _uiIcons = require("@teamleader/ui-icons");

var _classnames = _interopRequireDefault(require("classnames"));

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

var _box = _interopRequireDefault(require("../box"));

var _icon = _interopRequireDefault(require("../icon"));

var _tooltip = _interopRequireDefault(require("../tooltip"));

var _typography = require("../typography");

var _AvatarAdd = _interopRequireDefault(require("./AvatarAdd"));

var _AvatarAnonymous = _interopRequireDefault(require("./AvatarAnonymous"));

var _AvatarImage = _interopRequireDefault(require("./AvatarImage"));

var _AvatarInitials = _interopRequireDefault(require("./AvatarInitials"));

var _AvatarTeam = _interopRequireDefault(require("./AvatarTeam"));

var _theme = _interopRequireDefault(require("./theme.css"));

var _excluded = ["className", "selectable", "selected", "size", "shape", "tooltip", "fullName", "creatable", "children", "editable", "imageUrl", "id", "onImageChange", "team"];

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

var TooltippedBox = (0, _tooltip.default)(_box.default);

var AvatarInternalComponent = function AvatarInternalComponent(_ref) {
  var creatable = _ref.creatable,
      children = _ref.children,
      editable = _ref.editable,
      imageUrl = _ref.imageUrl,
      fullName = _ref.fullName,
      id = _ref.id,
      onImageChange = _ref.onImageChange,
      selected = _ref.selected,
      size = _ref.size,
      team = _ref.team;

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      failedToLoadImage = _useState2[0],
      setFailedToLoadImage = _useState2[1];

  var handleImageLoadFailure = function handleImageLoadFailure() {
    setFailedToLoadImage(false);
  };

  var childrenToRender = selected ? /*#__PURE__*/_react.default.createElement(_icon.default, {
    backgroundColor: "aqua",
    backgroundTint: "dark",
    borderColor: "aqua",
    borderTint: "dark",
    borderRadius: "circle",
    borderWidth: size === 'hero' ? 6 : 2,
    color: "neutral",
    tint: "lightest"
  }, size === 'hero' ? /*#__PURE__*/_react.default.createElement(_uiIcons.IconCloseMediumOutline, null) : /*#__PURE__*/_react.default.createElement(_uiIcons.IconCloseSmallOutline, null)) : children;

  if (team) {
    return /*#__PURE__*/_react.default.createElement(_AvatarTeam.default, {
      size: size
    });
  }

  if (creatable) {
    return /*#__PURE__*/_react.default.createElement(_AvatarAdd.default, {
      size: size
    }, children);
  }

  if (imageUrl && !failedToLoadImage) {
    return /*#__PURE__*/_react.default.createElement(_AvatarImage.default, {
      editable: editable,
      image: imageUrl,
      imageAlt: fullName,
      onImageChange: onImageChange,
      onImageLoadFailure: handleImageLoadFailure,
      size: size
    }, childrenToRender);
  }

  if (fullName) {
    return /*#__PURE__*/_react.default.createElement(_AvatarInitials.default, {
      editable: editable,
      id: id,
      name: fullName,
      onImageChange: onImageChange,
      size: size
    }, childrenToRender);
  }

  return /*#__PURE__*/_react.default.createElement(_AvatarAnonymous.default, {
    size: size
  }, childrenToRender);
};

var Avatar = function Avatar(_ref2) {
  var _cx;

  var className = _ref2.className,
      _ref2$selectable = _ref2.selectable,
      selectable = _ref2$selectable === void 0 ? false : _ref2$selectable,
      _ref2$selected = _ref2.selected,
      selected = _ref2$selected === void 0 ? false : _ref2$selected,
      _ref2$size = _ref2.size,
      size = _ref2$size === void 0 ? 'medium' : _ref2$size,
      _ref2$shape = _ref2.shape,
      shape = _ref2$shape === void 0 ? 'circle' : _ref2$shape,
      _ref2$tooltip = _ref2.tooltip,
      tooltip = _ref2$tooltip === void 0 ? false : _ref2$tooltip,
      fullName = _ref2.fullName,
      creatable = _ref2.creatable,
      children = _ref2.children,
      editable = _ref2.editable,
      imageUrl = _ref2.imageUrl,
      id = _ref2.id,
      onImageChange = _ref2.onImageChange,
      team = _ref2.team,
      others = (0, _objectWithoutProperties2.default)(_ref2, _excluded);
  var avatarClassNames = (0, _classnames.default)(_theme.default['wrapper'], _theme.default["is-".concat(size)], _theme.default["is-".concat(shape)], (_cx = {}, (0, _defineProperty2.default)(_cx, _theme.default['is-selectable'], selectable), (0, _defineProperty2.default)(_cx, _theme.default['is-selected'], selected), _cx), className);
  var enableTooltip = tooltip && typeof fullName === 'string' && fullName.length > 0;
  var Component = enableTooltip ? TooltippedBox : _box.default;
  var tooltipProps = enableTooltip ? {
    tooltip: /*#__PURE__*/_react.default.createElement(_typography.TextBodyCompact, null, fullName),
    tooltipColor: 'white',
    tooltipPosition: 'top',
    tooltipSize: 'small'
  } : {};
  return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({}, others, selectable && {
    boxSizing: 'content-box',
    padding: size === 'hero' ? 2 : 1
  }, tooltipProps, {
    className: avatarClassNames
  }), /*#__PURE__*/_react.default.createElement(AvatarInternalComponent, {
    creatable: creatable,
    editable: editable,
    imageUrl: imageUrl,
    fullName: fullName,
    id: id,
    onImageChange: onImageChange,
    selected: selected,
    size: size,
    team: team
  }, children));
};

var _default = Avatar;
exports.default = _default;