import E,{Fragment as F,createContext as C,useContext as I,useEffect as h,useMemo as S,useReducer as N,useRef as g}from"react";import{match as x}from'../../utils/match.js';import{forwardRefWithAs as L,render as O,Features as U}from'../../utils/render.js';import{optionalRef as K,useSyncRefs as B}from'../../hooks/use-sync-refs.js';import{useId as _}from'../../hooks/use-id.js';import{Keys as m}from'../keyboard.js';import{isDisabledReactIssue7711 as G}from'../../utils/bugs.js';import{OpenClosedProvider as j,State as R,useOpenClosed as W}from'../../internal/open-closed.js';import{useResolveButtonType as $}from'../../hooks/use-resolve-button-type.js';import{getOwnerDocument as J}from'../../utils/owner.js';import{useEvent as A}from'../../hooks/use-event.js';import{startTransition as X}from'../../utils/start-transition.js';var q=(o=>(o[o.Open=0]="Open",o[o.Closed=1]="Closed",o))(q||{}),z=(t=>(t[t.ToggleDisclosure=0]="ToggleDisclosure",t[t.CloseDisclosure=1]="CloseDisclosure",t[t.SetButtonId=2]="SetButtonId",t[t.SetPanelId=3]="SetPanelId",t[t.LinkPanel=4]="LinkPanel",t[t.UnlinkPanel=5]="UnlinkPanel",t))(z||{});let Q={[0]:e=>({...e,disclosureState:x(e.disclosureState,{[0]:1,[1]:0})}),[1]:e=>e.disclosureState===1?e:{...e,disclosureState:1},[4](e){return e.linkedPanel===!0?e:{...e,linkedPanel:!0}},[5](e){return e.linkedPanel===!1?e:{...e,linkedPanel:!1}},[2](e,n){return e.buttonId===n.buttonId?e:{...e,buttonId:n.buttonId}},[3](e,n){return e.panelId===n.panelId?e:{...e,panelId:n.panelId}}},k=C(null);k.displayName="DisclosureContext";function M(e){let n=I(k);if(n===null){let o=new Error(`<${e} /> is missing a parent <Disclosure /> component.`);throw Error.captureStackTrace&&Error.captureStackTrace(o,M),o}return n}let v=C(null);v.displayName="DisclosureAPIContext";function w(e){let n=I(v);if(n===null){let o=new Error(`<${e} /> is missing a parent <Disclosure /> component.`);throw Error.captureStackTrace&&Error.captureStackTrace(o,w),o}return n}let H=C(null);H.displayName="DisclosurePanelContext";function V(){return I(H)}function Y(e,n){return x(n.type,Q,e,n)}let Z=F;function ee(e,n){let{defaultOpen:o=!1,...u}=e,T=g(null),l=B(n,K(a=>{T.current=a},e.as===void 0||e.as===F)),t=g(null),f=g(null),s=N(Y,{disclosureState:o?0:1,linkedPanel:!1,buttonRef:f,panelRef:t,buttonId:null,panelId:null}),[{disclosureState:i,buttonId:c},D]=s,d=A(a=>{D({type:1});let r=J(T);if(!r||!c)return;let p=(()=>a?a instanceof HTMLElement?a:a.current instanceof HTMLElement?a.current:r.getElementById(c):r.getElementById(c))();p==null||p.focus()}),P=S(()=>({close:d}),[d]),b=S(()=>({open:i===0,close:d}),[i,d]),y={ref:l};return E.createElement(k.Provider,{value:s},E.createElement(v.Provider,{value:P},E.createElement(j,{value:x(i,{[0]:R.Open,[1]:R.Closed})},O({ourProps:y,theirProps:u,slot:b,defaultTag:Z,name:"Disclosure"}))))}let te="button";function ne(e,n){let o=_(),{id:u=`headlessui-disclosure-button-${o}`,...T}=e,[l,t]=M("Disclosure.Button"),f=V(),s=f===null?!1:f===l.panelId,i=g(null),c=B(i,n,s?null:l.buttonRef);h(()=>{if(!s)return t({type:2,buttonId:u}),()=>{t({type:2,buttonId:null})}},[u,t,s]);let D=A(r=>{var p;if(s){if(l.disclosureState===1)return;switch(r.key){case m.Space:case m.Enter:r.preventDefault(),r.stopPropagation(),t({type:0}),(p=l.buttonRef.current)==null||p.focus();break}}else switch(r.key){case m.Space:case m.Enter:r.preventDefault(),r.stopPropagation(),t({type:0});break}}),d=A(r=>{switch(r.key){case m.Space:r.preventDefault();break}}),P=A(r=>{var p;G(r.currentTarget)||e.disabled||(s?(t({type:0}),(p=l.buttonRef.current)==null||p.focus()):t({type:0}))}),b=S(()=>({open:l.disclosureState===0}),[l]),y=$(e,i),a=s?{ref:c,type:y,onKeyDown:D,onClick:P}:{ref:c,id:u,type:y,"aria-expanded":e.disabled?void 0:l.disclosureState===0,"aria-controls":l.linkedPanel?l.panelId:void 0,onKeyDown:D,onKeyUp:d,onClick:P};return O({ourProps:a,theirProps:T,slot:b,defaultTag:te,name:"Disclosure.Button"})}let le="div",oe=U.RenderStrategy|U.Static;function re(e,n){let o=_(),{id:u=`headlessui-disclosure-panel-${o}`,...T}=e,[l,t]=M("Disclosure.Panel"),{close:f}=w("Disclosure.Panel"),s=B(n,l.panelRef,P=>{X(()=>t({type:P?4:5}))});h(()=>(t({type:3,panelId:u}),()=>{t({type:3,panelId:null})}),[u,t]);let i=W(),c=(()=>i!==null?(i&R.Open)===R.Open:l.disclosureState===0)(),D=S(()=>({open:l.disclosureState===0,close:f}),[l,f]),d={ref:s,id:u};return E.createElement(H.Provider,{value:l.panelId},O({ourProps:d,theirProps:T,slot:D,defaultTag:le,features:oe,visible:c,name:"Disclosure.Panel"}))}let se=L(ee),ue=L(ne),ie=L(re),ve=Object.assign(se,{Button:ue,Panel:ie});export{ve as Disclosure};
