"use client";import R,{createContext as D,useContext as h,useMemo as v,useState as k}from"react";import{useEvent as P}from'../../hooks/use-event.js';import{useId as _}from'../../hooks/use-id.js';import{useIsoMorphicEffect as S}from'../../hooks/use-iso-morphic-effect.js';import{useSlot as A}from'../../hooks/use-slot.js';import{useSyncRefs as B}from'../../hooks/use-sync-refs.js';import{useDisabled as F}from'../../internal/disabled.js';import{useProvidedId as M}from'../../internal/id.js';import*as m from'../../utils/dom.js';import{forwardRefWithAs as I,useRender as H}from'../../utils/render.js';let L=D(null);L.displayName="LabelContext";function C(){let n=h(L);if(n===null){let l=new Error("You used a <Label /> component, but it is not inside a relevant parent.");throw Error.captureStackTrace&&Error.captureStackTrace(l,C),l}return n}function N(n){var a,e,o;let l=(e=(a=h(L))==null?void 0:a.value)!=null?e:void 0;return((o=n==null?void 0:n.length)!=null?o:0)>0?[l,...n].filter(Boolean).join(" "):l}function V({inherit:n=!1}={}){let l=N(),[a,e]=k([]),o=n?[l,...a].filter(Boolean):a;return[o.length>0?o.join(" "):void 0,v(()=>function(t){let p=P(i=>(e(u=>[...u,i]),()=>e(u=>{let d=u.slice(),f=d.indexOf(i);return f!==-1&&d.splice(f,1),d}))),b=v(()=>({register:p,slot:t.slot,name:t.name,props:t.props,value:t.value}),[p,t.slot,t.name,t.props,t.value]);return R.createElement(L.Provider,{value:b},t.children)},[e])]}let G="label";function U(n,l){var y;let a=_(),e=C(),o=M(),T=F(),{id:t=`headlessui-label-${a}`,htmlFor:p=o!=null?o:(y=e.props)==null?void 0:y.htmlFor,passive:b=!1,...i}=n,u=B(l);S(()=>e.register(t),[t,e.register]);let d=P(s=>{let g=s.currentTarget;if(!(s.target!==s.currentTarget&&m.isInteractiveElement(s.target))&&(m.isHTMLLabelElement(g)&&s.preventDefault(),e.props&&"onClick"in e.props&&typeof e.props.onClick=="function"&&e.props.onClick(s),m.isHTMLLabelElement(g))){let r=document.getElementById(g.htmlFor);if(r){let E=r.getAttribute("disabled");if(E==="true"||E==="")return;let x=r.getAttribute("aria-disabled");if(x==="true"||x==="")return;(m.isHTMLInputElement(r)&&(r.type==="file"||r.type==="radio"||r.type==="checkbox")||r.role==="radio"||r.role==="checkbox"||r.role==="switch")&&r.click(),r.focus({preventScroll:!0})}}}),f=A({...e.slot,disabled:T||!1}),c={ref:u,...e.props,id:t,htmlFor:p,onClick:d};return b&&("onClick"in c&&(delete c.htmlFor,delete c.onClick),"onClick"in i&&delete i.onClick),H()({ourProps:c,theirProps:i,slot:f,defaultTag:p?G:"div",name:e.name||"Label"})}let j=I(U),Z=Object.assign(j,{});export{Z as Label,C as useLabelContext,N as useLabelledBy,V as useLabels};
