"use client";import{useFocusRing as Z}from"@react-aria/focus";import{useHover as ee}from"@react-aria/interactions";import _,{createContext as te,useCallback as ge,useContext as oe,useMemo as M,useReducer as Oe,useRef as W}from"react";import{useByComparator as Pe}from'../../hooks/use-by-comparator.js';import{useControllable as De}from'../../hooks/use-controllable.js';import{useDefaultValue as ve}from'../../hooks/use-default-value.js';import{useEvent as w}from'../../hooks/use-event.js';import{useId as B}from'../../hooks/use-id.js';import{useIsoMorphicEffect as re}from'../../hooks/use-iso-morphic-effect.js';import{useLatestValue as ne}from'../../hooks/use-latest-value.js';import{useSlot as V}from'../../hooks/use-slot.js';import{useSyncRefs as K}from'../../hooks/use-sync-refs.js';import{useDisabled as ie}from'../../internal/disabled.js';import{FormFields as Ae}from'../../internal/form-fields.js';import{useProvidedId as _e}from'../../internal/id.js';import{isDisabledReactIssue7711 as ae}from'../../utils/bugs.js';import{Focus as H,FocusResult as pe,focusIn as le,sortByDomNode as Ge}from'../../utils/focus-management.js';import{attemptSubmit as Ee}from'../../utils/form.js';import{match as xe}from'../../utils/match.js';import{isActiveElement as $}from'../../utils/owner.js';import{forwardRefWithAs as j,mergeProps as se,useRender as J}from'../../utils/render.js';import{Description as Ce,useDescribedBy as he,useDescriptions as de}from'../description/description.js';import{Keys as C}from'../keyboard.js';import{Label as Le,useLabelledBy as ke,useLabels as ue}from'../label/label.js';var Ie=(e=>(e[e.RegisterOption=0]="RegisterOption",e[e.UnregisterOption=1]="UnregisterOption",e))(Ie||{});let Fe={[0](o,t){let e=[...o.options,{id:t.id,element:t.element,propsRef:t.propsRef}];return{...o,options:Ge(e,n=>n.element.current)}},[1](o,t){let e=o.options.slice(),n=o.options.findIndex(P=>P.id===t.id);return n===-1?o:(e.splice(n,1),{...o,options:e})}},X=te(null);X.displayName="RadioGroupDataContext";function z(o){let t=oe(X);if(t===null){let e=new Error(`<${o} /> is missing a parent component.`);throw Error.captureStackTrace&&Error.captureStackTrace(e,z),e}return t}let q=te(null);q.displayName="RadioGroupActionsContext";function Q(o){let t=oe(q);if(t===null){let e=new Error(`<${o} /> is missing a parent component.`);throw Error.captureStackTrace&&Error.captureStackTrace(e,Q),e}return t}function Ue(o,t){return xe(t.type,Fe,o,t)}let we="div";function Se(o,t){let e=B(),n=ie(),{id:P=`headlessui-radiogroup-${e}`,value:R,form:D,name:i,onChange:c,by:d,disabled:a=n||!1,defaultValue:h,tabIndex:f=0,...L}=o,T=Pe(d),[v,y]=Oe(Ue,{options:[]}),p=v.options,[k,G]=ue(),[I,F]=de(),E=W(null),m=K(E,t),b=ve(h),[s,x]=De(R,c,b),g=M(()=>p.find(r=>!r.propsRef.current.disabled),[p]),O=M(()=>p.some(r=>T(r.propsRef.current.value,s)),[p,s]),l=w(r=>{var U;if(a||T(r,s))return!1;let S=(U=p.find(u=>T(u.propsRef.current.value,r)))==null?void 0:U.propsRef.current;return S!=null&&S.disabled?!1:(x==null||x(r),!0)}),ce=w(r=>{if(!E.current)return;let U=p.filter(u=>u.propsRef.current.disabled===!1).map(u=>u.element.current);switch(r.key){case C.Enter:Ee(r.currentTarget);break;case C.ArrowLeft:case C.ArrowUp:if(r.preventDefault(),r.stopPropagation(),le(U,H.Previous|H.WrapAround)===pe.Success){let A=p.find(N=>$(N.element.current));A&&l(A.propsRef.current.value)}break;case C.ArrowRight:case C.ArrowDown:if(r.preventDefault(),r.stopPropagation(),le(U,H.Next|H.WrapAround)===pe.Success){let A=p.find(N=>$(N.element.current));A&&l(A.propsRef.current.value)}break;case C.Space:{r.preventDefault(),r.stopPropagation();let u=p.find(A=>$(A.element.current));u&&l(u.propsRef.current.value)}break}}),Y=w(r=>(y({type:0,...r}),()=>y({type:1,id:r.id}))),fe=M(()=>({value:s,firstOption:g,containsCheckedOption:O,disabled:a,compare:T,tabIndex:f,...v}),[s,g,O,a,T,f,v]),Te=M(()=>({registerOption:Y,change:l}),[Y,l]),me={ref:m,id:P,role:"radiogroup","aria-labelledby":k,"aria-describedby":I,onKeyDown:ce},Re=V({value:s}),ye=ge(()=>{if(b!==void 0)return l(b)},[l,b]),be=J();return _.createElement(F,{name:"RadioGroup.Description"},_.createElement(G,{name:"RadioGroup.Label"},_.createElement(q.Provider,{value:Te},_.createElement(X.Provider,{value:fe},i!=null&&_.createElement(Ae,{disabled:a,data:{[i]:s||"on"},overrides:{type:"radio",checked:s!=null},form:D,onReset:ye}),be({ourProps:me,theirProps:L,slot:Re,defaultTag:we,name:"RadioGroup"})))))}let Me="div";function He(o,t){var g;let e=z("RadioGroup.Option"),n=Q("RadioGroup.Option"),P=B(),{id:R=`headlessui-radiogroup-option-${P}`,value:D,disabled:i=e.disabled||!1,autoFocus:c=!1,...d}=o,a=W(null),h=K(a,t),[f,L]=ue(),[T,v]=de(),y=ne({value:D,disabled:i});re(()=>n.registerOption({id:R,element:a,propsRef:y}),[R,n,a,y]);let p=w(O=>{var l;if(ae(O.currentTarget))return O.preventDefault();n.change(D)&&((l=a.current)==null||l.focus())}),k=((g=e.firstOption)==null?void 0:g.id)===R,{isFocusVisible:G,focusProps:I}=Z({autoFocus:c}),{isHovered:F,hoverProps:E}=ee({isDisabled:i}),m=e.compare(e.value,D),b=se({ref:h,id:R,role:"radio","aria-checked":m?"true":"false","aria-labelledby":f,"aria-describedby":T,"aria-disabled":i?!0:void 0,tabIndex:(()=>i?-1:m||!e.containsCheckedOption&&k?e.tabIndex:-1)(),onClick:i?void 0:p,autoFocus:c},I,E),s=V({checked:m,disabled:i,active:G,hover:F,focus:G,autofocus:c}),x=J();return _.createElement(v,{name:"RadioGroup.Description"},_.createElement(L,{name:"RadioGroup.Label"},x({ourProps:b,theirProps:d,slot:s,defaultTag:Me,name:"RadioGroup.Option"})))}let Ne="span";function We(o,t){var g;let e=z("Radio"),n=Q("Radio"),P=B(),R=_e(),D=ie(),{id:i=R||`headlessui-radio-${P}`,value:c,disabled:d=e.disabled||D||!1,autoFocus:a=!1,...h}=o,f=W(null),L=K(f,t),T=ke(),v=he(),y=ne({value:c,disabled:d});re(()=>n.registerOption({id:i,element:f,propsRef:y}),[i,n,f,y]);let p=w(O=>{var l;if(ae(O.currentTarget))return O.preventDefault();n.change(c)&&((l=f.current)==null||l.focus())}),{isFocusVisible:k,focusProps:G}=Z({autoFocus:a}),{isHovered:I,hoverProps:F}=ee({isDisabled:d}),E=((g=e.firstOption)==null?void 0:g.id)===i,m=e.compare(e.value,c),b=se({ref:L,id:i,role:"radio","aria-checked":m?"true":"false","aria-labelledby":T,"aria-describedby":v,"aria-disabled":d?!0:void 0,tabIndex:(()=>d?-1:m||!e.containsCheckedOption&&E?e.tabIndex:-1)(),autoFocus:a,onClick:d?void 0:p},G,F),s=V({checked:m,disabled:d,hover:I,focus:k,autofocus:a});return J()({ourProps:b,theirProps:h,slot:s,defaultTag:Ne,name:"Radio"})}let Be=j(Se),Ve=j(He),Ke=j(We),$e=Le,je=Ce,yt=Object.assign(Be,{Option:Ve,Radio:Ke,Label:$e,Description:je});export{Ke as Radio,yt as RadioGroup,je as RadioGroupDescription,$e as RadioGroupLabel,Ve as RadioGroupOption};