import*as e from"react";import{PrimeReactContext as t,FilterService as n,localeOption as r}from"primereact/api";import{ComponentBase as o,useHandleStyle as i}from"primereact/componentbase";import{useMergeProps as l,useMountEffect as a}from"primereact/hooks";import{Tooltip as u}from"primereact/tooltip";import{classNames as c,IconUtils as s,ObjectUtils as p,DomHandler as f,UniqueComponentId as d}from"primereact/utils";import{VirtualScroller as m}from"primereact/virtualscroller";import{SearchIcon as v}from"primereact/icons/search";import{InputText as b}from"primereact/inputtext";import{Ripple as y}from"primereact/ripple";function h(){return h=Object.assign?Object.assign.bind():function(e){for(var t=1;te.length)&&(t=e.length);for(var n=0,r=Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},f:o}}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 i,l=!0,a=!1;return{s:function(){n=n.call(e)},n:function(){var e=n.next();return l=e.done,e},e:function(e){a=!0,i=e},f:function(){try{l||null==n.return||n.return()}finally{if(a)throw i}}}}function B(e,t){if(e){if("string"==typeof e)return _(e,t);var n={}.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?_(e,t):void 0}}function _(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n0,W={props:b,state:{filterValue:z}},Y=T.setMetaData(W);i(T.css.styles,Y.isUnstyled,{name:"listbox"});var Q=function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:-1;b.disabled||_e(t)||(b.multiple?re(e.originalEvent,t):ne(e.originalEvent,t),q.current=!1,-1!==n&&x(n))},Z=function(e,t){he(e,t)},ee=function(e,t){b.focusOnHover&&L&&he(e,t)},te=function(){b.disabled||(q.current=!0)},ne=function(e,t){var n=Ae(t),r=!1,o=null;!q.current&&b.metaKeySelection?n?(e.metaKey||e.ctrlKey)&&(o=null,r=!0):(o=Ve(t),r=!0):(o=n?null:Ve(t),r=!0);r&&Ce(e,o)},re=function(e,t){var n=Ae(t),r=!1,o=null;if(!q&&b.metaKeySelection){var i=e.metaKey||e.ctrlKey;n?(o=i?Te(t):[Ve(t)],r=!0):(o=[].concat(P(o=i&&b.value||[]),[Ve(t)]),r=!0)}else o=n?Te(t):[].concat(P(b.value||[]),[Ve(t)]),r=!0;r&&b.onChange({originalEvent:e,value:o,stopPropagation:function(){null==e||e.stopPropagation()},preventDefault:function(){null==e||e.preventDefault()},target:{name:b.name,id:b.id,value:o}})},oe=function(){return p.isNotEmpty(b.value)},ie=function(e){return b.optionGroupLabel&&e.optionGroup&&e.group},le=function(e){return p.isNotEmpty(e)&&!(_e(e)||ie(e))},ae=function(e){return le(e)&&Ae(e)},ue=function(){return Ye.findIndex((function(e){return le(e)}))},ce=function(){if(oe()){if(!b.multiple)return Ye.findIndex((function(e){return ae(e)}));for(var e,t=function(){var e=b.value[n],t=Ye.findIndex((function(t){return ae(t)&&Ge(e,Ve(t))}));if(t>-1)return{v:t}},n=b.value.length-1;n>=0;n--)if(e=t())return e.v}return-1},se=function(){return p.findLastIndex(Ye,(function(e){return le(e)}))},pe=function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1],n=-1;return oe()&&(n=t?-1===(n=ve(e))?me(e):n:-1===(n=me(e))?ve(e):n),n>-1?n:e},fe=function(e){var t;return le(e)&&(null===(t=Re(e))||void 0===t?void 0:t.toLocaleLowerCase(b.filterLocale).startsWith(B.toLocaleLowerCase(b.filterLocale)))},de=function(e,t){_((B||"")+t);var n=-1;p.isNotEmpty(B)&&(-1===(n=-1!==g?-1===(n=Ye.slice(g).findIndex((function(e){return fe(e)})))?Ye.slice(0,g).findIndex((function(e){return fe(e)})):n+g:Ye.findIndex((function(e){return fe(e)})))&&-1===g&&(n=ye()),-1!==n&&he(e,n)),O.current&&clearTimeout(O.current),O.current=setTimeout((function(){_(""),O.current=null}),500)},me=function(e){var t=oe()&&e-1?t+e+1:-1},ve=function(e){var t=oe()&&e>0?p.findLastIndex(Ye.slice(0,e),(function(e){return ae(e)})):-1;return t>-1?t:-1},be=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:-1,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:-1;if(-1===t&&(t=pe(n,!0)),-1===n&&(n=pe(t)),-1!==t&&-1!==n){var r=Math.min(t,n),o=Math.max(t,n),i=Ye.slice(r,o+1).filter((function(e){return le(e)})).map((function(e){return Ve(e)}));Ce(e,i)}},ye=function(){var e=oe()?Ye.findIndex((function(e){return ae(e)})):-1;return e<0?ue():e},he=function(e,t){g!==t&&(x(t),Pe(),e&&b.selectOnFocus&&!b.multiple&&Q(e,Ye[t]))},ge=function(e){var t,n,r=-1!==g?(n=(t=g)-1?n+t+1:t:ye();b.multiple&&e.shiftKey&&be(e,w,r),he(e,r),e.preventDefault()},xe=function(e){var t,n,r,o=-1!==g?(r=(n=g)>0?p.findLastIndex(Ye.slice(0,n),(function(e){return le(e)})):-1)>-1?r:n:(t=oe()?p.findLastIndex(Ye,(function(e){return ae(e)})):-1)<0?se():t;b.multiple&&e.shiftKey&&be(e,o,w),he(e,o),e.preventDefault()},Oe=function(e){-1!==g&&(b.multiple&&e.shiftKey?be(e,g):Q(e,Ye[g])),e.preventDefault()},Ie=function(e){Oe(e)},Ee=function(e){if(arguments.length>1&&void 0!==arguments[1]&&arguments[1])e.currentTarget.setSelectionRange(0,0),x(-1);else{var t=e.metaKey||e.ctrlKey,n=ue();b.multiple&&e.shiftKey&&t&&be(e,n,w),he(e,n)}e.preventDefault()},Se=function(e){if(arguments.length>1&&void 0!==arguments[1]&&arguments[1]){var t=e.currentTarget,n=t.value.length;t.setSelectionRange(n,n),x(-1)}else{var r=e.metaKey||e.ctrlKey,o=se();b.multiple&&e.shiftKey&&r&&be(e,w,o),he(e,o)}e.preventDefault()},we=function(e){Pe(0),e.preventDefault()},De=function(e){Pe(Ye.length-1),e.preventDefault()},Fe=function(e){var t=e.metaKey||e.ctrlKey;switch(e.code){case"ArrowDown":ge(e);break;case"ArrowUp":xe(e);break;case"Home":Ee(e);break;case"End":Se(e);break;case"PageDown":De(e);break;case"PageUp":we(e);break;case"Enter":case"NumpadEnter":case"Space":Ie(e),e.preventDefault();break;case"Tab":break;case"ShiftLeft":case"ShiftRight":D(g);break;default:if(b.multiple&&"a"===e.key&&t){var n=Ye.filter((function(e){return le(e)})).map((function(e){return Ve(e)}));Ce(e,n),e.preventDefault();break}!t&&p.isPrintableCharacter(e.key)&&(de(e,e.key),e.preventDefault())}},Pe=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:-1;setTimeout((function(){if(X.current){var t=-1!==e?"".concat(U.current,"_").concat(e):-1!==g?"".concat(U.current,"_").concat(g):null,n=X.current.querySelector('li[id="'.concat(t,'"]'));n?n.scrollIntoView({block:"nearest",inline:"nearest",behavior:"smooth"}):b.virtualScrollerOptions&&J.current&&J.current.scrollToIndex(-1!==e?e:g)}}),0)},Le=function(e){J.current&&J.current.scrollToIndex(0);var t=e.value;b.onFilterValueChange?b.onFilterValueChange({originalEvent:e.originalEvent,value:t}):K(t)},Ne=function(){K(""),b.onFilter&&b.onFilter({filter:""})},je=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:L;if(b.selectOnFocus&&b.autoOptionFocus&&!oe()&&!b.multiple&&e){var t=ye();Q(null,Ye[t]),x(t)}},Ce=function(e,t){b.onChange&&b.onChange({originalEvent:e,value:t,stopPropagation:function(){null==e||e.stopPropagation()},preventDefault:function(){null==e||e.preventDefault()},target:{name:b.name,id:b.id,value:t}})},Te=function(e){return b.value.filter((function(t){return!p.equals(t,Ve(e),b.dataKey)}))},Me=function(){if(null!=b.value&&Ye){if(!b.optionGroupLabel)return ke(b.value,Ye);for(var e=0;e2&&void 0!==arguments[2]?arguments[2]:{},o={height:r.props?r.props.itemSize:void 0};if(t.group&&t.optionGroup&&b.optionGroupLabel){var i=b.optionGroupTemplate?p.getJSXElement(b.optionGroupTemplate,t,n):p.resolveFieldData(t,b.optionGroupLabel),l=n+"_"+Ue(t),a=s({className:Y.cx("itemGroup"),style:Y.sx("itemGroup",{scrollerOptions:r}),role:"group"},Y.ptm("itemGroup"));return e.createElement("li",h({},a,{key:l}),i)}var u=Re(t),c=n+"_"+Be(t),f=_e(t);return e.createElement(G,{id:U.current+"_"+n,hostName:"ListBox",optionKey:c,key:c,label:u,index:n,onOptionMouseDown:Z,onOptionMouseMove:ee,focusedOptionIndex:g,option:t,style:o,template:b.itemTemplate,selected:Ae(t),onClick:Q,onTouchEnd:te,disabled:f,ptCallbacks:Y,metaData:W})},We=function(t,n){var o=s({className:Y.cx("emptyMessage")},Y.ptm("emptyMessage")),i=p.getJSXElement(t,b)||r(n?"emptyFilterMessage":"emptyMessage");return e.createElement("li",o,i)},Ye=function(){var e=b.optionGroupLabel?qe(b.options):b.options;if($){var t=z.trim().toLocaleLowerCase(b.filterLocale),r=b.filterBy?b.filterBy.split(","):[b.optionLabel||"label"];if(b.optionGroupLabel){var o,i=[],l=V(b.options);try{for(l.s();!(o=l.n()).done;){var a=o.value,u=n.filter(Xe(a),r,t,b.filterMatchMode,b.filterLocale);u&&u.length&&i.push(R(R({},a),{items:u}))}}catch(e){l.e(e)}finally{l.f()}return qe(i)}return n.filter(e,r,t,b.filterMatchMode,b.filterLocale)}return e}(),Qe=p.isNotEmpty(b.tooltip),Ze=T.getOtherProps(b),et=p.reduceKeys(Ze,f.ARIA_PROPS),tt=function(){if(b.virtualScrollerOptions){var t=R(R({},b.virtualScrollerOptions),{items:Ye,onLazyLoad:function(e){return b.virtualScrollerOptions.onLazyLoad(R(R({},e),{filter:Ye}))},itemTemplate:function(e,t){return e&&$e(e,t.index,t)},contentTemplate:function(t){var n=s(R({ref:X,style:Y.sx("list",{options:t}),className:Y.cx("list",{options:t}),role:"listbox",tabIndex:"-1","aria-multiselectable":b.multiple,onFocus:He,onBlur:Je,onKeyDown:Fe},et),Y.ptm("list"));return e.createElement("ul",n,t.children)}});return e.createElement(m,h({ref:J},t,{pt:Y.ptm("virtualScroller")}))}var n=p.isNotEmpty(Ye)?Ye.map($e):$?We(b.emptyFilterMessage,!0):We(b.emptyMessage),r=s(R({ref:X,className:Y.cx("list"),role:"listbox","aria-multiselectable":b.multiple,tabIndex:"-1",onFocus:He,onBlur:Je,onKeyDown:Fe},et),Y.ptm("list"));return e.createElement("ul",r,n)}(),nt=b.filter?e.createElement(k,{hostName:"ListBox",filter:z,filterIcon:b.filterIcon,onFilter:Le,resetFilter:Ne,filterTemplate:b.filterTemplate,disabled:b.disabled,filterPlaceholder:b.filterPlaceholder,filterInputProps:b.filterInputProps,ptCallbacks:Y,metaData:W}):null,rt=s({className:Y.cx("wrapper"),style:b.listStyle},Y.ptm("wrapper")),ot=s({ref:H,id:b.id,className:Y.cx("root"),style:b.style},T.getOtherProps(b),Y.ptm("root")),it=s({ref:I,role:"presentation","aria-hidden":"true",className:"p-hidden-accessible p-hidden-focusable",tabIndex:b.disabled?-1:b.tabIndex,onFocus:function(){f.focus(X.current);var e=f.getFirstFocusableElement(H.current,':not([data-p-hidden-focusable="true"])');E.current.tabIndex=f.isElement(e)?void 0:-1,I.current.tabIndex=-1,he(null,0)},"data-p-hidden-accessible":!0,"data-p-hidden-focusable":!0},Y.ptm("hiddenFirstFocusableEl")),lt=s({ref:E,role:"presentation","aria-hidden":"true",className:"p-hidden-accessible p-hidden-focusable",tabIndex:b.disabled?-1:b.tabIndex,onFocus:function(e){if(e.relatedTarget===X.current){var t=f.getFirstFocusableElement(H.current,':not([data-p-hidden-focusable="true"])');f.focus(t),I.current.tabIndex=void 0}else f.focus(I.current);E.current.tabIndex=-1},"data-p-hidden-accessible":!0,"data-p-hidden-focusable":!0},Y.ptm("hiddenLastFocusableEl"));return e.createElement(e.Fragment,null,e.createElement("div",ot,e.createElement("span",it),nt,e.createElement("div",rt,tt),e.createElement("span",lt)),Qe&&e.createElement(u,h({target:H,content:b.tooltip,pt:Y.ptm("tooltip")},b.tooltipOptions)))})));H.displayName="ListBox";export{H as ListBox};