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