import{aR as L,aS as E,aT as O,aU as A,z as B,r as n,l as H,az as k,aV as N,j as h,aW as $,o as F,p as D,ab as P,ac as U,aX as Y,aY as j,av as K,aZ as X,ae as Z}from"./index.Drusyo5m.js";import{u as _}from"./useBasicWidgetState.DFklfao0.js";import{a as q}from"./_arrayIncludes.B19Iyn2B.js";import"./FormClearHelper.C__r5Llk.js";import"./_baseIndexOf.BTknn6Gb.js";var G=200;function J(o,t,d,u){var e=-1,m=q,l=!0,p=o.length,s=[],f=t.length;if(!p)return s;t.length>=G&&(m=E,l=!1,t=new L(t));e:for(;++e<p;){var i=o[e],c=i;if(i=i!==0?i:0,l&&c===c){for(var g=f;g--;)if(t[g]===c)continue e;s.push(i)}else m(t,c,u)||s.push(i)}return s}var Q=O(function(o,t){return A(o)?J(o,t):[]});const ee=B("div",{target:"ekqzr6e0"})(({theme:o})=>({"span[aria-disabled='true']":{background:o.colors.fadedText05}})),te=(o,t)=>o.getStringArrayValue(t),oe=o=>o.default.map(t=>o.options[t])??null,re=o=>o.rawValues??null,se=(o,t,d,u)=>{t.setStringArrayValue(o,d.value,{fromUi:d.fromUi},u)},ne=o=>{const{element:t,widgetMgr:d,fragmentId:u}=o,e=H(),m=n.useContext(k),l=n.useRef(null),p=n.useRef(0),[s,f]=_({getStateFromWidgetMgr:te,getDefaultStateFromProto:oe,getCurrStateFromProto:re,updateWidgetMgrState:se,element:t,widgetMgr:d,fragmentId:u}),i=t.maxSelections>0&&s.length>=t.maxSelections,c=n.useMemo(()=>{if(t.maxSelections===0)return"No results";if(s.length===t.maxSelections){const r=t.maxSelections!==1?"options":"option";return`You can only select up to ${t.maxSelections} ${r}. Remove an option first.`}return"No results"},[t.maxSelections,s.length]),g=n.useCallback(r=>{switch(r.type){case"remove":return Q(s,r.option?.value);case"clear":return[];case"select":return s.concat([r.option?.value]);default:throw new Error(`State transition is unknown: ${r.type}`)}},[s]),C=n.useCallback(r=>{t.maxSelections&&r.type==="select"&&s.length>=t.maxSelections||f({value:g(r),fromUi:!0})},[t.maxSelections,g,f,s.length]),{options:v}=t,{placeholder:R,disabled:z,selectOptions:w,inputReadOnly:T,valuesToUiMulti:b,createFilterOptions:y}=N({options:v,isMulti:!0,acceptNewOptions:t.acceptNewOptions??!1,placeholderInput:t.placeholder}),M=n.useCallback((r,a)=>i?[]:y(s)(r,a),[y,i,s]),S=o.disabled||z,W=n.useMemo(()=>b(s),[b,s]),V=n.useMemo(()=>{const r=e.fontSizes.baseFontSize*1.6+14;return`${Math.round(r*4.25)}px`},[e.fontSizes.baseFontSize]);n.useLayoutEffect(()=>{l.current&&(l.current.scrollTop=p.current)});const x=n.useCallback(r=>{p.current=r.currentTarget.scrollTop},[]),I=n.useMemo(()=>(function(a){return h($,{...a,ref:l,onScroll:x})}),[x]);return F("div",{className:"stMultiSelect","data-testid":"stMultiSelect",children:[h(U,{label:t.label,disabled:S,labelVisibility:D(t.labelVisibility?.value),children:t.help&&h(P,{content:t.help,label:t.label})}),h(ee,{children:h(Y,{creatable:t.acceptNewOptions??!1,options:w,labelKey:"label",valueKey:"value","aria-label":t.label,placeholder:R,type:X.select,multi:!0,onChange:C,value:W,disabled:S,size:"compact",noResultsMsg:c,filterOptions:M,closeOnSelect:!1,ignoreCase:!1,overrides:{Popover:{props:{ignoreBoundary:m,overrides:{Body:{style:()=>({marginTop:e.spacing.px})}}}},SelectArrow:{component:K,props:{style:{cursor:"pointer"},overrides:{Svg:{style:()=>({width:e.iconSizes.xl,height:e.iconSizes.xl})}}}},IconsContainer:{style:()=>({paddingRight:e.spacing.sm})},ControlContainer:{style:({$isFocused:r})=>{const a=Z(e.colors,r);return{maxHeight:V,minHeight:e.sizes.minElementHeight,borderLeftWidth:e.sizes.borderWidth,borderRightWidth:e.sizes.borderWidth,borderTopWidth:e.sizes.borderWidth,borderBottomWidth:e.sizes.borderWidth,borderTopColor:a,borderRightColor:a,borderBottomColor:a,borderLeftColor:a}}},Placeholder:{style:()=>({flex:"inherit",color:S?e.colors.fadedText40:e.colors.fadedText60})},ValueContainer:{component:I,style:()=>({overflowY:"auto",paddingLeft:e.spacing.sm,paddingTop:e.spacing.none,paddingBottom:e.spacing.none,paddingRight:e.spacing.none})},ClearIcon:{props:{overrides:{Svg:{style:{color:e.colors.grayTextColor,padding:e.spacing.threeXS,height:e.sizes.clearIconSize,width:e.sizes.clearIconSize,cursor:"pointer",":hover":{fill:e.colors.bodyText}}}}}},SearchIcon:{style:{color:e.colors.grayTextColor}},Tag:{props:{overrides:{Root:{style:{fontWeight:e.fontWeights.normal,borderTopLeftRadius:e.radii.md,borderTopRightRadius:e.radii.md,borderBottomRightRadius:e.radii.md,borderBottomLeftRadius:e.radii.md,fontSize:e.fontSizes.md,paddingLeft:e.spacing.sm,marginLeft:e.spacing.none,marginRight:e.spacing.sm,height:`calc(${e.sizes.minElementHeight} - 2 * ${e.spacing.xs})`,maxWidth:`calc(100% - ${e.spacing.lg})`,cursor:"default !important"}},Action:{style:{paddingLeft:0}},ActionIcon:{props:{overrides:{Svg:{style:{width:"0.625em",height:"0.625em"}}}}}}}},MultiValue:{props:{overrides:{Root:{style:{fontSize:e.fontSizes.sm}}}}},Input:{props:{readOnly:T}},Dropdown:{component:j}}})})]})},ue=n.memo(ne);export{ue as default};
