import{n as T,r,z as W,b7 as I,c2 as V,c3 as O,C as $,j as a,bq as H,bG as L,br as N,b8 as P,bs as k,c1 as B,c4 as E,bZ as F,c5 as A,bC as D}from"./index.BEHais2d.js";import{a as G}from"./useBasicWidgetState.DxtD7xs-.js";import"./FormClearHelper.CpsBciie.js";const U=T("div",{target:"euzcfsp0"})(({theme:s})=>({"span[aria-disabled='true']":{background:s.colors.fadedText05}})),j=(s,t)=>s.getStringArrayValue(t),K=s=>s.default.map(t=>s.options[t])??null,Y=s=>s.rawValues??null,_=(s,t,l,u)=>{t.setStringArrayValue(s,l.value,{fromUi:l.fromUi},u)},q=s=>{var S;const{element:t,widgetMgr:l,fragmentId:u}=s,e=W(),f=r.useContext(I),[i,m]=G({getStateFromWidgetMgr:j,getDefaultStateFromProto:K,getCurrStateFromProto:Y,updateWidgetMgrState:_,element:t,widgetMgr:l,fragmentId:u}),h=t.maxSelections>0&&i.length>=t.maxSelections,y=r.useMemo(()=>{if(t.maxSelections===0)return"No results";if(i.length===t.maxSelections){const o=t.maxSelections!==1?"options":"option";return`You can only select up to ${t.maxSelections} ${o}. Remove an option first.`}return"No results"},[t.maxSelections,i.length]),x=r.useMemo(()=>i.map(o=>({value:o,label:o})),[i]),b=r.useCallback(o=>{var n,d;switch(o.type){case"remove":return V(i,(n=o.option)==null?void 0:n.value);case"clear":return[];case"select":return i.concat([(d=o.option)==null?void 0:d.value]);default:throw new Error(`State transition is unknown: ${o.type}`)}},[i]),v=r.useCallback(o=>{t.maxSelections&&o.type==="select"&&i.length>=t.maxSelections||m({value:b(o),fromUi:!0})},[t.maxSelections,b,m,i.length]),z=r.useCallback((o,n)=>{if(h)return[];const d=o.filter(R=>!i.includes(R.value));return O(d,n)},[h,i]),{options:p}=t;let c=s.disabled,g=t.placeholder;p.length===0&&(t.acceptNewOptions?g="Add options":(g="No options to select",c=!0));const w=p.map((o,n)=>({label:o,value:o,id:`${o}_${n}`})),M=p.length>10,C=r.useMemo(()=>{const o=e.fontSizes.baseFontSize*1.6+14;return`${Math.round(o*4.25)}px`},[e.fontSizes.baseFontSize]);return $("div",{className:"stMultiSelect","data-testid":"stMultiSelect",children:[a(k,{label:t.label,disabled:c,labelVisibility:H((S=t.labelVisibility)==null?void 0:S.value),children:t.help&&a(L,{children:a(N,{content:t.help,placement:P.TOP_RIGHT})})}),a(U,{children:a(B,{creatable:t.acceptNewOptions??!1,options:w,labelKey:"label",valueKey:"value","aria-label":t.label,placeholder:g,type:A.select,multi:!0,onChange:v,value:x,disabled:c,size:"compact",noResultsMsg:y,filterOptions:z,closeOnSelect:!1,ignoreCase:!1,overrides:{Popover:{props:{ignoreBoundary:f,overrides:{Body:{style:()=>({marginTop:e.spacing.px})}}}},SelectArrow:{component:F,props:{style:{cursor:"pointer"},overrides:{Svg:{style:()=>({width:e.iconSizes.xl,height:e.iconSizes.xl})}}}},IconsContainer:{style:()=>({paddingRight:e.spacing.sm})},ControlContainer:{style:{maxHeight:C,minHeight:e.sizes.minElementHeight,borderLeftWidth:e.sizes.borderWidth,borderRightWidth:e.sizes.borderWidth,borderTopWidth:e.sizes.borderWidth,borderBottomWidth:e.sizes.borderWidth}},Placeholder:{style:()=>({flex:"inherit",color:c?e.colors.fadedText40:e.colors.fadedText60})},ValueContainer:{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.darkGray,padding:e.spacing.threeXS,height:e.sizes.clearIconSize,width:e.sizes.clearIconSize,cursor:"pointer",":hover":{fill:e.colors.bodyText}}}}}},SearchIcon:{style:{color:e.colors.darkGray}},Tag:{props:{overrides:{Root:{style:{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:D()&&M===!1?"readonly":null}},Dropdown:{component:E}}})})]})},Q=r.memo(q);export{Q as default};
