"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AvatarGroup = void 0;
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _react = _interopRequireWildcard(require("react"));
var _Styles = require("./Styles");
var _Tooltip = require("../Tooltip");
var _Typography = require("../Typography/Typography");
var _excluded = ["limit", "size", "children", "hasTooltip"];
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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 _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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 _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; }
var AvatarGroup = exports.AvatarGroup = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var _Children$toArray;
  var limit = _ref.limit,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? 34 : _ref$size,
    children = _ref.children,
    _ref$hasTooltip = _ref.hasTooltip,
    hasTooltip = _ref$hasTooltip === void 0 ? false : _ref$hasTooltip,
    rest = _objectWithoutProperties(_ref, _excluded);
  // always display counter with min +2 as +1 can takes space of an 1 more Avatar
  var minChildren = _react.Children.count(children) - limit > 1 ? false : true;
  // gap between Avatars
  var gap = 8;
  var _children = minChildren ? _react.Children.toArray(children) : _react.Children.toArray(children).slice(0, limit);
  var handleVariant = (0, _react.useMemo)(function () {
    if (size >= 34) return "Caption 1";
    return "Caption 2";
  }, [size]);

  // get all names indexed above limit number from alt tag on Avatar for counter tooltip
  var nameArr = (_Children$toArray = _react.Children.toArray(children)) === null || _Children$toArray === void 0 ? void 0 : _Children$toArray.slice(limit).map(function (item) {
    var _ref2 = item,
      props = _ref2.props;
    return props.alt;
  });
  return /*#__PURE__*/_react.default.createElement(_Styles.AvatarGroupsStyles, _extends({
    hasTooltip: hasTooltip,
    ref: ref
  }, rest), _children.map(function (child, index) {
    var item = child;
    var tooltipText = item.props.alt;
    var alt = "Image of ".concat(tooltipText);
    return /*#__PURE__*/_react.default.createElement(_Tooltip.Tooltip, {
      title: tooltipText ? tooltipText : "",
      disable: !hasTooltip,
      key: alt
    }, /*#__PURE__*/(0, _react.cloneElement)(item, _objectSpread(_objectSpread({}, item.props), {}, {
      size: size,
      alt: alt,
      style: {
        // component preserves it's width
        marginLeft: "-".concat(index === 0 ? null : gap, "px"),
        outline: "1px solid var(--page-paper-main)"
      }
    })));
  }), limit >= _react.Children.count(children) || minChildren ? null : /*#__PURE__*/_react.default.createElement(_StyledTooltip, {
    title: nameArr.join(",\n"),
    disable: !hasTooltip
  }, /*#__PURE__*/_react.default.createElement(_StyledLengthCountStyles, {
    size: size,
    role: "status",
    "aria-label": "Rest of the users",
    $_css: "-".concat(gap, "px")
  }, /*#__PURE__*/_react.default.createElement(_Typography.Typography, {
    variant: handleVariant,
    weight: "medium"
  }, "+", _react.Children.count(children) - limit))));
});
AvatarGroup.displayName = "AvatarGroup";
var _StyledTooltip = (0, _styledComponents.default)(_Tooltip.Tooltip).withConfig({
  displayName: "AvatarGroup___StyledTooltip",
  componentId: "sc-1rsx1j5-0"
})({
  whiteSpace: "break-spaces",
  lineHeight: "18px"
});
var _StyledLengthCountStyles = (0, _styledComponents.default)(_Styles.LengthCountStyles).withConfig({
  displayName: "AvatarGroup___StyledLengthCountStyles",
  componentId: "sc-1rsx1j5-1"
})(function (p) {
  return {
    marginLeft: p.$_css
  };
});
//# sourceMappingURL=AvatarGroup.js.map