var _excluded = ["color", "label", "selected"],
  _excluded2 = ["afterHide", "afterShow", "getColor", "getGroup", "getId", "getLabel", "getSuggested", "groupGetId", "groupGetLabel", "optgroups", "options", "value"];
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
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 { Check } from '@procore/core-icons/dist';
import React from 'react';
import { Pill } from '../Pill/Pill';
import { Select } from '../Select/Select';
import { Tooltip } from '../Tooltip/Tooltip';
import { OverflowObserver } from '../_hooks/OverflowObserver';
import { PillSelectCheckedContainer, PillSelectLabel, PillSelectLabelWrapper, PillSelectOptionWrapper } from './PillSelect.styles';
var emptyArr = [];
function defaultGetId(option) {
  return option.id;
}
function defaultGetLabel(option) {
  return option.label;
}
function defaultGetGroup(option) {
  return option.groupId;
}
function defaultGetSuggested(option) {
  return option.suggested;
}
export function defaultGetColor(option) {
  return option.color;
}
var PillSelectOption = /*#__PURE__*/React.forwardRef(function PillSelectOption(_ref, ref) {
  var color = _ref.color,
    label = _ref.label,
    selected = _ref.selected,
    props = _objectWithoutProperties(_ref, _excluded);
  return /*#__PURE__*/React.createElement(Select.Option, _extends({
    selected: selected
  }, props, {
    ref: ref
  }), /*#__PURE__*/React.createElement(PillSelectOptionWrapper, null, /*#__PURE__*/React.createElement(Pill, {
    color: color
  }, label), /*#__PURE__*/React.createElement(PillSelectCheckedContainer, null, selected && /*#__PURE__*/React.createElement(Check, {
    size: "sm"
  }))));
});
var PillSelectOptGroup = /*#__PURE__*/React.forwardRef(function PillSelectOptGroup(props, ref) {
  return /*#__PURE__*/React.createElement(Select.OptGroup, _extends({}, props, {
    ref: ref
  }));
});

/**

 Pill Selects are used when you need a user to select between one of several
 options that are shown as pills. Often this will be a status, such as marking
 an item “closed”, “draft”, etc.

 @since 10.19.0

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

 @see [Design Guidelines](https://design.procore.com/pill-select)

 */
export var PillSelect = /*#__PURE__*/React.forwardRef(function PillSelect(_ref2, ref) {
  var afterHide_ = _ref2.afterHide,
    afterShow_ = _ref2.afterShow,
    _ref2$getColor = _ref2.getColor,
    getColor = _ref2$getColor === void 0 ? defaultGetColor : _ref2$getColor,
    _ref2$getGroup = _ref2.getGroup,
    getGroup = _ref2$getGroup === void 0 ? defaultGetGroup : _ref2$getGroup,
    _ref2$getId = _ref2.getId,
    getId = _ref2$getId === void 0 ? defaultGetId : _ref2$getId,
    _ref2$getLabel = _ref2.getLabel,
    getLabel = _ref2$getLabel === void 0 ? defaultGetLabel : _ref2$getLabel,
    _ref2$getSuggested = _ref2.getSuggested,
    getSuggested = _ref2$getSuggested === void 0 ? defaultGetSuggested : _ref2$getSuggested,
    _ref2$groupGetId = _ref2.groupGetId,
    groupGetId = _ref2$groupGetId === void 0 ? defaultGetId : _ref2$groupGetId,
    _ref2$groupGetLabel = _ref2.groupGetLabel,
    groupGetLabel = _ref2$groupGetLabel === void 0 ? defaultGetLabel : _ref2$groupGetLabel,
    optgroups = _ref2.optgroups,
    _ref2$options = _ref2.options,
    options = _ref2$options === void 0 ? emptyArr : _ref2$options,
    value = _ref2.value,
    props = _objectWithoutProperties(_ref2, _excluded2);
  var _React$useMemo = React.useMemo(function () {
      if (!value) {
        return {};
      }
      return {
        color: getColor(value),
        id: getId(value),
        label: getLabel(value)
      };
    }, [getColor, getId, getLabel, value]),
    color = _React$useMemo.color,
    id = _React$useMemo.id,
    label = _React$useMemo.label;
  var optionsByGroup = React.useMemo(function () {
    return optgroups === null || optgroups === void 0 ? void 0 : optgroups.map(function (optgroup) {
      return {
        optgroup: optgroup,
        options: options.filter(function (option) {
          return getGroup(option) === groupGetId(optgroup);
        })
      };
    }).filter(function (_ref3) {
      var options = _ref3.options;
      return options.length;
    });
  }, [options, optgroups, groupGetId, getGroup]);
  var _React$useState = React.useState(false),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    isOpened = _React$useState2[0],
    setIsOpened = _React$useState2[1];
  var afterShow = React.useCallback(function () {
    setIsOpened(true);
    afterShow_ === null || afterShow_ === void 0 ? void 0 : afterShow_();
  }, [afterShow_]);
  var afterHide = React.useCallback(function () {
    setIsOpened(false);
    afterHide_ === null || afterHide_ === void 0 ? void 0 : afterHide_();
  }, [afterHide_]);
  return /*#__PURE__*/React.createElement(Select, _extends({
    afterHide: afterHide,
    afterShow: afterShow,
    label: label ? /*#__PURE__*/React.createElement(OverflowObserver, null, function (_ref4) {
      var isOverflowing = _ref4.isOverflowing,
        ref = _ref4.ref;
      var pill = /*#__PURE__*/React.createElement(PillSelectLabel, {
        color: color,
        ref: ref
      }, label);
      var showTooltip = isOverflowing && !isOpened;
      return /*#__PURE__*/React.createElement(PillSelectLabelWrapper, {
        disabled: props.disabled
      }, showTooltip ? /*#__PURE__*/React.createElement(Tooltip, {
        trigger: "hover",
        overlay: label
      }, pill) : pill);
    }) : null
  }, props, {
    ref: ref
  }), optionsByGroup ? optionsByGroup.map(function (_ref5) {
    var optgroup = _ref5.optgroup,
      options = _ref5.options;
    return /*#__PURE__*/React.createElement(React.Fragment, {
      key: groupGetId(optgroup)
    }, /*#__PURE__*/React.createElement(PillSelectOptGroup, {
      key: groupGetId(optgroup)
    }, groupGetLabel(optgroup)), options.map(function (option) {
      return /*#__PURE__*/React.createElement(PillSelectOption, {
        key: getId(option),
        color: getColor(option),
        label: getLabel(option),
        selected: getId(option) === id,
        suggested: getSuggested(option),
        value: option
      });
    }));
  }) : /*#__PURE__*/React.createElement(React.Fragment, null, options.map(function (option) {
    return /*#__PURE__*/React.createElement(PillSelectOption, {
      key: getId(option),
      color: getColor(option),
      label: getLabel(option),
      selected: getId(option) === id,
      suggested: getSuggested(option),
      value: option
    });
  })));
});
PillSelect.displayName = 'PillSelect';
//# sourceMappingURL=PillSelect.js.map