import{br as S,bC as v}from"./index-D5GfLbIx.js";const g=["snapshot-tick","selection-detail","alarm","object-created","object-deleted","object-patched","object-batch-changed","pick-result","project-saved","project-loaded","car-state","mission-status","monitor-config-updated","workspace-shortcut","viewport-style-updated"];function T(a={}){const y=a.url??"/api/sl/projection/stream",j=a.reconnectDelayMs??3e3,r=v(!1),u=v(null),s=new Set;let e=null,c=null,l=!1;function i(){if(l||e)return;try{e=new EventSource(y,{withCredentials:!0})}catch{p();return}e.onopen=()=>{r.value=!0},e.onerror=()=>{r.value=!1,f(),p()},e.onmessage=t=>{d(void 0,t.data)};const n=new Set([...g,...a.extraEventKinds??[]]);for(const t of n)e.addEventListener(t,o=>{d(t,o.data)})}function d(n,t){if(!(typeof t!="string"||!t))try{const o=JSON.parse(t),m=typeof o=="object"&&o!==null&&"kind"in o?o:{kind:n??"message",payload:o};u.value=m,s.forEach(k=>{try{k(m)}catch{}})}catch{}}function f(){if(e){try{e.close()}catch{}e=null}}function p(){l||c||(c=setTimeout(()=>{c=null,i()},j))}function h(){l=!0,c&&(clearTimeout(c),c=null),f(),r.value=!1}function b(n){s.add(n)}function E(n){s.delete(n)}return a.autoConnect!==!1&&i(),S(()=>{h(),s.clear()}),{connected:r,lastEvent:u,connect:i,disconnect:h,on:b,off:E}}export{T as u};
