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