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