import l,{Fragment as H,createContext as G,useContext as K,useMemo as S,useRef as M,useState as x,useEffect as U}from"react";import{forwardRefWithAs as _,render as w,compact as F}from'../../utils/render.js';import{useId as I}from'../../hooks/use-id.js';import{Keys as P}from'../keyboard.js';import{isDisabledReactIssue7711 as B}from'../../utils/bugs.js';import{Label as O,useLabels as W}from'../label/label.js';import{Description as j,useDescriptions as N}from'../description/description.js';import{useResolveButtonType as $}from'../../hooks/use-resolve-button-type.js';import{useSyncRefs as q}from'../../hooks/use-sync-refs.js';import{Hidden as z,Features as J}from'../../internal/hidden.js';import{attemptSubmit as Q}from'../../utils/form.js';import{useEvent as f}from'../../hooks/use-event.js';import{useControllable as V}from'../../hooks/use-controllable.js';import{useDisposables as X}from'../../hooks/use-disposables.js';let b=G(null);b.displayName="GroupContext";let Y=H;function Z(y){let[t,a]=x(null),[s,m]=W(),[c,o]=N(),p=S(()=>({switch:t,setSwitch:a,labelledby:s,describedby:c}),[t,a,s,c]),d={},h=y;return l.createElement(o,{name:"Switch.Description"},l.createElement(m,{name:"Switch.Label",props:{onClick(){!t||(t.click(),t.focus({preventScroll:!0}))}}},l.createElement(b.Provider,{value:p},w({ourProps:d,theirProps:h,defaultTag:Y,name:"Switch.Group"}))))}let ee="button",te=_(function(t,a){let s=I(),{id:m=`headlessui-switch-${s}`,checked:c,defaultChecked:o=!1,onChange:p,name:d,value:h,...v}=t,r=K(b),u=M(null),k=q(u,a,r===null?null:r.setSwitch),[n,i]=V(c,p,o),T=f(()=>i==null?void 0:i(!n)),g=f(e=>{if(B(e.currentTarget))return e.preventDefault();e.preventDefault(),T()}),C=f(e=>{e.key===P.Space?(e.preventDefault(),T()):e.key===P.Enter&&Q(e.currentTarget)}),D=f(e=>e.preventDefault()),L=S(()=>({checked:n}),[n]),R={id:m,ref:k,role:"switch",type:$(t,u),tabIndex:0,"aria-checked":n,"aria-labelledby":r==null?void 0:r.labelledby,"aria-describedby":r==null?void 0:r.describedby,onClick:g,onKeyUp:C,onKeyPress:D},A=X();return U(()=>{var E;let e=(E=u.current)==null?void 0:E.closest("form");!e||o!==void 0&&A.addEventListener(e,"reset",()=>{i(o)})},[u,i]),l.createElement(l.Fragment,null,d!=null&&n&&l.createElement(z,{features:J.Hidden,...F({as:"input",type:"checkbox",hidden:!0,readOnly:!0,checked:n,name:d,value:h})}),w({ourProps:R,theirProps:v,slot:L,defaultTag:ee,name:"Switch"}))}),Pe=Object.assign(te,{Group:Z,Label:O,Description:j});export{Pe as Switch};
