import*as e from"react";import{useComposedRefs as t}from"../../react-compose-refs/dist/index.js";import{createContextScope as r}from"../../react-context/dist/index.js";import{composeEventHandlers as n}from"../../primitive/dist/index.js";import{useControllableState as o}from"../../react-use-controllable-state/dist/index.js";import{usePrevious as s}from"../../react-use-previous/dist/index.js";import{useSize as i}from"../../react-use-size/dist/index.js";import{Presence as a}from"../../react-presence/dist/index.js";import{Primitive as c}from"../../react-primitive/dist/index.js";import{jsxs as d,jsx as u}from"react/jsx-runtime";var l="Checkbox",[p,f]=r(l),[m,b]=p(l),h=e.forwardRef(((r,s)=>{const{__scopeCheckbox:i,name:a,checked:l,defaultChecked:p,required:f,disabled:b,value:h="on",onCheckedChange:k,form:v,...j}=r,[E,w]=e.useState(null),g=t(s,(e=>w(e))),R=e.useRef(!1),D=!E||(v||!!E.closest("form")),[P=!1,_]=o({prop:l,defaultProp:p,onChange:k}),q=e.useRef(P);return e.useEffect((()=>{const e=E?.form;if(e){const t=()=>_(q.current);return e.addEventListener("reset",t),()=>e.removeEventListener("reset",t)}}),[E,_]),d(m,{scope:i,state:P,disabled:b,children:[u(c.button,{type:"button",role:"checkbox","aria-checked":y(P)?"mixed":P,"aria-required":f,"data-state":C(P),"data-disabled":b?"":void 0,disabled:b,value:h,...j,ref:g,onKeyDown:n(r.onKeyDown,(e=>{"Enter"===e.key&&e.preventDefault()})),onClick:n(r.onClick,(e=>{_((e=>!!y(e)||!e)),D&&(R.current=e.isPropagationStopped(),R.current||e.stopPropagation())}))}),D&&u(x,{control:E,bubbles:!R.current,name:a,value:h,checked:P,required:f,disabled:b,form:v,style:{transform:"translateX(-100%)"},defaultChecked:!y(p)&&p})]})}));h.displayName=l;var k="CheckboxIndicator",v=e.forwardRef(((e,t)=>{const{__scopeCheckbox:r,forceMount:n,...o}=e,s=b(k,r);return u(a,{present:n||y(s.state)||!0===s.state,children:u(c.span,{"data-state":C(s.state),"data-disabled":s.disabled?"":void 0,...o,ref:t,style:{pointerEvents:"none",...e.style}})})}));v.displayName=k;var x=t=>{const{control:r,checked:n,bubbles:o=!0,defaultChecked:a,...c}=t,d=e.useRef(null),l=s(n),p=i(r);e.useEffect((()=>{const e=d.current,t=window.HTMLInputElement.prototype,r=Object.getOwnPropertyDescriptor(t,"checked").set;if(l!==n&&r){const t=new Event("click",{bubbles:o});e.indeterminate=y(n),r.call(e,!y(n)&&n),e.dispatchEvent(t)}}),[l,n,o]);const f=e.useRef(!y(n)&&n);return u("input",{type:"checkbox","aria-hidden":!0,defaultChecked:a??f.current,...c,tabIndex:-1,ref:d,style:{...t.style,...p,position:"absolute",pointerEvents:"none",opacity:0,margin:0}})};function y(e){return"indeterminate"===e}function C(e){return y(e)?"indeterminate":e?"checked":"unchecked"}var j=h,E=v;export{h as Checkbox,v as CheckboxIndicator,E as Indicator,j as Root,f as createCheckboxScope};
//# sourceMappingURL=index.js.map
