import{_ as y}from"./chunk-LLYDPG5N.js";import{ca as o,f as b,g as k,ha as h}from"./chunk-2BMUMDG3.js";import{a as m,i as f}from"./chunk-2AVGQ2MQ.js";var I=(()=>{class n{constructor(){this.subjects=new Map,typeof Worker<"u"?(this.worker=new Worker(new URL("worker-Q7FRQCZT.js",import.meta.url),{type:"module"}),this.worker.onmessage=e=>this.handleMsg(e.data)):console.warn("Web Worker not supported")}handleMsg(e){if(!e?.fileId)return;let t=this.subjects.get(e.fileId);if(t)switch(e.type){case"PROGRESS":t.next({type:"progress",fileId:e.fileId,percent:e.percent});break;case"CHUNK_OK":t.next({type:"chunk_ok",fileId:e.fileId,currentChunk:e.currentChunk,totalChunks:e.totalChunks});break;case"FILE_DONE":t.next({type:"done",fileId:e.fileId}),t.complete(),this.subjects.delete(e.fileId);break;case"PAUSED":t.next({type:"paused",fileId:e.fileId});break;case"RESUMED":t.next({type:"resumed",fileId:e.fileId});break;case"CANCELED":t.next({type:"canceled",fileId:e.fileId}),t.complete(),this.subjects.delete(e.fileId);break;case"ERROR":t.next({type:"error",fileId:e.fileId,error:e.error}),t.complete(),this.subjects.delete(e.fileId);break}}uploadFile(e){if(!this.worker)throw new Error("Worker not available");let t=new b;return this.subjects.set(e.fileId,t),this.worker.postMessage(m({type:"UPLOAD_FILE"},e)),t.asObservable()}pause(e){this.worker?.postMessage({type:"PAUSE_UPLOAD",fileId:e})}resume(e){this.worker?.postMessage({type:"RESUME_UPLOAD",fileId:e})}cancel(e){this.worker?.postMessage({type:"CANCEL_UPLOAD",fileId:e})}static{this.\u0275fac=function(t){return new(t||n)}}static{this.\u0275prov=o({token:n,factory:n.\u0275fac,providedIn:"root"})}}return n})();var D=(()=>{class n{constructor(e,t){this.workerService=e,this.translate=t,this.filesMap=new Map,this.filesSubject=new k([]),this.files$=this.filesSubject.asObservable()}get files(){return this.filesSubject.value}enqueueFiles(e,t){let{chunkSize:r,maxFileSizeMB:s,filterType:p,fileExtensions:l,maxRetries:i=3,retryDelayMs:w=500,onValidationMessage:c,getUploadUrlCb:S,finalizeCb:U,deleteSyncFileCb:g}=t;for(let a of e){if(a.name===".DS_Store")continue;if(a.size===0){c?.(`Skipping empty file: ${a.name}`);continue}if(a.size>s*1024*1024){c?.(`File too large: ${a.name}`);continue}if(!this.isAllowedExtension(a.name,p,l)){c?.(`File type not allowed: ${a.name}`);continue}let u=this.generateUUID(),d={id:u,file:a,name:a.name,progress:0,status:"queued",error:null,meta:null};this.filesMap.set(u,d),this.emitFiles(),this.processFile(u,d,{chunkSize:r,maxRetries:i,retryDelayMs:w,getUploadUrlCb:S,finalizeCb:U,deleteSyncFileCb:g})}}processFile(e,t,r){return f(this,null,function*(){t.status="uploading",this.emitFiles();try{let s=yield r.getUploadUrlCb(t);if(!s?.url)throw new Error("Signed URL missing");t.meta=s,this.emitFiles();let l=this.workerService.uploadFile({fileId:e,file:t.file,uploadUrl:s.url,chunkSize:r.chunkSize,maxRetries:r.maxRetries,retryDelayMs:r.retryDelayMs,extraForm:{deviceUUID:s.deviceUUID??"",batchId:s.batchId??"",odItemId:s.odItemId??""}}).subscribe({next:i=>f(this,null,function*(){i.type==="progress"?(t.progress=i.percent,this.emitFiles()):i.type==="chunk_ok"||(i.type==="done"?(t.progress=100,r.finalizeCb&&(yield r.finalizeCb(t,s)),t.status="completed",this.emitFiles()):i.type==="paused"?(t.status="paused",this.emitFiles()):i.type==="resumed"?(t.status="uploading",this.emitFiles()):i.type==="canceled"?(t.status="canceled",this.emitFiles(),l.unsubscribe(),this.filesMap.delete(e),this.emitFiles()):i.type==="error"&&(t.status="failed",t.error=i.error,this.emitFiles(),l.unsubscribe()))}),error:i=>{t.status="failed",t.error=i&&i.message?i.message:String(i),this.emitFiles()}})}catch(s){t.status="failed",t.error=s&&s.message?s.message:String(s),this.emitFiles()}})}pauseFile(e){this.workerService.pause(e);let t=this.filesMap.get(e);t&&(t.status="paused",this.emitFiles())}resumeFile(e){this.workerService.resume(e);let t=this.filesMap.get(e);t&&(t.status="uploading",this.emitFiles())}cancelFile(e){this.workerService.cancel(e);let t=this.filesMap.get(e);t&&(t.status="canceled",this.filesMap.delete(e),this.emitFiles())}removeFile(e){this.filesMap.delete(e),this.emitFiles()}emitFiles(){this.filesSubject.next([...this.filesMap.values()])}generateUUID(){return("10000000-1000-4000-8000"+-1e11).replace(/[018]/g,e=>(e^crypto.getRandomValues(new Uint8Array(1))[0]&15>>e/4).toString(16))}isAllowedExtension(e,t,r){let s=e.split(".").pop()?.toLowerCase()??"";return t==="inclusion"?r.includes("all")||r.includes(s):t==="exclusion"?!r.includes(s):!0}hasFilesUploading(){return this.hasFiles()}hasFiles(){return this.files.some(e=>["queued","uploading","paused"].includes(e.status))}static{this.\u0275fac=function(t){return new(t||n)(h(I),h(y))}}static{this.\u0275prov=o({token:n,factory:n.\u0275fac,providedIn:"root"})}}return n})();export{I as a,D as b};
