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