import{defineComponent as ne,computed as se,reactive as le,watch as H,toRaw as ie,nextTick as R,createVNode as c,Fragment as ae}from"vue";import a from"../_util/KeyCode.mjs";import z from"../_util/classNames.mjs";import re from"../_util/createRef.mjs";import ce from"../_util/hooks/useMemo.mjs";import ue from"../_util/omit.mjs";import fe from"../_util/pickAttrs.mjs";import{isValidElement as pe}from"../_util/props-util/index.mjs";import de from"../vc-virtual-list/List.mjs";import me from"./SelectContext.mjs";import ve from"./TransBtn.mjs";import be from"./hooks/useBaseProps.mjs";import{isPlatformMac as ge}from"./utils/platformUtil.mjs";function B(V){return typeof V=="string"||typeof V=="number"}const De=ne({compatConfig:{MODE:3},name:"OptionList",inheritAttrs:!1,setup(V,{expose:U,slots:W}){const n=be(),l=me(),f=se(()=>`${n.prefixCls}-item`),r=ce(()=>l.flattenOptions,[()=>n.open,()=>l.flattenOptions],e=>e[0]),w=re(),D=e=>{e.preventDefault()},k=e=>{w.current&&w.current.scrollTo(typeof e=="number"?{index:e}:e)},C=(e,o=1)=>{const s=r.value.length;for(let t=0;t<s;t+=1){const i=(e+t*o+s)%s,{group:v,data:p}=r.value[i];if(!v&&!p.disabled)return i}return-1},P=le({activeIndex:C(0)}),I=(e,o=!1)=>{P.activeIndex=e;const s={source:o?"keyboard":"mouse"},t=r.value[e];if(!t){l.onActiveValue(null,-1,s);return}l.onActiveValue(t.value,e,s)};H([()=>r.value.length,()=>n.searchValue],()=>{I(l.defaultActiveFirstOption!==!1?C(0):-1)},{immediate:!0});const K=e=>l.rawValues.has(e)&&n.mode!=="combobox";H([()=>n.open,()=>n.searchValue],()=>{if(!n.multiple&&n.open&&l.rawValues.size===1){const e=Array.from(l.rawValues)[0],o=ie(r.value).findIndex(({data:s})=>s[l.fieldNames.value]===e);o!==-1&&(I(o),R(()=>{k(o)}))}n.open&&R(()=>{w.current?.scrollTo(void 0)})},{immediate:!0,flush:"post"});const N=e=>{e!==void 0&&l.onSelect(e,{selected:!l.rawValues.has(e)}),n.multiple||n.toggleOpen(!1)},E=e=>typeof e.label=="function"?e.label():e.label;function T(e){const o=r.value[e];if(!o)return null;const s=o.data||{},{value:t}=s,{group:i}=o,v=fe(s,!0),p=E(o);return o?c("div",{"aria-label":typeof p=="string"&&!i?p:null,...v,key:e,role:i?"presentation":"option",id:`${n.id}_list_${e}`,"aria-selected":K(t)},[t]):null}return U({onKeydown:e=>{const{which:o,ctrlKey:s}=e;switch(o){case a.N:case a.P:case a.UP:case a.DOWN:{let t=0;if(o===a.UP?t=-1:o===a.DOWN?t=1:ge()&&s&&(o===a.N?t=1:o===a.P&&(t=-1)),t!==0){const i=C(P.activeIndex+t,t);k(i),I(i,!0)}break}case a.ENTER:{const t=r.value[P.activeIndex];t&&!t.data.disabled?N(t.value):N(void 0),n.open&&e.preventDefault();break}case a.ESC:n.toggleOpen(!1),n.open&&e.stopPropagation()}},onKeyup:()=>{},scrollTo:e=>{k(e)}}),()=>{const{id:e,notFoundContent:o,onPopupScroll:s}=n,{menuItemSelectedIcon:t,fieldNames:i,virtual:v,listHeight:p,listItemHeight:j}=l,O=W.option,{activeIndex:b}=P,q=Object.keys(i).map(u=>i[u]);return r.value.length===0?c("div",{role:"listbox",id:`${e}_list`,class:`${f.value}-empty`,onMousedown:D},[o]):c(ae,null,[c("div",{role:"listbox",id:`${e}_list`,style:{height:0,width:0,overflow:"hidden"}},[T(b-1),T(b),T(b+1)]),c(de,{itemKey:"key",ref:w,data:r.value,height:p,itemHeight:j,fullHeight:!1,onMousedown:D,onScroll:s,virtual:v},{default:(u,S)=>{const{group:G,groupOption:J,data:g,value:A}=u,{key:Q}=g,$=typeof u.label=="function"?u.label():u.label;if(G){const h=g.title??(B($)&&$);return c("div",{class:z(f.value,`${f.value}-group`),title:h},[O?O(g):$!==void 0?$:Q])}const{disabled:x,title:F,children:Pe,style:X,class:Y,className:Z,...y}=g,ee=ue(y,q),d=K(A),m=`${f.value}-option`,te=z(f.value,m,Y,Z,{[`${m}-grouped`]:J,[`${m}-active`]:b===S&&!x,[`${m}-disabled`]:x,[`${m}-selected`]:d}),M=E(u),oe=!t||typeof t=="function"||d,L=typeof M=="number"?M:M||A;let _=B(L)?L.toString():void 0;return F!==void 0&&(_=F),c("div",{...ee,"aria-selected":d,class:te,title:_,onMousemove:h=>{y.onMousemove&&y.onMousemove(h),!(b===S||x)&&I(S)},onClick:h=>{x||N(A),y.onClick&&y.onClick(h)},style:X},[c("div",{class:`${m}-content`},[O?O(g):L]),pe(t)||d,oe&&c(ve,{class:`${f.value}-option-state`,customizeIcon:t,customizeIconProps:{isSelected:d}},{default:()=>[d?"✓":null]})])}})])}}});export{De as default};
