"use client";import{useFocusRing as z}from"@react-aria/focus";import{useHover as Q}from"@react-aria/interactions";import i,{Fragment as Y,createContext as Z,useCallback as ee,useContext as te,useMemo as L,useRef as oe,useState as S}from"react";import{useActivePress as re}from'../../hooks/use-active-press.js';import{useControllable as ne}from'../../hooks/use-controllable.js';import{useDefaultValue as le}from'../../hooks/use-default-value.js';import{useDisposables as ie}from'../../hooks/use-disposables.js';import{useEvent as m}from'../../hooks/use-event.js';import{useId as ae}from'../../hooks/use-id.js';import{useResolveButtonType as se}from'../../hooks/use-resolve-button-type.js';import{useSyncRefs as pe}from'../../hooks/use-sync-refs.js';import{useDisabled as ce}from'../../internal/disabled.js';import{FormFields as ue}from'../../internal/form-fields.js';import{useProvidedId as de}from'../../internal/id.js';import{isDisabledReactIssue7711 as me}from'../../utils/bugs.js';import{attemptSubmit as fe}from'../../utils/form.js';import{forwardRefWithAs as he,mergeProps as be,render as G}from'../../utils/render.js';import{Description as Te,useDescribedBy as ye,useDescriptions as Se}from'../description/description.js';import{Keys as R}from'../keyboard.js';import{Label as we,useLabelledBy as Ee,useLabels as _e}from'../label/label.js';let w=Z(null);w.displayName="GroupContext";let Pe=Y;function De(n){var a;let[o,p]=S(null),[f,h]=_e(),[b,t]=Se(),c=L(()=>({switch:o,setSwitch:p}),[o,p]),T={},y=n;return i.createElement(t,{name:"Switch.Description",value:b},i.createElement(h,{name:"Switch.Label",value:f,props:{htmlFor:(a=c.switch)==null?void 0:a.id,onClick(u){o&&(u.currentTarget instanceof HTMLLabelElement&&u.preventDefault(),o.click(),o.focus({preventScroll:!0}))}}},i.createElement(w.Provider,{value:c},G({ourProps:T,theirProps:y,slot:{},defaultTag:Pe,name:"Switch.Group"}))))}let ge="button";function ve(n,o){var C;let p=ae(),f=de(),h=ce(),{id:b=f||`headlessui-switch-${p}`,disabled:t=h||!1,checked:c,defaultChecked:T,onChange:y,name:a,value:u,form:A,autoFocus:d=!1,...F}=n,E=te(w),[H,k]=S(null),M=oe(null),U=pe(M,o,E===null?null:E.setSwitch,k),l=le(T),[s,r]=ne(c,y,l!=null?l:!1),I=ie(),[_,P]=S(!1),D=m(()=>{P(!0),r==null||r(!s),I.nextFrame(()=>{P(!1)})}),B=m(e=>{if(me(e.currentTarget))return e.preventDefault();e.preventDefault(),D()}),K=m(e=>{e.key===R.Space?(e.preventDefault(),D()):e.key===R.Enter&&fe(e.currentTarget)}),W=m(e=>e.preventDefault()),O=Ee(),N=ye(),{isFocusVisible:g,focusProps:J}=z({autoFocus:d}),{isHovered:v,hoverProps:V}=Q({isDisabled:t}),{pressed:x,pressProps:X}=re({disabled:t}),j=L(()=>({checked:s,disabled:t,hover:v,focus:g,active:x,autofocus:d,changing:_}),[s,v,g,x,t,_,d]),$=be({id:b,ref:U,role:"switch",type:se(n,H),tabIndex:n.tabIndex===-1?0:(C=n.tabIndex)!=null?C:0,"aria-checked":s,"aria-labelledby":O,"aria-describedby":N,disabled:t||void 0,autoFocus:d,onClick:B,onKeyUp:K,onKeyPress:W},J,V,X),q=ee(()=>{if(l!==void 0)return r==null?void 0:r(l)},[r,l]);return i.createElement(i.Fragment,null,a!=null&&i.createElement(ue,{disabled:t,data:{[a]:u||"on"},overrides:{type:"checkbox",checked:s},form:A,onReset:q}),G({ourProps:$,theirProps:F,slot:j,defaultTag:ge,name:"Switch"}))}let xe=he(ve),Ce=De,Le=we,Ge=Te,Qe=Object.assign(xe,{Group:Ce,Label:Le,Description:Ge});export{Qe as Switch,Ge as SwitchDescription,Ce as SwitchGroup,Le as SwitchLabel};
