var _excluded = ["afterHide", "afterShow", "beforeHide", "beforeShow", "block", "getGroupId", "getId", "getLabel", "getNextGroupId", "getValueString", "i18nScope", "onChange", "onClear", "onNavigate", "onQuickCreate", "onScrollBottom", "onSearch", "selectableTiers", "isTierDisabled", "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; }
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; }
import React from 'react';
import { LeafSelectionTieredSelect } from './LeafSelectionTieredSelect';
import { TierSelectionTieredSelect } from './TierSelectionTieredSelect';
function defaultGetGroupId(tier) {
  return tier.groupId;
}
function defaultGetId(tier) {
  return tier.id;
}
function defaultGetLabel(tier) {
  return tier.label;
}
function defaultGetNextGroupId(tier) {
  return tier.nextGroupId;
}
function getFullPath() {
  var getLabel = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetLabel;
  return function getValueString(value) {
    return value.map(function (tier) {
      return getLabel(tier);
    }).join(' > ');
  };
}
function getLeafOnly() {
  var getLabel = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetLabel;
  return function getValueString(value) {
    return value.length ? getLabel(value[value.length - 1]) : '';
  };
}
function getValueStringFn(valueStringResolution) {
  var getLabel = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetLabel;
  if (valueStringResolution === 'full-path') {
    return getFullPath(getLabel);
  }
  if (valueStringResolution === 'leaf-only') {
    return getLeafOnly(getLabel);
  }
  return valueStringResolution;
}
function noop() {}
function always() {
  return true;
}
export var useHighlightItemEffects = function useHighlightItemEffects(_ref) {
  var currentTier = _ref.currentTier,
    searchValue = _ref.searchValue,
    menuRef = _ref.menuRef,
    loading = _ref.loading,
    loadingMore = _ref.loadingMore;
  var _React$useState = React.useState(),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    previousTier = _React$useState2[0],
    setPreviousTier = _React$useState2[1];
  React.useEffect(function () {
    setPreviousTier(currentTier);
  }, [currentTier]);
  var _React$useState3 = React.useState(''),
    _React$useState4 = _slicedToArray(_React$useState3, 2),
    previousSearch = _React$useState4[0],
    setPreviousSearch = _React$useState4[1];
  React.useEffect(function () {
    setPreviousSearch(searchValue);
  }, [searchValue]);
  var currentMenuRef = menuRef.current;
  React.useEffect(function () {
    if (previousSearch !== searchValue || previousTier !== currentTier) {
      currentMenuRef === null || currentMenuRef === void 0 ? void 0 : currentMenuRef.highlightFirst();
    }
  }, [currentMenuRef, currentTier, previousTier, searchValue, previousSearch]);
  React.useEffect(function () {
    if (loadingMore) {
      currentMenuRef === null || currentMenuRef === void 0 ? void 0 : currentMenuRef.highlightLast(false);
    }
  }, [currentMenuRef, loadingMore]);
  React.useEffect(function () {
    if (!loading && !(currentMenuRef !== null && currentMenuRef !== void 0 && currentMenuRef.highlighted())) {
      currentMenuRef === null || currentMenuRef === void 0 ? void 0 : currentMenuRef.highlightFirst();
    }
  }, [currentMenuRef, loading]);
};
export var TieredSelectContext = /*#__PURE__*/React.createContext({
  afterHide: noop,
  afterShow: noop,
  beforeHide: always,
  beforeShow: always,
  getGroupId: defaultGetGroupId,
  getId: defaultGetId,
  getLabel: defaultGetLabel,
  getNextGroupId: defaultGetNextGroupId,
  getValueString: getFullPath(defaultGetLabel),
  i18nScope: 'core.tieredSelect',
  onChange: noop
});

/**

 We use tiered selects to allow users to choose a single parent or child option
 that is nested within a tiered list. For example, choosing a location from
 within a list of tiered locations. We typically see these selects on forms.

 Other select components are group select, multi select, and select.
 @since 10.19.0

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

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

 */
export var TieredSelect = /*#__PURE__*/React.forwardRef(function TieredSelect(_ref2, ref) {
  var _ref2$afterHide = _ref2.afterHide,
    afterHide = _ref2$afterHide === void 0 ? noop : _ref2$afterHide,
    _ref2$afterShow = _ref2.afterShow,
    afterShow = _ref2$afterShow === void 0 ? noop : _ref2$afterShow,
    _ref2$beforeHide = _ref2.beforeHide,
    beforeHide = _ref2$beforeHide === void 0 ? always : _ref2$beforeHide,
    _ref2$beforeShow = _ref2.beforeShow,
    beforeShow = _ref2$beforeShow === void 0 ? always : _ref2$beforeShow,
    block = _ref2.block,
    _ref2$getGroupId = _ref2.getGroupId,
    getGroupId = _ref2$getGroupId === void 0 ? defaultGetGroupId : _ref2$getGroupId,
    _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$getNextGroupId = _ref2.getNextGroupId,
    getNextGroupId = _ref2$getNextGroupId === void 0 ? defaultGetNextGroupId : _ref2$getNextGroupId,
    _ref2$getValueString = _ref2.getValueString,
    getValueString = _ref2$getValueString === void 0 ? getFullPath(getLabel) : _ref2$getValueString,
    _ref2$i18nScope = _ref2.i18nScope,
    i18nScope = _ref2$i18nScope === void 0 ? 'core.tieredSelect' : _ref2$i18nScope,
    _ref2$onChange = _ref2.onChange,
    onChange = _ref2$onChange === void 0 ? noop : _ref2$onChange,
    onClear = _ref2.onClear,
    onNavigate = _ref2.onNavigate,
    onQuickCreate = _ref2.onQuickCreate,
    onScrollBottom = _ref2.onScrollBottom,
    onSearch = _ref2.onSearch,
    _ref2$selectableTiers = _ref2.selectableTiers,
    selectableTiers = _ref2$selectableTiers === void 0 ? true : _ref2$selectableTiers,
    _ref2$isTierDisabled = _ref2.isTierDisabled,
    isTierDisabled = _ref2$isTierDisabled === void 0 ? function () {
      return false;
    } : _ref2$isTierDisabled,
    tabIndex = _ref2.tabIndex,
    props = _objectWithoutProperties(_ref2, _excluded);
  var isLeaf = React.useCallback(function (tier) {
    return !getNextGroupId(tier);
  }, [getNextGroupId]);
  return /*#__PURE__*/React.createElement(TieredSelectContext.Provider, {
    value: {
      afterHide: afterHide,
      afterShow: afterShow,
      beforeHide: beforeHide,
      beforeShow: beforeShow,
      block: block,
      getGroupId: getGroupId,
      getId: getId,
      getLabel: getLabel,
      getNextGroupId: getNextGroupId,
      getValueString: getValueStringFn(getValueString),
      i18nScope: i18nScope,
      isLeaf: isLeaf,
      isTierDisabled: isTierDisabled,
      onChange: onChange,
      onClear: onClear,
      onNavigate: onNavigate,
      onQuickCreate: onQuickCreate,
      onScrollBottom: onScrollBottom,
      onSearch: onSearch,
      tabIndex: tabIndex
    }
  }, selectableTiers ? /*#__PURE__*/React.createElement(TierSelectionTieredSelect, _extends({
    ref: ref
  }, props)) : /*#__PURE__*/React.createElement(LeafSelectionTieredSelect, _extends({
    ref: ref
  }, props)));
});
//# sourceMappingURL=TieredSelect.js.map