"use strict";const r=require("react/jsx-runtime"),y=require("react"),B=require("@mui/material/Select"),q=require("@mui/material/MenuItem"),K=require("@mui/material/FormControl"),U=require("@mui/material/InputLabel"),V=require("@mui/material/FormHelperText"),G=require("@mui/material/ListItemIcon"),_=require("@mui/material/ListItemText"),J=require("@mui/material/CircularProgress"),Q=require("@mui/material/OutlinedInput"),X=require("@mui/material/InputBase"),Y=require("@mui/material/Chip"),f=require("@mui/material/Box"),Z=require("@mui/icons-material/Search"),$=require("@mui/icons-material/Clear"),b=require("./types.cjs");function L({options:x,value:g,onChange:l,size:h="md",color:z="neutral",label:c,helperText:P,placeholder:s,searchable:d=!1,multiple:v=!1,loading:w=!1,error:p,fullWidth:T,disabled:E,clearable:M=!1,sx:H}){const[m,C]=y.useState(""),[A,k]=y.useState(!1),[i,a]=y.useState(-1),O=b.toMuiInputSize(h),I=b.toMuiColor(z),F=typeof p=="string"?!!p:p,D=typeof p=="string"?p:void 0,j=()=>{k(!1),a(-1),d&&C("")},o=y.useMemo(()=>{if(!d||!m)return x;const e=m.toLowerCase();return x.filter(t=>t.label.toLowerCase().includes(e))},[x,m,d]),R=e=>{l(e.target.value)},W=e=>{if(e.key==="Escape"){j();return}if(e.key==="ArrowDown"){if(e.preventDefault(),e.stopPropagation(),o.length===0)return;const t=i+1;a(t>=o.length?0:t);return}if(e.key==="ArrowUp"){e.preventDefault(),e.stopPropagation();const t=i-1;a(t<0?o.length-1:t);return}if(e.key==="Tab"){if(e.preventDefault(),e.stopPropagation(),o.length===0)return;const t=i+1;a(t>=o.length?0:t);return}if(e.key==="Enter"&&i>=0&&i<o.length){e.preventDefault(),e.stopPropagation();const t=o[i];if(!t.disabled)if(v){const n=Array.isArray(g)?g:[],S=n.includes(t.value)?n.filter(u=>u!==t.value):[...n,t.value];l(S)}else l(t.value),j();return}e.stopPropagation()};return r.jsxs(K,{size:O,fullWidth:T,error:F,disabled:E,style:{"--ov-input-height":b.INPUT_HEIGHTS[h]},sx:H,children:[c&&r.jsx(U,{color:I==="default"||I==="inherit"?void 0:I,shrink:!!s||void 0,children:c}),r.jsxs(B,{value:g,onChange:R,multiple:v,displayEmpty:!!s,label:c,notched:c&&!!s||void 0,input:v?r.jsx(Q,{label:c}):void 0,open:A,onOpen:()=>{k(!0),a(-1)},onClose:j,renderValue:v?e=>{const t=e;return t.length===0&&s?r.jsx(f,{sx:{color:"var(--ov-fg-faint)"},children:s}):r.jsxs(f,{sx:{display:"flex",flexWrap:"nowrap",gap:.5,overflow:"hidden",alignItems:"center"},children:[t.map(n=>{const S=x.find(u=>u.value===n);return r.jsx(Y,{label:S?.label??n,size:"small",...M?{onDelete:u=>{u.stopPropagation(),l(t.filter(N=>N!==n))},onMouseDown:u=>{u.stopPropagation()}}:{}},n)}),M&&t.length>1&&r.jsx("button",{type:"button","aria-label":"Clear all",onClick:n=>{n.stopPropagation(),l([])},style:{display:"inline-flex",alignItems:"center",flexShrink:0,marginLeft:2,cursor:"pointer",color:"var(--ov-fg-faint)",lineHeight:0,background:"none",border:"none",padding:0},children:r.jsx($,{sx:{fontSize:16}})})]})}:s&&!g?()=>r.jsx(f,{sx:{color:"var(--ov-fg-faint)"},children:s}):void 0,MenuProps:{PaperProps:{sx:{maxHeight:300,...h==="xs"||h==="sm"?{"& .MuiMenuItem-root":{fontSize:"0.8rem",minHeight:28,padding:"3px 8px"},"& .MuiListItemText-root":{margin:0},"& .MuiListItemText-primary":{fontSize:"0.8rem"},"& .MuiListItemIcon-root":{minWidth:22}}:{}}}},children:[d&&r.jsx(f,{sx:{px:1,pb:.5,pt:.5},onClickCapture:e=>e.stopPropagation(),onMouseDown:e=>e.stopPropagation(),children:r.jsxs(f,{sx:{display:"flex",alignItems:"center",height:28,border:"1px solid var(--ov-border-default)",borderRadius:"4px",bgcolor:"var(--ov-bg-base)",px:.75,"&:focus-within":{borderColor:"var(--ov-accent)"}},children:[r.jsx(Z,{sx:{fontSize:14,color:"var(--ov-fg-faint)",mr:.5}}),r.jsx(X,{autoFocus:!0,fullWidth:!0,placeholder:"Search...",value:m,onChange:e=>{C(e.target.value),a(-1)},onKeyDown:W,sx:{flex:1,fontSize:"0.75rem",color:"var(--ov-fg-default)","& input":{py:0,px:0},"& input::placeholder":{color:"var(--ov-fg-faint)",opacity:1}}})]})}),w&&r.jsxs(q,{disabled:!0,children:[r.jsx(J,{size:16,sx:{mr:1}})," Loading..."]}),o.map((e,t)=>r.jsxs(q,{value:e.value,disabled:e.disabled,...d?{onMouseEnter:()=>a(t),sx:t===i?{bgcolor:"action.focus"}:void 0}:{},children:[e.icon&&r.jsx(G,{sx:{minWidth:28},children:e.icon}),r.jsx(_,{children:e.label})]},e.value)),o.length===0&&!w&&r.jsx(q,{disabled:!0,children:"No options"})]}),(D||P)&&r.jsx(V,{children:D??P})]})}L.displayName="Select";exports.Select=L;
