var f=Object.defineProperty;var b=(t,n,e)=>n in t?f(t,n,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[n]=e;var p=(t,n,e)=>(b(t,typeof n!="symbol"?n+"":n,e),e);import{Machine as P}from'../../machine.js';import{stackMachines as y}from'../../machines/stack-machine.js';import*as a from'../../utils/dom.js';import{getFocusableElements as I}from'../../utils/focus-management.js';import{match as M}from'../../utils/match.js';import{getOwnerDocument as T}from'../../utils/owner.js';var v=(e=>(e[e.Open=0]="Open",e[e.Closed=1]="Closed",e))(v||{}),h=(r=>(r[r.OpenPopover=0]="OpenPopover",r[r.ClosePopover=1]="ClosePopover",r[r.SetButton=2]="SetButton",r[r.SetButtonId=3]="SetButtonId",r[r.SetPanel=4]="SetPanel",r[r.SetPanelId=5]="SetPanelId",r))(h||{});let E={[0]:t=>t.popoverState===0?t:{...t,popoverState:0,__demoMode:!1},[1](t){return t.popoverState===1?t:{...t,popoverState:1,__demoMode:!1}},[2](t,n){return t.button===n.button?t:{...t,button:n.button}},[3](t,n){return t.buttonId===n.buttonId?t:{...t,buttonId:n.buttonId}},[4](t,n){return t.panel===n.panel?t:{...t,panel:n.panel}},[5](t,n){return t.panelId===n.panelId?t:{...t,panelId:n.panelId}}};class d extends P{constructor(e){super(e);p(this,"actions",{close:()=>this.send({type:1}),refocusableClose:e=>{this.actions.close();let o=(()=>e?a.isHTMLElement(e)?e:"current"in e&&a.isHTMLElement(e.current)?e.current:this.state.button:this.state.button)();o==null||o.focus()},open:()=>this.send({type:0}),setButtonId:e=>this.send({type:3,buttonId:e}),setButton:e=>this.send({type:2,button:e}),setPanelId:e=>this.send({type:5,panelId:e}),setPanel:e=>this.send({type:4,panel:e})});p(this,"selectors",{isPortalled:e=>{var i;if(!e.button||!e.panel)return!1;let o=(i=T(e.button))!=null?i:document;for(let u of o.querySelectorAll("body > *"))if(Number(u==null?void 0:u.contains(e.button))^Number(u==null?void 0:u.contains(e.panel)))return!0;let l=I(o),s=l.indexOf(e.button),r=(s+l.length-1)%l.length,c=(s+1)%l.length,S=l[r],m=l[c];return!e.panel.contains(S)&&!e.panel.contains(m)}});{let o=this.state.id,l=y.get(null);this.on(0,()=>l.actions.push(o)),this.on(1,()=>l.actions.pop(o))}}static new({id:e,__demoMode:o=!1}){return new d({id:e,__demoMode:o,popoverState:o?0:1,buttons:{current:[]},button:null,buttonId:null,panel:null,panelId:null,beforePanelSentinel:{current:null},afterPanelSentinel:{current:null},afterButtonSentinel:{current:null}})}reduce(e,o){return M(o.type,E,e,o)}}export{h as ActionTypes,d as PopoverMachine,v as PopoverStates};
