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); }
var _excluded = ["imageUrl", "initials", "type", "size"],
  _excluded2 = ["items", "title", "size", "getInitials", "getImageUrl", "onClickViewAll"];
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
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(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : 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 _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import { Building, People } from '@procore/core-icons';
import { useId } from '@react-aria/utils';
import React, { forwardRef, useMemo, useState } from 'react';
import { Avatar } from '../Avatar';
import { Button } from '../Button';
import { ContactItem } from '../ContactItem';
import { Link } from '../Link';
import { Modal } from '../Modal';
import { Popover } from '../Popover';
import { Typography } from '../Typography';
import { useI18nContext } from '../_hooks/I18n';
import { colorsOrder, foldedItemsCap, restCountThreshold, visibleItemsCap } from './AvatarStack.constants';
import { StyledAvatar, StyledContactItem, StyledContactItems, StyledModalBody, StyledViewAllWrapper, StyledWrapper } from './AvatarStack.styles';
var defaultPopoverTrigger = ['hover', 'focus'];
export function getOverflowValues(items) {
  var foldedItems = [];
  var restCountLabel = null;
  var isViewAllNeeded = false;
  var visibleItems = items.length > visibleItemsCap ? items.slice(0, visibleItemsCap - 1) : items;
  var restItemsCount = items.length - visibleItems.length;
  if (restItemsCount > 0) {
    foldedItems = items.slice(visibleItems.length, visibleItems.length + foldedItemsCap);
    restCountLabel = restItemsCount > restCountThreshold ? "".concat(restCountThreshold, "+") : "+".concat(restItemsCount);
    isViewAllNeeded = restItemsCount > foldedItemsCap;
  }
  return {
    visibleItems: visibleItems,
    foldedItems: foldedItems,
    restCountLabel: restCountLabel,
    isViewAllNeeded: isViewAllNeeded
  };
}
export function getIcon(type, size) {
  switch (type) {
    case 'company':
      return /*#__PURE__*/React.createElement(Building, {
        size: size
      });
    case 'group':
      return /*#__PURE__*/React.createElement(People, {
        size: size
      });
    case 'user':
    default:
      return null;
  }
}
function getContactIcon(type) {
  return getIcon(type, 'md');
}
export function getAvatarIcon(type, size) {
  var avatarSize = size === 'lg' ? 'md' : 'sm';
  return getIcon(type, avatarSize);
}
export function getColorOrder(avatarItems) {
  return avatarItems.reduce(function (_ref, item) {
    var map = _ref.map,
      color = _ref.color;
    if (item.imageUrl) {
      return {
        map: map,
        color: color
      };
    }
    if (item.inactive) {
      map.set(item.id, 'gray70');
      return {
        map: map,
        color: color
      };
    }
    map.set(item.id, color);
    var currentColorIndex = colorsOrder.indexOf(color);
    var nextColorIndex = (currentColorIndex + 1) % colorsOrder.length;
    return {
      map: map,
      color: item.imageUrl ? color : colorsOrder[nextColorIndex]
    };
  }, {
    map: new Map(),
    color: colorsOrder[0]
  }).map;
}
export function AvatarStackContactItem(_ref2) {
  var id = _ref2.id,
    type = _ref2.type,
    imageUrl = _ref2.imageUrl,
    initials = _ref2.initials,
    inactive = _ref2.inactive,
    name = _ref2.name,
    linkUrl = _ref2.linkUrl,
    description = _ref2.description;
  return /*#__PURE__*/React.createElement(StyledContactItem, {
    disabled: inactive,
    icon: getContactIcon(type),
    imageUrl: imageUrl,
    initials: initials,
    key: id
  }, /*#__PURE__*/React.createElement(ContactItem.Title, null, !inactive && linkUrl ? /*#__PURE__*/React.createElement(Link, {
    href: linkUrl
  }, name) : name), /*#__PURE__*/React.createElement(ContactItem.Description, null, type === 'group' ? /*#__PURE__*/React.createElement(Typography, {
    color: "gray15"
  }, description) : description));
}
export function AvatarContent(_ref3) {
  var imageUrl = _ref3.imageUrl,
    initials = _ref3.initials,
    type = _ref3.type,
    size = _ref3.size,
    props = _objectWithoutProperties(_ref3, _excluded);
  var avatarIcon = getAvatarIcon(type, size);
  if (imageUrl) {
    return /*#__PURE__*/React.createElement(Avatar.Portrait, _extends({}, props, {
      imageUrl: imageUrl
    }));
  }
  if (avatarIcon) {
    return /*#__PURE__*/React.createElement(Avatar.Icon, _extends({}, props, {
      icon: avatarIcon
    }));
  }
  if (initials) {
    return /*#__PURE__*/React.createElement(Avatar.Label, props, initials);
  }
  return null;
}
function AvatarWithPopover(_ref4) {
  var item = _ref4.item,
    size = _ref4.size,
    colors = _ref4.colors;
  var hasEnabledLink = item.linkUrl && !item.inactive;
  var avatarProps = hasEnabledLink ? {
    role: 'link',
    href: item.linkUrl
  } : {
    role: 'img',
    // Allow SR to open Popover as it cycles through. Keeps visual with audio.
    // The link or image is labelled, and focus does not enter the Popover.
    tabIndex: -1
  };
  return /*#__PURE__*/React.createElement(Popover, {
    key: item.id,
    trigger: defaultPopoverTrigger,
    overlay: /*#__PURE__*/React.createElement(Popover.Content, null, /*#__PURE__*/React.createElement(AvatarStackContactItem, {
      id: item.id,
      type: item.type,
      imageUrl: item.imageUrl,
      initials: item.initials,
      inactive: item.inactive,
      name: item.name,
      linkUrl: item.linkUrl,
      description: item.description
    }))
  }, /*#__PURE__*/React.createElement(StyledAvatar, _extends({}, avatarProps, {
    $color: colors.get(item.id),
    "aria-label": "".concat(item.name, ", ").concat(item.description),
    disabled: item.inactive,
    size: size
  }), /*#__PURE__*/React.createElement(AvatarContent, {
    imageUrl: item.imageUrl,
    initials: item.initials,
    type: item.type,
    size: size
  })));
}
export function FoldedAvatarStack(_ref5) {
  var items = _ref5.items,
    onClickViewAll = _ref5.onClickViewAll,
    size = _ref5.size,
    restCountLabel = _ref5.restCountLabel,
    isViewAllNeeded = _ref5.isViewAllNeeded,
    title = _ref5.title;
  var I18n = useI18nContext();
  var overlayRef = React.useRef(null);
  var restAvatarId = useId();
  return /*#__PURE__*/React.createElement(Popover, {
    "aria-labelledby": restAvatarId,
    overlayRef: overlayRef,
    trigger: defaultPopoverTrigger,
    beforeHide: function beforeHide(e) {
      return e.type !== 'blur';
    },
    overlay: /*#__PURE__*/React.createElement(Popover.Content, null, /*#__PURE__*/React.createElement(StyledContactItems, null, items.map(function (item) {
      return /*#__PURE__*/React.createElement(AvatarStackContactItem, {
        key: item.id,
        id: item.id,
        type: item.type,
        imageUrl: item.imageUrl,
        initials: item.initials,
        inactive: item.inactive,
        name: item.name,
        linkUrl: item.linkUrl,
        description: item.description
      });
    })), isViewAllNeeded && /*#__PURE__*/React.createElement(StyledViewAllWrapper, null, /*#__PURE__*/React.createElement(Button, {
      "data-qa": "core-avatar-stack-view-all-modal-trigger",
      onClick: onClickViewAll,
      size: "sm",
      variant: "secondary"
    }, I18n.t('core.avatarStack.viewAll'))))
  }, /*#__PURE__*/React.createElement(StyledAvatar, {
    role: "button",
    onPress: onClickViewAll,
    id: restAvatarId,
    "aria-label": "".concat(restCountLabel, ", ").concat(title),
    "data-qa": "core-avatar-stack-folded-avatars-popover-trigger",
    size: size,
    $color: "gray85"
  }, /*#__PURE__*/React.createElement(Avatar.Label, {
    "aria-hidden": true
  }, /*#__PURE__*/React.createElement(Typography, {
    color: "black"
  }, restCountLabel))));
}
export function ViewAllModal(_ref6) {
  var isOpen = _ref6.isOpen,
    onClose = _ref6.onClose,
    title = _ref6.title,
    items = _ref6.items;
  var I18n = useI18nContext();
  var labelId = useId();
  var text = "".concat(title, " (").concat(items.length, ")");
  return /*#__PURE__*/React.createElement(Modal, {
    role: "dialog",
    "aria-labelledby": labelId,
    open: isOpen,
    onClose: onClose
  }, /*#__PURE__*/React.createElement(Modal.Header, {
    onClose: onClose
  }, /*#__PURE__*/React.createElement(Modal.Heading, {
    id: labelId
  }, text)), /*#__PURE__*/React.createElement(StyledModalBody, null, items.map(function (item) {
    return /*#__PURE__*/React.createElement(AvatarStackContactItem, {
      key: item.id,
      id: item.id,
      type: item.type,
      imageUrl: item.imageUrl,
      initials: item.initials,
      inactive: item.inactive,
      name: item.name,
      linkUrl: item.linkUrl,
      description: item.description
    });
  })), /*#__PURE__*/React.createElement(Modal.Footer, null, /*#__PURE__*/React.createElement(Modal.FooterButtons, null, /*#__PURE__*/React.createElement(Button, {
    onClick: onClose
  }, I18n.t('core.avatarStack.close')))));
}
export function defaultInitials(item) {
  return item.initials;
}
export function defaultGetImageUrl(item) {
  return item.imageUrl;
}
export function getTransformedItems(_ref7) {
  var items = _ref7.items,
    getInitials = _ref7.getInitials,
    getImageUrl = _ref7.getImageUrl;
  return items.map(function (item) {
    return _objectSpread(_objectSpread({}, item), {}, {
      initials: getInitials(item),
      imageUrl: getImageUrl(item)
    });
  });
}
var _AvatarStack = function _AvatarStack(_ref8, ref) {
  var _items = _ref8.items,
    title = _ref8.title,
    _ref8$size = _ref8.size,
    size = _ref8$size === void 0 ? 'lg' : _ref8$size,
    _ref8$getInitials = _ref8.getInitials,
    getInitials = _ref8$getInitials === void 0 ? defaultInitials : _ref8$getInitials,
    _ref8$getImageUrl = _ref8.getImageUrl,
    getImageUrl = _ref8$getImageUrl === void 0 ? defaultGetImageUrl : _ref8$getImageUrl,
    onClickViewAll = _ref8.onClickViewAll,
    props = _objectWithoutProperties(_ref8, _excluded2);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isModalOpen = _useState2[0],
    setIsModalOpen = _useState2[1];
  var items = useMemo(function () {
    return getTransformedItems({
      items: _items,
      getInitials: getInitials,
      getImageUrl: getImageUrl
    });
  }, [_items]);
  var _useMemo = useMemo(function () {
      return getOverflowValues(items);
    }, [items]),
    visibleItems = _useMemo.visibleItems,
    foldedItems = _useMemo.foldedItems,
    restCountLabel = _useMemo.restCountLabel,
    isViewAllNeeded = _useMemo.isViewAllNeeded;
  var visibleItemsColors = useMemo(function () {
    return getColorOrder(visibleItems);
  }, [visibleItems]);
  return /*#__PURE__*/React.createElement("div", _extends({
    ref: ref
  }, props), isViewAllNeeded && !onClickViewAll && /*#__PURE__*/React.createElement(ViewAllModal, {
    isOpen: isModalOpen,
    onClose: function onClose() {
      return setIsModalOpen(false);
    },
    title: title,
    items: items
  }), /*#__PURE__*/React.createElement(StyledWrapper, null, visibleItems.map(function (item) {
    return /*#__PURE__*/React.createElement(AvatarWithPopover, {
      colors: visibleItemsColors,
      item: item,
      key: "".concat(item.name, "_").concat(item.id),
      size: size
    });
  }), foldedItems.length > 0 && /*#__PURE__*/React.createElement(FoldedAvatarStack, {
    restCountLabel: restCountLabel,
    isViewAllNeeded: isViewAllNeeded,
    items: foldedItems,
    onClickViewAll: onClickViewAll || function () {
      return setIsModalOpen(true);
    },
    size: size,
    title: title
  })));
};

/**

 We use avatars to visually represent our users, places, and things in the app.
 These can be in the form of rich media or representative illustrations.

 @since 10.19.0

 @see [Storybook](https://procore.github.io/core/latest/?path=/story/demos-avatarstack--demo)

 @see [Design Guidelines](https://design.procore.com/avatar-stack)
 */
export var AvatarStack = /*#__PURE__*/forwardRef(_AvatarStack);

// @ts-ignore
AvatarStack.displayName = 'AvatarStack';
//# sourceMappingURL=AvatarStack.js.map