import{a as T}from"./chunk-AGS2J6BT.js";import{f as $}from"./chunk-ERPQLX3P.js";import{a as I}from"./chunk-H4HM5ZJD.js";import{a as F}from"./chunk-MLI5PH7P.js";import{ca as L,ha as p}from"./chunk-42LKD4JQ.js";import{a as S,b as C,i as j}from"./chunk-2AVGQ2MQ.js";var M=(()=>{class w{constructor(r,n,c,u){this.authService=r,this.configService=n,this.downloads=c,this.document=u}openUrl(r,n="_blank"){r?window.open(r,n):console.error("No URL provided to open.")}openWithHeaders(r,n){return j(this,null,function*(){let c="download";try{let a=new URL(r).searchParams.get("rscd")||"",d=a.match(/filename\*=UTF-8''([^;]+)|filename=\"?([^\";]+)\"?/i)||a.match(/filename=\"?([^";]+)\"?/i),m=d&&(d[1]||d[2])||"";m&&(c=decodeURIComponent(m))}catch{}let u=this.authService.Token??"",E=n&&Object.keys(n).reduce((t,a)=>typeof n[a]<"u"?`${t}&${a}=${encodeURIComponent(n[a]||"")}`:t,"")||"",O=`${this.configService.protocol}${this.configService.baseFolder}/paralite/cloud/${this.configService.cloudName}/proxy/stream?target=${encodeURIComponent(r)}${E}`,f=new AbortController,o=this.downloads.createTask(c,null);this.downloads.attachAbortController(o,f);try{let t=yield fetch(O,C(S({method:"GET"},u?{headers:{token:u}}:{}),{signal:f.signal}));if(!t.ok)throw new Error(`Download failed with status ${t.status}`);let a=t.headers.get("content-disposition")||"",d=a.match(/filename\*=UTF-8''([^;]+)|filename=\"?([^\";]+)\"?/i)||a.match(/filename=\"?([^";]+)\"?/i),m=d&&(d[1]||d[2])||"";m&&(c=decodeURIComponent(m));let k=t.headers.get("content-length"),v=k?parseInt(k,10):void 0;this.downloads.setTotalSize(o,isFinite(v)?v:null);let y=t.body;if(typeof window.showSaveFilePicker=="function"&&!!y){let g;try{g=yield window.showSaveFilePicker({suggestedName:c})}catch(e){throw console.log(e),this.downloads.cancelFile(o),new Error("Download failed as user Cancelled.")}let i=yield g.createWritable(),h=y.getReader(),s=i.getWriter?i.getWriter():i;try{for(;;){let{done:e,value:b}=yield h.read();if(e)break;b&&(yield s.write(b),this.downloads.onChunk(o,b.byteLength))}s.close?yield s.close():i.close&&(yield i.close()),this.downloads.complete(o);return}catch(e){try{s.abort&&(yield s.abort(e))}catch{}try{i.abort&&(yield i.abort(e))}catch{}if(f.signal.aborted)return;throw this.downloads.fail(o,e),e}}if(y&&typeof window.ReadableStream<"u"){let g=y.getReader(),i=[];try{for(;;){let{done:b,value:U}=yield g.read();if(b)break;U&&(i.push(U),this.downloads.onChunk(o,U.byteLength))}let h=new Blob(i),s=window.URL.createObjectURL(h),e=this.document.createElement("a");e.style.display="none",e.href=s,e.download=c,this.document.body.appendChild(e),e.click(),setTimeout(()=>{this.document.body.removeChild(e),URL.revokeObjectURL(s)},0),this.downloads.complete(o);return}catch(h){if(f.signal.aborted)return;throw this.downloads.fail(o,h),h}}let N=yield t.blob(),R=window.URL.createObjectURL(N),l=this.document.createElement("a");l.style.display="none",l.href=R,l.download=c,this.document.body.appendChild(l),l.click(),setTimeout(()=>{this.document.body.removeChild(l),URL.revokeObjectURL(R)},0),this.downloads.complete(o)}catch(t){if(f.signal.aborted)return;console.error("Download failed:",t),this.downloads.fail(o,t)}})}static{this.\u0275fac=function(n){return new(n||w)(p($),p(I),p(T),p(F))}}static{this.\u0275prov=L({token:w,factory:w.\u0275fac,providedIn:"root"})}}return w})();export{M as a};
