import{u as X,h as C,L as H,M as Y}from"./OdysseyThemeProvider-Bh0Bu-2o.js";import{r}from"./index-NdkWRN8K.js";import{M as Z}from"./Field-DH3L8v09.js";import{g as _,u as ee,C as ne}from"./inputUtils-Dbi8XkA6.js";import{j as t}from"./jsx-runtime-CnH95YSl.js";import{C as te}from"./Chip-CXnMieIl.js";import{L as se}from"./ListSubheader-CQJAAOsd.js";import{M as re}from"./MenuItem-vhp8G-Nb.js";import{C as ae}from"./Checkbox-vZwuXtzm.js";import{L as le}from"./ListItem-C0ExFtXb.js";import{S as ie}from"./Select-DZTigpTO.js";import{B as oe}from"./Box-BcMTQ_R2.js";/*!
 * Copyright (c) 2024-present, Okta, Inc. and/or its affiliates. All rights reserved.
 * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
 *
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */const ce=(...s)=>{const c="-";return s.join(c).replace(/\W+/g,c).toLowerCase()},ue=C.div`
  position: relative;
  width: 100%;
  display: flex;
`,de=C("div",{shouldForwardProp:s=>s!=="odysseyDesignTokens"})(({odysseyDesignTokens:s})=>({alignItems:"center",display:"flex",insetBlockEnd:0,insetBlockStart:0,insetInlineEnd:"25px",insetInlineStart:"4px",marginInlineStart:s.BorderWidthMain,opacity:1,pointerEvents:"none",position:"absolute"})),me=C(H,{shouldForwardProp:s=>s!=="odysseyDesignTokens"})(({odysseyDesignTokens:s})=>({fontSize:"1em",marginBlockEnd:"-1px",marginInlineEnd:`-${s.Spacing1}`,marginInlineStart:s.Spacing2})),pe=C(oe,{shouldForwardProp:s=>s!=="odysseyDesignTokens"&&s!=="isInteractive"&&s!=="isReadOnly"})(({isInteractive:s,isReadOnly:c,odysseyDesignTokens:p})=>({display:"flex",flexWrap:"wrap",gap:p.Spacing1,marginBlock:`-${p.Spacing2}`,marginInlineEnd:p.Spacing1,minHeight:p.Spacing6,opacity:s||c?1:0,pointerEvents:"none"})),{CONTROLLED:x}=ne,fe=({ariaDescribedBy:s,defaultValue:c,errorMessage:p,errorMessageList:N,hasMultipleChoices:I,hint:W,HintLinkComponent:P,id:$,inputRef:K,isDisabled:j=!1,isFullWidth:q=!1,isMultiSelect:M,isOptional:G=!1,isReadOnly:u=!1,label:J,name:b,onBlur:k,onChange:h,onFocus:E,options:L,testId:w,translate:z,value:f})=>{const Q=r.useRef(null),a=r.useMemo(()=>I===void 0?M:I,[I,M]),d=r.useRef(_({controlledValue:f,uncontrolledValue:c})),[l,F]=r.useState(d.current===x?f:c),v=r.useRef(null),g=X();r.useImperativeHandle(K,()=>({focus:()=>{var e;(e=v.current)==null||e.focus()}}),[]),r.useEffect(()=>{d.current===x&&F(f)},[f]);const A=ee({defaultValue:c,value:f,controlState:d.current}),y=r.useCallback((e,o)=>{if(u)e.preventDefault();else{const{target:{value:n}}=e;d.current!==x&&F(typeof n=="string"&&a?n.split(","):n),h==null||h(e,o)}},[a,h,u]),[B,D]=r.useMemo(()=>{const e=L.map(n=>{if(typeof n=="object"){const m=(n==null?void 0:n.value)===""?n.value:n.value||n.text;return{text:n.text,value:m,type:n.type==="heading"?"heading":"option",lang:n.language}}return{text:n,value:n,type:"option"}}),o=new Map(e.map(n=>[n.value,n]));return[e,o]},[L]),V=r.useCallback(e=>{if(Array.isArray(l)){const n={target:{value:l.filter(m=>m!==e)}};y(n,null)}},[l,y]),S=r.useCallback(({isInteractive:e,isReadOnly:o})=>{const n=i=>i.stopPropagation(),m=!e&&!o&&d.current===x&&a;return Array.isArray(l)&&t.jsx(pe,{isInteractive:e,isReadOnly:o,odysseyDesignTokens:g,children:l.map(i=>{var O;return(i==null?void 0:i.length)>0&&t.jsx(te,{deleteIcon:t.jsx(H,{onMouseDown:n,sx:{pointerEvents:"auto"}}),label:t.jsxs(t.Fragment,{children:[(O=D.get(i))==null?void 0:O.text,m&&t.jsx(me,{odysseyDesignTokens:g})]}),onDelete:e&&d.current===x?()=>V(i):void 0,tabIndex:-1},i)})})},[d,a,l,g,V,D]),R=r.useMemo(()=>B.map((e,o)=>{if(e.type==="heading")return t.jsxs(se,{children:[" ",e.text," "]},e.text);const n=a?l==null?void 0:l.includes(e.value):l===e.value;return t.jsxs(re,{"data-empty":!e.text,lang:e.lang,selected:n,value:e.value,children:[a&&t.jsx(ae,{checked:n}),e.text,!a&&l===e.value&&t.jsx(le,{children:t.jsx(Y,{})})]},ce(e.text,o.toString()))}),[a,B,l]),T=r.useCallback(e=>Array.isArray(e)&&t.jsx(S,{isInteractive:!1}),[S]),U=r.useCallback(({ariaDescribedBy:e,errorMessageElementId:o,id:n,labelElementId:m})=>t.jsxs(ue,{children:[t.jsx(ie,{...A,"aria-describedby":e,"aria-errormessage":o,displayEmpty:!0,id:n,inputProps:{"data-se":w,"aria-disabled":j||u,readOnly:u},inputRef:i=>{v.current!==i&&(v.current=i),Q.current=i},labelId:m,MenuProps:{sx:{".MuiPaper-root":{maxHeight:"50vh"}}},multiple:a,name:b??n,onBlur:k,onChange:y,onFocus:E,renderValue:a?T:void 0,translate:z,children:R}),a&&t.jsx(t.Fragment,{children:t.jsx(de,{odysseyDesignTokens:g,children:t.jsx(S,{isInteractive:!u,isReadOnly:u})})})]}),[S,A,a,j,u,b,g,k,y,E,R,T,w,z]);return t.jsx(Z,{ariaDescribedBy:s,errorMessage:p,errorMessageList:N,fieldType:"single",hasVisibleLabel:!0,hint:W,HintLinkComponent:P,id:$,isDisabled:j,isFullWidth:q,isOptional:G,label:J,renderFieldComponent:U})},ge=r.memo(fe);ge.displayName="Select";export{ge as M};
