import{slicedToArray as e}from"../../_virtual/_rollupPluginBabelHelpers.js";import o,{forwardRef as t,useRef as i,useState as s,useCallback as n,useMemo as r}from"react";import"prop-types";import{noop as l,isFunction as a}from"lodash-es";import c from"classnames";import{ComponentDefaultTestId as d}from"../../tests/constants.js";import{getTestId as m}from"../../tests/testIds.js";import p from"../../hooks/useMergeRefs.js";import u from"../Search/Search.js";import{SIZES as b}from"../../constants/sizes.js";import f from"../Button/Button.js";import v from"./components/ComboboxOption/ComboboxOption.js";import{defaultFilter as g}from"./ComboboxService.js";import{ComboboxItems as h}from"./components/ComboboxItems/ComboboxItems.js";import{StickyCategoryHeader as C}from"./components/StickyCategoryHeader/StickyCategoryHeader.js";import O from"../../hooks/useActiveDescendantListFocus/index.js";import{getOptionId as x}from"./helpers.js";import"./Combobox.scss.js";var I=t((function(t,l){var b=t.className,v=t.id,g=t.placeholder,I=t.size,S=t.defaultVisualFocusFirstIndex,y=t.optionLineHeight,F=t.optionsListHeight,k=t.autoFocus,N=t.disabled,w=t.options,E=t.categories,R=t.withCategoriesDivider,j=t.noResultsMessage,L=t.onAddNew,H=t.addNewLabel,A=t.onClick,B=t.filter,T=t.disableFilter,z=t.onFilterChanged,M=t.loading,V=t.onOptionHover,D=t.onOptionLeave,U=t.shouldScrollToSelectedItem,W=t.noResultsRenderer,X=t.stickyCategories,P=t.optionRenderer,_=t.renderOnlyVisibleOptions,Y=t.clearFilterOnSelection,q=t.maxOptionsWithoutScroll,G=t.forceUndoScrollNullCheck,J=void 0!==G&&G,K=i(null),Q=i(null),Z=i(null),$=s(""),ee=e($,2),oe=ee[0],te=ee[1],ie=p({refs:[l,K]}),se=n((function(e){z&&z(e),te(e)}),[te,z]),ne=n((function(e,o,t){V(e,o,t)}),[V]),re=r((function(){return T?w:B(oe,w)}),[w,oe,B,T]),le=s(-1),ae=e(le,2),ce=ae[0],de=ae[1],me=n((function(e){return void 0!==e&&re[e]&&!re[e].disabled}),[re]),pe=r((function(){return re.map((function(e,o){return x(null==e?void 0:e.id,o)}))}),[re]),ue=n((function(){L&&L(oe),te("")}),[L,oe,te]),be=n((function(e,o){A(re[o]),me(o)&&de(o),Y&&se("")}),[A,re,me,Y,se]),fe=O({defaultVisualFocusFirstIndex:S,focusedElementRef:Q,containerElementRef:Z,focusedElementRole:O.roles.COMBOBOX,itemsIds:pe,onItemClick:be,isItemSelectable:me,isIgnoreSpaceAsItemSelection:!0}),ve=fe.visualFocusItemIndex,ge=fe.visualFocusItemId,he=fe.onItemClickCallback,Ce=re.length>0,Oe=oe.length>0;var xe=s(),Ie=e(xe,2),Se=Ie[0],ye=Ie[1],Fe=n((function(e){var o,t;(null==e||null===(o=e.category)||void 0===o?void 0:o.label)!==Se&&ye(null==e||null===(t=e.category)||void 0===t?void 0:t.label)}),[Se]);return o.createElement("div",{ref:ie,className:c("combobox--wrapper",b,"size-".concat(I),{empty:!Ce,"sticky-category":X}),id:v,"data-testid":m(d.COMBOBOX,v)},o.createElement("div",{className:"combobox--wrapper-list",style:{maxHeight:F},role:"listbox"},o.createElement(u,{ref:Q,value:oe,wrapperClassName:"combobox--wrapper-search-wrapper",className:"combobox--wrapper-search",inputAriaLabel:"Search for content",activeDescendant:ge,id:"combobox-search",placeholder:g,size:I,disabled:N,onChange:se,autoFocus:k,loading:M}),X&&o.createElement(C,{label:Se}),o.createElement(h,{forceUndoScrollNullCheck:J,ref:Z,categories:E,options:re,filterValue:oe,withCategoriesDivider:R,optionRenderer:P,activeItemIndex:ce,visualFocusItemIndex:ve,onActiveCategoryChanged:Fe,onOptionClick:he,onOptionEnter:ne,onOptionLeave:D,optionLineHeight:y,shouldScrollToSelectedItem:U,renderOnlyVisibleOptions:_,maxOptionsWithoutScroll:q})),Oe&&!Ce&&!M&&(W?W():o.createElement("div",{className:"combobox--wrapper-no-results"},o.createElement("div",{className:"combobox-message-wrapper"},o.createElement("span",{className:"combobox-message"},j)),L&&!N&&o.createElement(f,{className:"add-new-button",size:I,kind:f.kinds.TERTIARY,onClick:ue},o.createElement("span",{className:"button-label"},a(H)?H(oe):H)))))}));I.sizes=b,I.iconTypes=v.iconTypes,I.defaultTestId=d.COMBOBOX,I.defaultProps={className:"",placeholder:"",id:"",noResultsMessage:"No results found",disabled:!1,options:[],categories:void 0,withCategoriesDivider:!1,size:I.sizes.MEDIUM,optionLineHeight:32,optionsListHeight:void 0,autoFocus:!1,maxOptionsWithoutScroll:void 0,onAddNew:void 0,addNewLabel:"Add new",filter:g,disableFilter:!1,onFilterChanged:void 0,loading:!1,onOptionHover:l,onOptionLeave:l,shouldScrollToSelectedItem:!0,noResultsRenderer:void 0,stickyCategories:!1,optionRenderer:null,clearFilterOnSelection:!1,defaultVisualFocusFirstIndex:void 0,renderOnlyVisibleOptions:!1,onClick:function(e){}};var S=I;export{S as default};
