import{defineProperty as e}from"../../../../_virtual/_rollupPluginBabelHelpers.js";import t,{forwardRef as o,useRef as r,useMemo as i,useCallback as n}from"react";import a from"classnames";import{getOptionsByCategories as s}from"../../ComboboxService.js";import{comboboxItemRenderer as l,createDividerItemObject as c,createCategoryItemObject as m,createOptionItemObject as p}from"../../ComboboxHelpers/ComboboxHelpers.js";import u from"../../../VirtualizedList/VirtualizedList.js";import{COMBOBOX_CATEGORY_ITEM as d,COMBOBOX_OPTION_ITEM as f}from"../ComboboxConstants.js";import h from"./ComboboxItems.module.scss.js";var g=o((function(o,g){var v=o.className,y=o.categories,C=o.options,I=o.filterValue,b=o.withCategoriesDivider,x=o.optionRenderer,O=o.activeItemIndex,j=o.visualFocusItemIndex,S=o.onOptionClick,E=o.onOptionEnter,N=o.onOptionLeave,k=o.optionLineHeight,L=o.shouldScrollToSelectedItem,R=o.renderOnlyVisibleOptions,w=o.onActiveCategoryChanged,H=o.maxOptionsWithoutScroll,V=o.forceUndoScrollNullCheck,D=r(),M=i((function(){if(H)return{height:k*Math.min(C.length+Object.keys(null!=y?y:{}).length,H+.5)}}),[H,k,C,y]),z=n((function(e,t,o){return l({item:e,index:t,style:o,optionEvents:{onOptionClick:S,onOptionEnter:E,onOptionLeave:N},optionRenderData:{optionLineHeight:k,optionRenderer:x,visualFocusItemIndex:j,scrollRef:R?null:g,activeItemIndex:O,shouldScrollToSelectedItem:L,forceUndoScrollNullCheck:V},isVirtualized:R})}),[S,E,N,k,x,g,j,O,L,V,R]),F=i((function(){var t=[],o=new Map;if(y){var r=s(C,y,I),i=0;Object.keys(r).forEach((function(n,s){var l,u=b&&0!==s;u&&t.push(c({categoryId:n}));var d=r[n].length>0,f=0===s,g=m({categoryId:n,categoryData:y[n],withDivider:u,className:a((l={},e(l,h.categoryWithOptions,d),e(l,h.categoryWithoutOptions,!d),e(l,h.firstCategory,f),l))});t.push(g),o.set(n,g),r[n].forEach((function(e){var r=p({height:k,option:e,index:i,categoryId:g.id});o.set(r.id,r),t.push(r),i++}))}))}else t=C.map((function(e,t){return p({height:k,option:e,index:t})}));return{items:t,itemsMap:o}}),[y,C,I,b,k]),W=F.items,A=F.itemsMap,P=n((function(e){var t=e.firstItemId;window.requestAnimationFrame((function(){var e=A.get(t);if(e&&(e.type===d||e.type===f)){var o=e.type===f&&e.categoryId?e.categoryId:e.id;if(o!==D.current){D.current=o;var r=A.get(D.current);w(r)}}}),[A,w])}),[A,w]);return R?t.createElement(u,{ref:g,className:a(h.optionsContainer,v),items:W,itemRenderer:z,role:"treegrid",scrollableClassName:h.scrollableContainer,onItemsRendered:P,style:M}):t.createElement("div",{className:a(h.scrollableContainer,h.optionsContainer,v),role:"treegrid",style:M,ref:g},W.map((function(e){return z(e)})))}));export{g as ComboboxItems};
