let c=null,d=null,s=null,l=null,w=null;async function p(t){switch(t){case"webp":c||(c=(await import("./index-C6gZtoa3.js")).encode);break;case"avif":d||(d=(await import("./index-CNmw7YHP.js")).encode);break;case"jpeg":s||(s=(await import("./index-Cjum_-Wu.js")).encode);break;case"png":l||(l=(await import("./index-Ccg-KFP7.js")).encode);break}}async function z(){w||(w=(await import("./index-Dc9k37mQ.js")).default)}function v(t,e,a,o){let n=t,r=e;return a&&n>a&&(r=Math.round(r*(a/n)),n=a),o&&r>o&&(n=Math.round(n*(o/r)),r=o),{width:Math.max(1,n),height:Math.max(1,r)}}function g(t){return new Promise((e,a)=>{const o=new FileReader;o.onload=()=>e(o.result),o.onerror=()=>a(new Error("Failed to read blob")),o.readAsDataURL(t)})}function y(t){switch(t){case"webp":return"image/webp";case"avif":return"image/avif";case"jpeg":return"image/jpeg";case"png":return"image/png";default:return"application/octet-stream"}}async function M(t,e,a){switch(await p(e),e){case"webp":if(!c)throw new Error("WebP encoder not loaded");return c(t,{quality:a});case"avif":if(!d)throw new Error("AVIF encoder not loaded");return d(t,{quality:a});case"jpeg":if(!s)throw new Error("JPEG encoder not loaded");return s(t,{quality:a});case"png":if(!l)throw new Error("PNG encoder not loaded");return l(t);default:throw new Error(`Unsupported format: ${e}`)}}async function U(t,e,a){a(10);const o=await createImageBitmap(t);a(20);const{width:n,height:r}=v(o.width,o.height,e.maxWidth,e.maxHeight);a(30);const i=new OffscreenCanvas(o.width,o.height),f=i.getContext("2d");if(!f)throw new Error("Failed to get canvas context");f.drawImage(o,0,0),o.close(),a(40);let h=f.getImageData(0,0,i.width,i.height);if(a(50),(n!==i.width||r!==i.height)&&(await z(),w&&(h=await w(h,{width:n,height:r}))),e.format==="original"){a(100);const E=await g(t);return{blob:t,dataUrl:E,width:o.width,height:o.height,originalSize:t.size,optimizedSize:t.size,savings:0,format:"original"}}a(60),await p(e.format),a(70);const m=await M(h,e.format,e.quality);a(90);const u=new Blob([m],{type:y(e.format)}),b=await g(u);return a(100),{blob:u,dataUrl:b,width:n,height:r,originalSize:t.size,optimizedSize:m.byteLength,savings:Math.round((1-m.byteLength/t.size)*100),format:e.format}}self.onmessage=async t=>{const{id:e,type:a,file:o,options:n}=t.data;if(a!=="cancel"){if(!o){self.postMessage({id:e,error:"No file provided"});return}try{const r=await U(o,n,i=>{self.postMessage({id:e,progress:i})});self.postMessage({id:e,result:r,progress:100})}catch(r){self.postMessage({id:e,error:r instanceof Error?r.message:"Unknown error"})}}};
