import{jsxs as b,jsx as o}from"react/jsx-runtime";import V from"../icons/ico-caret-down.js";import B from"../icons/ico-caret-up.js";import U from"../icons/ico-checkmark.js";import G from"../icons/ico-question-circle-fill.js";import{createContext as J,useContext as Q,useCallback as N,useState as A,useRef as F,useMemo as X,Children as m,useEffect as Y,cloneElement as Z}from"react";import{cn as S}from"../../lib/cn.js";import"../feedback/callout.js";import"./button.js";import"./checkbox.js";import"./popover.js";import"./radio.js";import"./segmented-control.js";import"./switch.js";import z from"./text.js";import"./portal.js";import"../../hooks/useToast.js";import $ from"../feedback/tooltip.js";import{useButton as ee,useFocusRing as te,useKeyboard as re,mergeProps as oe}from"react-aria";const _={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}},j=J({selectedOption:null,setSelectedOption:()=>{}}),q=({value:r,label:i,size:u="lg",isFocused:C,optionHoverColor:d})=>{const{selectedOption:h,setSelectedOption:p}=Q(j),l=h?.value===r,a=_[u],g=N(()=>{p({value:r,label:i})},[p,r,i]);return b("div",{className:S("flex flex-row items-center py-1.5 gap-2",l?"bg-brand-base":C?"bg-brand-lightest":"",!l&&`hover:bg-${d||"brand-lightest"}`),role:"option",tabIndex:0,"aria-selected":l,onClick:g,onKeyDown:y=>{(y.key==="Enter"||y.key===" ")&&g()},style:{paddingLeft:a.width,paddingRight:a.width},children:[o("div",{style:{minWidth:a.width},children:l&&o(U,{width:a.check,height:a.check,color:"var(--color-text-quaternary)"})}),o(z,{size:u,fontWeight:"medium",fontColor:l?"text.quaternary":"text.primary",styles:{textAlign:"left"},children:i})]})},H=({children:r,onSelect:i,label:u,placeholder:C="Select one",selectedValue:d,size:h="lg",tooltipContent:p,disabled:l,viewMax:a=5,optionHoverColor:g,...y})=>{const[f,w]=A(!1),[t,v]=A(null),x=F(null),D=F(null),s=_[h],K=a*s.mult+1,L=N(()=>{w(e=>!e),f||(v(null),setTimeout(()=>{var e;return(e=D.current)===null||e===void 0?void 0:e.focus()}))},[f]),R=N(e=>{i(e.value),w(!1)},[i]),O=X(()=>{const e=m.toArray(r).find(n=>n.props.value===d);return e?e.props.label:C},[d,r]),{buttonProps:W}=ee({...y,isDisabled:l,onPress:L},x),{focusProps:I,isFocusVisible:M}=te(),{keyboardProps:T}=re({onKeyDown:e=>{var n,c;if(!f)return;let k=t;const E=m.toArray(r);switch(e.key){case"ArrowUp":e.preventDefault(),t===null?v(m.count(r)-1):(k=t>0?t-1:m.count(r)-1,v(k));break;case"ArrowDown":e.preventDefault(),t===null?v(0):(k=t<m.count(r)-1?t+1:0,v(k));break;case"Enter":if(e.preventDefault(),(n=x.current)===null||n===void 0||n.focus(),t===null)return;if(t>=0&&t<E.length){const P=E[t];P&&i(P.props.value)}break;case"Escape":w(!1),(c=x.current)===null||c===void 0||c.focus();break}}});return Y(()=>{const e=n=>{var c;!((c=D.current)===null||c===void 0)&&c.contains(n.target)||w(!1)};return document.addEventListener("mousedown",e),()=>{document.removeEventListener("mousedown",e)}},[]),o(j.Provider,{value:{selectedOption:{value:d??"",label:O},setSelectedOption:R},children:b("div",{className:"flex flex-col gap-2 w-full items-start",children:[(u||p)&&b("div",{className:S("flex flex-row items-center gap-2 transition-all duration-[120ms] linear",l?"pointer-events-none opacity-30":"pointer-events-auto"),children:[u&&o(z,{size:"sm",fontWeight:"medium",children:u}),p&&o($,{content:p,children:o(G,{className:"w-4 h-4 text-neutral-primary"})})]}),b("button",{className:S("w-full h-fit bg-surface-primary border border-neutral-secondary rounded-xl transition-all duration-[120ms] linear relative cursor-pointer hover:border-neutral-primary disabled:opacity-30 disabled:pointer-events-none outline-offset-[0.125rem]",M?"outline outline-brand-base outline-thick":"outline-none"),ref:x,...oe(W,I,T),children:[b("div",{className:"flex flex-row items-center w-full p-4 justify-between",style:{height:s.height},children:[o(z,{truncate:!0,size:h,fontColor:d?"text.primary":"text.tertiary",children:O}),f?o(B,{width:s.caret,height:s.caret,color:"var(--color-brand-base)"}):o(V,{width:s.caret,height:s.caret,color:"var(--color-brand-base)"})]}),f&&o("div",{ref:D,tabIndex:-1,className:"py-2 bg-surface-primary absolute w-full rounded-input shadow-[4px_8px_20px_0px_rgba(0,0,0,0.15)] overflow-y-auto outline-none z-max",style:{maxHeight:`${K}rem`,top:s.top},children:m.map(r,(e,n)=>Z(e,{isFocused:n===t,size:h,optionHoverColor:g}))})]})]})})};q.displayName="DropdownOption",H.displayName="DropdownSelector";export{q as DropdownOption,H as DropdownSelector};
//# sourceMappingURL=dropdown-selector.js.map
