"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 _react = _interopRequireWildcard(require("react"));

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

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

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

var _AvatarStackOverflow = _interopRequireDefault(require("./AvatarStackOverflow"));

var _excluded = ["children", "className", "direction", "displayMax", "inverse", "onOverflowClick", "selectable", "size", "getNamesOverflowLabel", "tooltip"];

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

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) { (0, _defineProperty2.default)(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; }

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 = (0, _objectWithoutProperties2.default)(_ref, _excluded);

  var childrenArray = _react.default.Children.toArray(children);

  var childrenToDisplay = childrenArray.length > displayMax ? childrenArray.slice(0, displayMax) : childrenArray;
  var overflowAmount = childrenArray.length - displayMax;
  var hasOverflow = overflowAmount > 0;
  var classNames = (0, _classnames.default)(_theme.default[direction], _theme.default["is-".concat(size)], inverse ? [_theme.default['light']] : [_theme.default['dark']], (_cx = {}, (0, _defineProperty2.default)(_cx, _theme.default['has-overflow'], hasOverflow), (0, _defineProperty2.default)(_cx, _theme.default['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.default.createElement(_box.default, (0, _extends2.default)({}, others, {
    "data-teamleader-ui": "avatar-stack",
    className: classNames,
    alignItems: "center",
    display: "flex",
    flexDirection: direction === 'horizontal' ? 'row' : 'column',
    style: !selectable && !hasOverflow ? wrapperPaddingStyles : undefined
  }), _react.default.Children.map(childrenToDisplay, function (child, index) {
    if (! /*#__PURE__*/_react.default.isValidElement(child)) {
      return child;
    }

    return /*#__PURE__*/(0, _react.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.default.createElement(_AvatarStackOverflow.default, {
    displayMax: displayMax,
    overflowAmount: overflowAmount,
    onOverflowClick: onOverflowClick,
    overflowChildren: childrenArray.slice(displayMax),
    getNamesOverflowLabel: getNamesOverflowLabel,
    tooltip: tooltip
  }));
};

var _default = AvatarStack;
exports.default = _default;