import{r as o}from"./react-Cb2sDjhD.js";function L(c,m={}){const{url:u="/events",enabled:s=!0}=m,n=o.useRef(c);n.current=c;const[S,t]=o.useState("connecting");return o.useEffect(()=>{if(!s){t("closed");return}if(typeof EventSource>"u"){t("closed");return}let e=null,a=!1;function l(){if(a)return;t(i=>i==="closed"?"connecting":i),e=new EventSource(u,{withCredentials:!0}),e.addEventListener("open",()=>{t("connected")}),e.addEventListener("error",()=>{t("reconnecting")});const r=(i,b)=>{b&&e.addEventListener(i,h=>{let p;try{p=JSON.parse(h.data)}catch{return}n.current[i]?.(p)})};r("hello",n.current.hello),r("audit",n.current.audit),r("approval",n.current.approval),r("job",n.current.job),r("metrics",n.current.metrics)}function d(){try{e?.close()}catch{}e=null}function f(){d(),l()}function v(){document.visibilityState==="visible"&&(!e||e.readyState!==EventSource.OPEN)&&f()}function E(){f()}return l(),document.addEventListener("visibilitychange",v),window.addEventListener("online",E),()=>{a=!0,document.removeEventListener("visibilitychange",v),window.removeEventListener("online",E),d(),t("closed")}},[u,s]),S}export{L as u};
//# sourceMappingURL=use-event-stream-DgGpGKop.js.map
