var _=Object.defineProperty;var u=a=>{throw TypeError(a)};var g=(a,s,t)=>s in a?_(a,s,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[s]=t;var h=(a,s,t)=>g(a,typeof s!="symbol"?s+"":s,t),S=(a,s,t)=>s.has(a)||u("Cannot "+t);var c=(a,s,t)=>(S(a,s,"read from private field"),t?t.call(a):s.get(a)),p=(a,s,t)=>s.has(a)?u("Cannot add the same private member more than once"):s instanceof WeakSet?s.add(a):s.set(a,t);import{s as y,b as f,g as b,c as D}from"./DpkolLiq.js";import{m as l,a as v}from"./DxBGAPM1.js";var w;class E{constructor(){p(this,w,y(f(new Map)));h(this,"eventSources",new Map)}get downloads(){return b(c(this,w))}set downloads(s){D(c(this,w),s,!0)}async startDownload(s){if(this.isDownloading(s))return;const t={model_id:s,task_id:"",status:"pending",progress:0,downloaded_bytes:0,total_bytes:0},o=new Map(this.downloads);o.set(s,t),this.downloads=o;try{const{task_id:n,download_id:d}=await l.startDownload(s);this.updateDownload(s,{task_id:n,download_id:d}),this.connectSSE(s,n)}catch(n){this.updateDownload(s,{status:"failed",error:n instanceof Error?n.message:"Download failed"})}}async pauseDownload(s){const t=this.downloads.get(s);t!=null&&t.download_id&&(await l.pauseDownload(t.download_id),this.closeSSE(s),this.updateDownload(s,{status:"paused"}))}async resumeDownload(s){const t=this.downloads.get(s);if(!(t!=null&&t.download_id))return;const{task_id:o,progress:n,downloaded_bytes:d,total_bytes:i}=await l.resumeDownload(t.download_id);this.updateDownload(s,{status:"downloading",task_id:o,progress:n,downloaded_bytes:d,total_bytes:i}),this.connectSSE(s,o)}async cancelDownload(s){const t=this.downloads.get(s);if(!(t!=null&&t.download_id))return;await l.cancelDownload(t.download_id),this.closeSSE(s);const o=new Map(this.downloads);o.delete(s),this.downloads=o}isPaused(s){const t=this.downloads.get(s);return(t==null?void 0:t.status)==="paused"}connectSSE(s,t){this.closeSSE(s);const o=v.token,n=`/api/models/download/${t}/progress${o?`?token=${o}`:""}`,d=new EventSource(n);this.eventSources.set(s,d),d.onmessage=i=>{try{const e=JSON.parse(i.data),r=this.downloads.get(s);this.updateDownload(s,{status:e.status,progress:e.progress||0,downloaded_bytes:e.downloaded_bytes||0,total_bytes:e.total_bytes||0,error:e.error,download_id:e.download_id??(r==null?void 0:r.download_id)}),(e.status==="completed"||e.status==="failed")&&this.closeSSE(s)}catch{}},d.onerror=()=>{this.closeSSE(s);const i=this.downloads.get(s);i&&(i.status==="downloading"||i.status==="starting"||i.status==="pending")&&setTimeout(()=>{const e=this.downloads.get(s);e&&e.task_id&&!this.eventSources.has(s)&&(e.status==="downloading"||e.status==="starting"||e.status==="pending")&&this.connectSSE(s,e.task_id)},3e3)}}closeSSE(s){const t=this.eventSources.get(s);t&&(t.close(),this.eventSources.delete(s))}updateDownload(s,t){const o=this.downloads.get(s);if(o){const n=new Map(this.downloads);n.set(s,{...o,...t}),this.downloads=n}}reconnect(s,t,o){if(this.eventSources.has(s))return;const n={model_id:s,task_id:t,download_id:o.download_id,status:o.status||"downloading",progress:o.progress||0,downloaded_bytes:o.downloaded_bytes||0,total_bytes:o.total_bytes||0},d=new Map(this.downloads);d.set(s,n),this.downloads=d,this.connectSSE(s,t)}async loadActiveDownloads(){try{const s=await l.getActiveDownloads();for(const t of s)if(t.status==="paused"){const o={model_id:t.model_id,task_id:t.task_id,download_id:t.download_id,status:"paused",progress:t.progress,downloaded_bytes:t.downloaded_bytes,total_bytes:t.total_bytes},n=new Map(this.downloads);n.set(t.model_id,o),this.downloads=n}else(t.status==="starting"||t.status==="downloading"||t.status==="pending")&&this.reconnect(t.model_id,t.task_id,{download_id:t.download_id,status:t.status,progress:t.progress,downloaded_bytes:t.downloaded_bytes,total_bytes:t.total_bytes})}catch{}}isDownloading(s){const t=this.downloads.get(s);return t?t.status==="pending"||t.status==="starting"||t.status==="downloading":!1}getProgress(s){return this.downloads.get(s)}getAllDownloads(){return Array.from(this.downloads.values())}clearCompleted(){const s=new Map;for(const[t,o]of this.downloads)o.status!=="completed"&&o.status!=="failed"&&o.status!=="cancelled"&&s.set(t,o);this.downloads=s}cleanup(){for(const[s]of this.eventSources)this.closeSSE(s)}}w=new WeakMap;const $=new E;export{$ as d};
