var _excluded = ["color", "label", "selected"],
  _excluded2 = ["afterHide", "afterShow", "getColor", "getGroup", "getId", "getLabel", "getSuggested", "groupGetId", "groupGetLabel", "optgroups", "options", "value"];
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 _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 { 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://stories.core.procore.com/?path=/story/core-react_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