import{defineComponent as y,ref as d,provide as C,inject as O,computed as m,watchEffect as M,onMounted as P,onUnmounted as h}from"vue";import{Keys as f}from'../../keyboard.js';import{match as I}from'../../utils/match.js';import{render as b,Features as R}from'../../utils/render.js';import{useId as E}from'../../hooks/use-id.js';import{dom as p}from'../../utils/dom.js';import{useOpenClosedProvider as w,State as v,useOpenClosed as H}from'../../internal/open-closed.js';import{useResolveButtonType as L}from'../../hooks/use-resolve-button-type.js';var j=(o=>(o[o.Open=0]="Open",o[o.Closed=1]="Closed",o))(j||{});let x=Symbol("DisclosureContext");function g(t){let r=O(x,null);if(r===null){let o=new Error(`<${t} /> is missing a parent <Disclosure /> component.`);throw Error.captureStackTrace&&Error.captureStackTrace(o,g),o}return r}let B=Symbol("DisclosurePanelContext");function $(){return O(B,null)}let Q=y({name:"Disclosure",props:{as:{type:[Object,String],default:"template"},defaultOpen:{type:[Boolean],default:!1}},setup(t,{slots:r,attrs:o}){let u=d(t.defaultOpen?0:1),e=d(null),i=d(null),s={buttonId:d(null),panelId:d(null),disclosureState:u,panel:e,button:i,toggleDisclosure(){u.value=I(u.value,{[0]:1,[1]:0})},closeDisclosure(){u.value!==1&&(u.value=1)},close(l){s.closeDisclosure();let a=(()=>l?l instanceof HTMLElement?l:l.value instanceof HTMLElement?p(l):p(s.button):p(s.button))();a==null||a.focus()}};return C(x,s),w(m(()=>I(u.value,{[0]:v.Open,[1]:v.Closed}))),()=>{let{defaultOpen:l,...a}=t,c={open:u.value===0,close:s.close};return b({theirProps:a,ourProps:{},slot:c,slots:r,attrs:o,name:"Disclosure"})}}}),V=y({name:"DisclosureButton",props:{as:{type:[Object,String],default:"button"},disabled:{type:[Boolean],default:!1},id:{type:String,default:()=>`headlessui-disclosure-button-${E()}`}},setup(t,{attrs:r,slots:o,expose:u}){let e=g("DisclosureButton");P(()=>{e.buttonId.value=t.id}),h(()=>{e.buttonId.value=null});let i=$(),s=m(()=>i===null?!1:i.value===e.panelId.value),l=d(null);u({el:l,$el:l}),s.value||M(()=>{e.button.value=l.value});let a=L(m(()=>({as:t.as,type:r.type})),l);function c(){var n;t.disabled||(s.value?(e.toggleDisclosure(),(n=p(e.button))==null||n.focus()):e.toggleDisclosure())}function D(n){var S;if(!t.disabled)if(s.value)switch(n.key){case f.Space:case f.Enter:n.preventDefault(),n.stopPropagation(),e.toggleDisclosure(),(S=p(e.button))==null||S.focus();break}else switch(n.key){case f.Space:case f.Enter:n.preventDefault(),n.stopPropagation(),e.toggleDisclosure();break}}function T(n){switch(n.key){case f.Space:n.preventDefault();break}}return()=>{let n={open:e.disclosureState.value===0},{id:S,...k}=t,K=s.value?{ref:l,type:a.value,onClick:c,onKeydown:D}:{id:S,ref:l,type:a.value,"aria-expanded":t.disabled?void 0:e.disclosureState.value===0,"aria-controls":p(e.panel)?e.panelId.value:void 0,disabled:t.disabled?!0:void 0,onClick:c,onKeydown:D,onKeyup:T};return b({ourProps:K,theirProps:k,slot:n,attrs:r,slots:o,name:"DisclosureButton"})}}}),X=y({name:"DisclosurePanel",props:{as:{type:[Object,String],default:"div"},static:{type:Boolean,default:!1},unmount:{type:Boolean,default:!0},id:{type:String,default:()=>`headlessui-disclosure-panel-${E()}`}},setup(t,{attrs:r,slots:o,expose:u}){let e=g("DisclosurePanel");P(()=>{e.panelId.value=t.id}),h(()=>{e.panelId.value=null}),u({el:e.panel,$el:e.panel}),C(B,e.panelId);let i=H(),s=m(()=>i!==null?(i.value&v.Open)===v.Open:e.disclosureState.value===0);return()=>{let l={open:e.disclosureState.value===0,close:e.close},{id:a,...c}=t,D={id:a,ref:e.panel};return b({ourProps:D,theirProps:c,slot:l,attrs:r,slots:o,features:R.RenderStrategy|R.Static,visible:s.value,name:"DisclosurePanel"})}}});export{Q as Disclosure,V as DisclosureButton,X as DisclosurePanel};
