import React from 'react';
import styled, { css } from 'styled-components';
import Badge from '../..';
import { AccountBadgeSize } from '../AccountBadge/AccountBadge.constants';
var BORDER_WIDTH = 2;
var CROSS_FADE = 1;
var CircleWrapper = styled.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 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 = styled.div.withConfig({
  displayName: "AccountBadgeStack__StyledContainer",
  componentId: "sc-cut51o-1"
})(["white-space:nowrap;overflow:hidden;display:flex;"]);
var currentColorFn = function currentColorFn() {
  return 'currentColor';
};
export var AccountBadgeStack = React.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 : 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.createElement(StyledContainer, {
    className: className,
    ref: ref
  }, items.slice(0, passThroughIndex).map(function (passedItem, index) {
    var key = "".concat(passedItem.label, "_").concat(index);
    return React.createElement(CircleWrapper, {
      $size: size,
      key: key
    }, React.createElement(Badge.Account, {
      badgeSize: badgeSize,
      badgeColor: useCurrentColor ? currentColorFn : passedItem.badgeColor
    }, passedItem.label));
  }), exceedsMax ? React.createElement(CircleWrapper, {
    $size: size
  }, React.createElement(Badge.Account, {
    badgeSize: badgeSize,
    badgeColor: useCurrentColor ? currentColorFn : truncationBadgeColor
  }, typeof truncationWrapper === 'function' ? truncationWrapper({
    truncatedItems: truncatedItems
  }) : "+".concat(amountOfTruncatedBadges))) : null);
});