var i="image/webp",h="high";addEventListener("message",async({data:t})=>{try{let e=t.message instanceof File?await l(t.message):await w(t.message);postMessage({message:e,key:t.key})}catch(e){reportError(e)}});async function l(t){let a=await(await c(t)).convertToBlob({type:i,quality:.85});return g(a)}async function w(t){if(!t?.length)throw new Error("workers/image.ts: Empty image list provided");let e={};for(let a=0;a<t.length;a+=5){let s=t.slice(a,a+5);await Promise.all(s.map(async n=>{try{let o=await d(n),m=await(await c(o)).convertToBlob({type:i,quality:.85});e[n]=await g(m)}catch(o){console.error(o)}}))}return e}async function c(t){let e=await createImageBitmap(t),a=Math.min(1,512/Math.max(e.width,e.height)),s=Math.max(1,Math.round(e.width*a)),n=Math.max(1,Math.round(e.height*a)),o=new OffscreenCanvas(s,n),r=o.getContext("2d");if(!r)throw new Error("Failed to get 2D context");return r.imageSmoothingEnabled=!0,r.imageSmoothingQuality=h,r.drawImage(e,0,0,s,n),e.close?.(),o}function g(t){return new Promise((e,a)=>{let s=new FileReader;s.onload=()=>e(s.result),s.onerror=a,s.readAsDataURL(t)})}async function d(t){let e=await fetch(t.replace("imgs/","get_image.php?file="),{method:"GET",headers:{Accept:"image/*","Cache-Control":"no-cache"},mode:"cors",cache:"no-cache",credentials:"omit"});if(!e.ok)throw new Error(`${e.status}: Failed to fetch image from ${t}`);return e.blob()}
