import{jsxs as b,jsx as o}from"react/jsx-runtime";import{token as C}from"@styled/tokens";import U from"../icons/ico-caret-down.js";import Y from"../icons/ico-caret-up.js";import G from"../icons/ico-checkmark.js";import J from"../icons/ico-question-circle-fill.js";import{createContext as Q,useContext as X,useCallback as S,useState as W,useRef as A,useMemo as Z,Children as m,useEffect as $,cloneElement as ee}from"react";import{HStack as D,Box as N,VStack as te}from"@styled/jsx";import{css as B}from"@styled/css";import"../feedback/callout.js";import"../feedback/progress-bar.js";import"./button.js";import"./checkbox.js";import"./popover.js";import"./radio.js";import"./segmented-control.js";import"./switch.js";import O from"./text.js";import"./portal.js";import"../../hooks/useToast.js";import"../../recipes/toast.js";import re from"../feedback/tooltip.js";import{useButton as oe,useFocusRing as ne,useKeyboard as le,mergeProps as ie}from"react-aria";const F={sm:{caret:16,check:14,height:"2.625rem",top:48,width:"0.75rem",mult:2.25},md:{caret:17,check:15,height:"2.8125rem",top:52,width:"0.85rem",mult:2.25},lg:{caret:18,check:16,height:"3rem",top:55,width:"1rem",mult:2.5}},R=Q({selectedOption:null,setSelectedOption:()=>{}}),_=({value:r,label:s,size:u="lg",isFocused:y})=>{const{selectedOption:d,setSelectedOption:p}=X(R),i=d?.value===r,n=F[u],x=S(()=>{p({value:r,label:s})},[p,r,s]);return b(D,{py:1.5,gap:2,bg:i?"brand.base":y?"brand.lightest":"",_hover:i?{}:{bg:"brand.lightest"},onClick:x,style:{paddingLeft:n.width,paddingRight:n.width},children:[o(N,{style:{minWidth:n.width},children:i&&o(G,{width:n.check,height:n.check,color:C("colors.text.quaternary")})}),o(O,{size:u,fontWeight:"medium",fontColor:i?"text.quaternary":"text.primary",styles:{textAlign:"left"},children:s})]})},j=({children:r,onSelect:s,label:u,placeholder:y="Select one",selectedValue:d,size:p="lg",tooltipContent:i,disabled:n,viewMax:x=5,...q})=>{const[h,g]=W(!1),[t,f]=W(null),v=A(null),k=A(null),a=F[p],H=x*a.mult+1,I=S(()=>{g(e=>!e),h||(f(null),setTimeout(()=>{var e;return(e=k.current)===null||e===void 0?void 0:e.focus()}))},[h]),K=S(e=>{s(e.value),g(!1)},[s]),E=Z(()=>{const e=m.toArray(r).find(l=>l.props.value===d);return e?e.props.label:y},[d,r]),{buttonProps:L}=oe({...q,isDisabled:n,onPress:I},v),{focusProps:V,isFocusVisible:M}=ne(),{keyboardProps:T}=le({onKeyDown:e=>{var l,c;if(!h)return;let w=t;const z=m.toArray(r);switch(e.key){case"ArrowUp":e.preventDefault(),t===null?f(m.count(r)-1):(w=t>0?t-1:m.count(r)-1,f(w));break;case"ArrowDown":e.preventDefault(),t===null?f(0):(w=t<m.count(r)-1?t+1:0,f(w));break;case"Enter":if(e.preventDefault(),(l=v.current)===null||l===void 0||l.focus(),t===null)return;if(t>=0&&t<z.length){const P=z[t];P&&s(P.props.value)}break;case"Escape":g(!1),(c=v.current)===null||c===void 0||c.focus();break}}});return $(()=>{const e=l=>{var c;!((c=k.current)===null||c===void 0)&&c.contains(l.target)||g(!1)};return document.addEventListener("mousedown",e),()=>{document.removeEventListener("mousedown",e)}},[]),o(R.Provider,{value:{selectedOption:{value:d??"",label:E},setSelectedOption:K},children:b(te,{gap:2,w:"full",alignItems:"flex-start",children:[(u||i)&&b(D,{gap:2,pointerEvents:n?"none":"auto",opacity:n?.3:"",transition:"all linear 120ms",children:[u&&o(O,{size:"sm",fontWeight:"medium",children:u}),i&&o(re,{content:i,children:o(J,{className:B({w:4,h:4,color:"neutral.primary"})})})]}),b("button",{className:B({w:"full",h:"fit-content",bg:"surface.primary",borderWidth:"thin",borderColor:"neutral.secondary",rounded:"xl",transition:"all linear 120ms",position:"relative",cursor:"pointer",outlineColor:"brand.base",outlineStyle:M?"solid":"none",outlineWidth:"thick",outlineOffset:.5,_hover:{borderColor:"neutral.primary"},_disabled:{opacity:.3,pointerEvents:"none"}}),ref:v,...ie(L,V,T),children:[b(D,{w:"full",p:4,justifyContent:"space-between",style:{height:a.height},children:[o(O,{truncate:!0,size:p,fontColor:d?"text.primary":"text.tertiary",children:E}),h?o(Y,{width:a.caret,height:a.caret,color:C("colors.brand.base")}):o(U,{width:a.caret,height:a.caret,color:C("colors.brand.base")})]}),h&&o(N,{ref:k,tabIndex:-1,py:2,bg:"surface.primary",position:"absolute",w:"full",rounded:"input",boxShadow:"4px 8px 20px 0px rgba(0, 0, 0, 0.15)",style:{maxHeight:`${H}rem`,top:a.top},overflowY:"auto",outline:"none",zIndex:"max",children:m.map(r,(e,l)=>ee(e,{isFocused:l===t,size:p}))})]})]})})};_.displayName="DropdownOption",j.displayName="DropdownSelector";export{_ as DropdownOption,j as DropdownSelector};
//# sourceMappingURL=dropdown-selector.js.map
