import{kt as e}from"./BA99CCI5.js";import"./D7pSEO6W.js";var t=6e3,n=5e3;function r(){let{subscribe:r,set:i,update:a}=e({state:`idle`}),o=null,s=null,c=0,l=null,u=null,d=null,f=new Set;function p(e){return f.add(e),()=>{f.delete(e)}}function m(){o&&=(clearTimeout(o),null),s&&=(clearTimeout(s),null),l=null}function h(e){let t=Date.now()-c;if(c>0&&t<n){l=e,s&&clearTimeout(s),s=setTimeout(()=>{l&&=(o&&=(clearTimeout(o),null),u=l.jobId,i({state:`running`,jobType:l.jobType,displayLabel:l.displayLabel}),null),c=0,s=null},n-t);return}o&&=(clearTimeout(o),null),c=0,l=null,s&&=(clearTimeout(s),null),u=e.jobId,i({state:`running`,jobType:e.jobType,displayLabel:e.displayLabel})}function g(e){a(t=>t.state!==`running`||u!==null&&u!==e.jobId?t:(u===null&&(u=e.jobId),{...t,displayLabel:e.label}))}function _(e){for(let t of f)try{t(e)}catch{}l!==null&&l.jobId===e.jobId&&(l=null),(u===null||u===e.jobId)&&(m(),c=Date.now(),i({state:`completed`,jobType:e.jobType,displayLabel:e.displayLabel,status:e.status,durationMs:e.durationMs}),o=setTimeout(()=>{u=null,i({state:`idle`}),c=0,o=null,v()},t))}function v(){d?.close(),d=null}function y(){d||(d=new EventSource(`/jobs/events`),d.addEventListener(`job.started`,e=>{try{h(JSON.parse(e.data))}catch{}}),d.addEventListener(`job.progress`,e=>{try{g(JSON.parse(e.data))}catch{}}),d.addEventListener(`job.finished`,e=>{try{_(JSON.parse(e.data))}catch{}}))}function b(){v(),m(),c=0,u=null,i({state:`idle`})}function x(e,t){u=null,i({state:`running`,jobType:e,displayLabel:t})}function S(){a(e=>e.state===`running`&&u===null?{state:`idle`}:e)}return{subscribe:r,connect:y,disconnect:b,setRunning:x,cancelOptimistic:S,onJobFinished:p}}var i=r();export{i as t};