var _excluded = ["icon", "count", "countTruncationThreshold", "loading", "error", "selected", "disabled", "focused", "children", "tabIndex"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { Error } from '@procore/core-icons/dist';
import React from 'react';
import { Spinner } from '../../Spinner/Spinner';
import { Tooltip } from '../../Tooltip/Tooltip';
import { OverflowObserver } from '../../_hooks/OverflowObserver';
import { StyledIcon, StyledLabel, StyledSourceItem } from './SourceItem.styles';
function formatCount(count, truncationThreshold) {
  if (count === 0) {
    return '';
  } else if (count > truncationThreshold) {
    return "(".concat(truncationThreshold, "+)");
  } else {
    return "(".concat(count, ")");
  }
}
export var SourceItem = /*#__PURE__*/React.forwardRef(function SourceItem(_ref, ref) {
  var _ref$icon = _ref.icon,
    icon = _ref$icon === void 0 ? '' : _ref$icon,
    _ref$count = _ref.count,
    count = _ref$count === void 0 ? 0 : _ref$count,
    _ref$countTruncationT = _ref.countTruncationThreshold,
    countTruncationThreshold = _ref$countTruncationT === void 0 ? 99 : _ref$countTruncationT,
    _ref$loading = _ref.loading,
    loading = _ref$loading === void 0 ? false : _ref$loading,
    _ref$error = _ref.error,
    error = _ref$error === void 0 ? false : _ref$error,
    _ref$selected = _ref.selected,
    selected = _ref$selected === void 0 ? false : _ref$selected,
    _ref$disabled = _ref.disabled,
    disabled = _ref$disabled === void 0 ? false : _ref$disabled,
    _ref$focused = _ref.focused,
    focused = _ref$focused === void 0 ? false : _ref$focused,
    children = _ref.children,
    _ref$tabIndex = _ref.tabIndex,
    tabIndex = _ref$tabIndex === void 0 ? 0 : _ref$tabIndex,
    props = _objectWithoutProperties(_ref, _excluded);
  var tabIndexAttribute = !disabled ? {
    tabIndex: tabIndex
  } : {};
  var iconElement = /*#__PURE__*/React.createElement(StyledIcon, null, function () {
    if (error) {
      return /*#__PURE__*/React.createElement(Error, null);
    }
    if (loading) {
      return /*#__PURE__*/React.createElement(Spinner, {
        size: "xs"
      });
    }
    return icon;
  }());
  var labelElement = /*#__PURE__*/React.createElement(OverflowObserver, null, function (_ref2) {
    var isOverflowing = _ref2.isOverflowing,
      ref = _ref2.ref;
    var item = /*#__PURE__*/React.createElement(StyledLabel, {
      ref: ref
    }, children);
    return isOverflowing ? /*#__PURE__*/React.createElement(Tooltip, {
      showDelay: 500,
      trigger: "hover",
      overlay: /*#__PURE__*/React.createElement(Tooltip.Content, null, children)
    }, item) : item;
  });
  var countElement = /*#__PURE__*/React.createElement("div", null, "\xA0", formatCount(count, countTruncationThreshold));
  return /*#__PURE__*/React.createElement(StyledSourceItem, _extends({
    ref: ref
  }, tabIndexAttribute, props, {
    error: error,
    selected: selected,
    disabled: disabled,
    focused: focused
  }), iconElement, labelElement, countElement);
});
//# sourceMappingURL=SourceItem.js.map