import _toConsumableArray from"@babel/runtime/helpers/toConsumableArray";import _slicedToArray from"@babel/runtime/helpers/slicedToArray";function _createForOfIteratorHelper(a,b){var c="undefined"!=typeof Symbol&&a[Symbol.iterator]||a["@@iterator"];if(!c){if(Array.isArray(a)||(c=_unsupportedIterableToArray(a))||b&&a&&"number"==typeof a.length){c&&(a=c);var d=0,e=function(){};return{s:e,n:function n(){return d>=a.length?{done:!0}:{done:!1,value:a[d++]}},e:function(a){function b(){return a.apply(this,arguments)}return b.toString=function(){return a.toString()},b}(function(a){throw a}),f:e}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var f,g=!0,h=!1;return{s:function s(){c=c.call(a)},n:function n(){var a=c.next();return g=a.done,a},e:function(a){function b(){return a.apply(this,arguments)}return b.toString=function(){return a.toString()},b}(function(a){h=!0,f=a}),f:function f(){try{g||null==c["return"]||c["return"]()}finally{if(h)throw f}}}}function _unsupportedIterableToArray(a,b){if(a){if("string"==typeof a)return _arrayLikeToArray(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(a):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?_arrayLikeToArray(a,b):void 0}}function _arrayLikeToArray(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}import{useCallback,useEffect,useMemo,useRef,useState}from"react";import{scrollToIndex}from"../SelectComponents/useSelect/helpers";import{useClickOutside}from"../../hooks/useClickOutside";import{useFlag}from"../../hooks/useFlag";import{useKeys}from"../../hooks/useKeysDepricated";import{useRefs}from"../../hooks/useRefs";import{getGroups}from"../../utils/getGroups";export function useAutoComplete(a){var b=a.items,c=a.dropdownRef,d=a.controlRef,e=a.disabled,f=void 0!==e&&e,g=a.getItemLabel,h=a.getItemKey,i=a.searchFunction,j=a.getItemGroupKey,k=a.groups,l=a.getGroupKey,m=a.onFocus,n=a.onBlur,o=a.searchValue,p=a.isLoading,q=a.dropdownOpen,r=a.onDropdownOpen,s=a.ignoreOutsideClicksRefs,t=useRef(null),u=useFlag(),v=_slicedToArray(u,2),w=v[0],x=v[1],y=useState(0),z=_slicedToArray(y,2),A=z[0],B=z[1],C=function(a,b){return!!b&&-1!==g(a).toLocaleLowerCase().indexOf(b.toLocaleLowerCase())},D=useMemo(function(){return b.filter(function(a){return i?i(a,o||""):C(a,o||"")})},[o,b]),E=useMemo(function(){var a=getGroups(D,null!==k&&void 0!==k&&k.length?j:void 0,k,l,void 0);return a},[D,k,j,l]),F=useMemo(function(){return!!E.find(function(a){return 0<a.items.length})},[E]),G=useCallback(function(a){B(Math.min(Math.max(0,"function"==typeof a?a(A):a),D.length-1))},[D,A]),H=function(b,c){f||a.onChange({value:c,e:b})},I=function(a,b){f||(b.preventDefault(),x.on(),G(function(a){return a-1}))},J=function(a,b){f||(b.preventDefault(),x.on(),G(function(a){return a+1}))},K=useKeys({ArrowUp:I,ArrowDown:J,PageUp:I,PageDown:J,Home:I,End:J,Enter:function Enter(a,b){if(w){(o||D[A])&&b.preventDefault();var c=function(a){var b,c=0,d=_createForOfIteratorHelper(E);try{for(d.s();!(b=d.n()).done;){var e=b.value;if(e.items.length+c>a)return e.items[a-c];c+=e.items.length}}catch(a){d.e(a)}finally{d.f()}},d=c(A);d&&H(b,d)}else x.on()},Escape:function Escape(){x.off()},Tab:function Tab(a,b){x.off(),w&&F&&b.preventDefault()}}),L=useRefs(D.length,[w]);return useClickOutside({isActive:w,ignoreClicksInsideRefs:[c,d].concat(_toConsumableArray(s||[])),handler:x.off}),useEffect(function(){if(f){var a;x.off(),null===(a=t.current)||void 0===a?void 0:a.blur()}},[f]),useEffect(function(){0<D.length&&scrollToIndex(A,c,L,function(){return G(0)}),x.on()},[A]),useEffect(function(){null===r||void 0===r?void 0:r(w)},[w]),useEffect(function(){x.set(q||!1)},[q]),{isOpen:!!(w&&(!!p||F)),visibleItems:E,getOptionProps:function getOptionProps(a){var b=a.index,c=a.item,d=h(c);return{onClick:function onClick(a){H(a,c)},onMouseEnter:function onMouseEnter(){G(b)},active:!1,hovered:b===A,key:d}},handleInputFocus:function handleInputFocus(a){f||(x.toggle(),"function"==typeof m&&m(a))},handleInputBlur:function handleInputBlur(a){if(w){var b;return void(null===(b=t.current)||void 0===b?void 0:b.focus())}"function"==typeof n&&n(a)},inputRef:t,getKeyProps:K,hasItems:F,optionsRefs:L}}
//# sourceMappingURL=useAutoComplete.js.map