import{ok as e}from"../modules/ok.js";const t=["system:ready","system:stopped","system:clock_change","system:poke","system:drain","system:bus_state_change","item:create","item:update","item:remove","index:reset","index:ready","index:progress","index:drop","index:registered","fts:registered","fts:drop","fts:reset","sync:apply","txn:start","txn:end","txn:rollback","type:registered","type:drop","env:opened","env:removed","engine:started","engine:stopped","engine:error","engine:installed","engine:uninstalled","embeddings:indexer:ready","embeddings:indexer:flushed","embeddings:indexer:doc:done","embeddings:indexer:doc:failed","embeddings:indexer:doc:queued","embeddings:doc:status_change","embeddings:pipeline:ready","queue:enqueued","queue:done","queue:failed","queue:retry","queue:removed","queue:reconciled","processor:ready","processor:progress","processor:error","processor:paused","processor:resumed","processor:retry","relay:error"];export{t as ALL_EVENTS};let n=null;const r={};function s(e){try{return JSON.parse(e)}catch{return null}}function o(e,t){const n=r[e];if(n)for(const e of n)try{e(t)}catch(e){console.error("events-store handler error",e)}}function i(e,t){return[...e.length>=500?e.slice(-499):e,t]}const d=e.proxy({connected:!1,connecting:!1,error:null,activity:[],get logs(){return this.activity.filter(e=>"log"===e._kind)},connect(e=null){if(n||this.connecting)return;this.connecting=!0,this.error=null;let r=__ENV__.API+"/events";e?.length&&(r+="?types="+encodeURIComponent(e.join(",")));const d=new EventSource(r);n=d,d.addEventListener("open",()=>{this.connected=!0,this.connecting=!1,this.error=null}),d.addEventListener("error",()=>{this.connected=!1,this.connecting=!1,this.error="Stream disconnected — reconnecting…"});const c=e=>{const t=s(e.data);if(!t)return;const n={_kind:"log",_ts:t._ts??t.ts??Date.now(),...t};this.activity=i(this.activity,n),o("log",t)};d.addEventListener("log",c);for(const e of t)d.addEventListener(e,t=>{try{const n=s(t.data);if(!n)return;const r={_kind:"event",_event:e,_ts:n._ts??Date.now(),...n};this.activity=i(this.activity,r),o(e,n)}catch{}});d.onmessage=c},disconnect(){n?.close(),n=null,this.connected=!1,this.connecting=!1},on:(e,t)=>(r[e]||(r[e]=new Set),r[e].add(t),()=>r[e]?.delete(t)),clearActivity(){this.activity=[]},clearLogs(){this.clearActivity()}});export default d;