"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AccountBadgeStack = void 0;
var _react = _interopRequireDefault(require("react"));
var _styledComponents = _interopRequireWildcard(require("styled-components"));
var _ = _interopRequireDefault(require("../.."));
var _AccountBadge = require("../AccountBadge/AccountBadge.constants");
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 }; }
var BORDER_WIDTH = 2;
var CROSS_FADE = 1;
var CircleWrapper = _styledComponents.default.div.withConfig({
  displayName: "AccountBadgeStack__CircleWrapper",
  componentId: "sc-cut51o-0"
})(["border-radius:50%;display:inline-flex;position:relative;", ""], function (_ref) {
  var theme = _ref.theme,
    $size = _ref.$size;
  var diameter = theme.spacing.unit($size);
  var radius = diameter / 2;
  return (0, _styledComponents.css)(["&:not(:last-of-type){mask:radial-gradient( circle at ", "px,transparent ", "px,white ", "px );}&:not(:first-of-type){margin-left:-", "px;}"], diameter, radius + BORDER_WIDTH / 2, radius + BORDER_WIDTH / 2 + CROSS_FADE, radius - BORDER_WIDTH / 2);
});
var StyledContainer = _styledComponents.default.div.withConfig({
  displayName: "AccountBadgeStack__StyledContainer",
  componentId: "sc-cut51o-1"
})(["white-space:nowrap;overflow:hidden;display:flex;"]);
var currentColorFn = function currentColorFn() {
  return 'currentColor';
};
var AccountBadgeStack = exports.AccountBadgeStack = _react.default.forwardRef(function (_ref2, ref) {
  var className = _ref2.className,
    items = _ref2.items,
    _ref2$badgeSize = _ref2.badgeSize,
    badgeSize = _ref2$badgeSize === void 0 ? 's' : _ref2$badgeSize,
    _ref2$maxElementsInSt = _ref2.maxElementsInStack,
    maxElementsInStack = _ref2$maxElementsInSt === void 0 ? 3 : _ref2$maxElementsInSt,
    _ref2$truncated = _ref2.truncated,
    truncated = _ref2$truncated === void 0 ? true : _ref2$truncated,
    truncationBadgeColor = _ref2.truncationBadgeColor,
    truncationWrapper = _ref2.truncationWrapper,
    useCurrentColor = _ref2.useCurrentColor;
  var isTruncated = truncated && maxElementsInStack;
  var size = typeof badgeSize === 'number' ? badgeSize : _AccountBadge.AccountBadgeSize[badgeSize];
  var exceedsMax = isTruncated ? items.length >= maxElementsInStack : false;
  var passThroughIndex = isTruncated ? maxElementsInStack - 1 : items.length;
  var amountOfTruncatedBadges = isTruncated ? items.length - maxElementsInStack + 1 : 0;
  var truncatedItems = items.slice(passThroughIndex);
  return _react.default.createElement(StyledContainer, {
    className: className,
    ref: ref
  }, items.slice(0, passThroughIndex).map(function (passedItem, index) {
    var key = "".concat(passedItem.label, "_").concat(index);
    return _react.default.createElement(CircleWrapper, {
      $size: size,
      key: key
    }, _react.default.createElement(_.default.Account, {
      badgeSize: badgeSize,
      badgeColor: useCurrentColor ? currentColorFn : passedItem.badgeColor
    }, passedItem.label));
  }), exceedsMax ? _react.default.createElement(CircleWrapper, {
    $size: size
  }, _react.default.createElement(_.default.Account, {
    badgeSize: badgeSize,
    badgeColor: useCurrentColor ? currentColorFn : truncationBadgeColor
  }, typeof truncationWrapper === 'function' ? truncationWrapper({
    truncatedItems: truncatedItems
  }) : "+".concat(amountOfTruncatedBadges))) : null);
});