import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "className", "direction", "displayMax", "inverse", "onOverflowClick", "selectable", "size", "getNamesOverflowLabel", "tooltip"];

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

import React, { cloneElement } from 'react';
import Box from '../box';
import cx from 'classnames';
import theme from './theme.css';
import AvatarStackOverflow from './AvatarStackOverflow';
var OVERLAP_SPACINGS = {
  tiny: -6,
  small: -6,
  medium: -18,
  large: -24,
  hero: -48
};
var SPACING = 6;

var AvatarStack = function AvatarStack(_ref) {
  var _cx;

  var children = _ref.children,
      className = _ref.className,
      _ref$direction = _ref.direction,
      direction = _ref$direction === void 0 ? 'horizontal' : _ref$direction,
      _ref$displayMax = _ref.displayMax,
      displayMax = _ref$displayMax === void 0 ? 99 : _ref$displayMax,
      _ref$inverse = _ref.inverse,
      inverse = _ref$inverse === void 0 ? false : _ref$inverse,
      onOverflowClick = _ref.onOverflowClick,
      _ref$selectable = _ref.selectable,
      selectable = _ref$selectable === void 0 ? false : _ref$selectable,
      _ref$size = _ref.size,
      size = _ref$size === void 0 ? 'medium' : _ref$size,
      getNamesOverflowLabel = _ref.getNamesOverflowLabel,
      _ref$tooltip = _ref.tooltip,
      tooltip = _ref$tooltip === void 0 ? false : _ref$tooltip,
      others = _objectWithoutProperties(_ref, _excluded);

  var childrenArray = React.Children.toArray(children);
  var childrenToDisplay = childrenArray.length > displayMax ? childrenArray.slice(0, displayMax) : childrenArray;
  var overflowAmount = childrenArray.length - displayMax;
  var hasOverflow = overflowAmount > 0;
  var classNames = cx(theme[direction], theme["is-".concat(size)], inverse ? [theme['light']] : [theme['dark']], (_cx = {}, _defineProperty(_cx, theme['has-overflow'], hasOverflow), _defineProperty(_cx, theme['has-overlapping-avatars'], !selectable), _cx), className);

  var spacingStyles = _objectSpread(_objectSpread({}, direction === 'horizontal' && {
    marginRight: selectable ? SPACING : OVERLAP_SPACINGS[size]
  }), direction === 'vertical' && {
    marginBottom: selectable ? SPACING : OVERLAP_SPACINGS[size]
  });

  var wrapperPaddingStyles = _objectSpread(_objectSpread({}, direction === 'horizontal' && {
    paddingRight: Math.abs(OVERLAP_SPACINGS[size])
  }), direction === 'vertical' && {
    paddingBottom: Math.abs(OVERLAP_SPACINGS[size])
  });

  return /*#__PURE__*/React.createElement(Box, _extends({}, others, {
    "data-teamleader-ui": "avatar-stack",
    className: classNames,
    alignItems: "center",
    display: "flex",
    flexDirection: direction === 'horizontal' ? 'row' : 'column',
    style: !selectable && !hasOverflow ? wrapperPaddingStyles : undefined
  }), React.Children.map(childrenToDisplay, function (child, index) {
    if (! /*#__PURE__*/React.isValidElement(child)) {
      return child;
    }

    return /*#__PURE__*/cloneElement(child, _objectSpread(_objectSpread({
      key: index
    }, child.props), {}, {
      selectable: selectable,
      size: size,
      tooltip: tooltip,
      style: _objectSpread(_objectSpread({}, spacingStyles), {}, {
        zIndex: childrenArray.length - index
      }, child.props.style)
    }));
  }), hasOverflow && /*#__PURE__*/React.createElement(AvatarStackOverflow, {
    displayMax: displayMax,
    overflowAmount: overflowAmount,
    onOverflowClick: onOverflowClick,
    overflowChildren: childrenArray.slice(displayMax),
    getNamesOverflowLabel: getNamesOverflowLabel,
    tooltip: tooltip
  }));
};

export default AvatarStack;