var i="image/webp",d="high";addEventListener("message",async({data:t})=>{try{let e=t.message,a=e instanceof File?await h(e):await f(t.key,e);postMessage({type:"message",message:a,key:t.key})}catch(e){reportError(e)}});async function h(t){let a=await(await c(t)).convertToBlob({type:i,quality:.85});return g(a)}async function f(t,e){if(!e?.length)throw new Error("workers/image.ts: Empty image list provided");let a={};for(let s=0;s<e.length;s+=5){let r=e.slice(s,s+5);await Promise.all(r.map(async o=>{try{let n=await y(o),l=await(await c(n)).convertToBlob({type:i,quality:.85}),m=await g(l);a[o]=w(m)}catch(n){console.error(n)}finally{postMessage({type:"progress",key:t,message:void 0})}}))}return a}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)),r=Math.max(1,Math.round(e.height*a)),o=new OffscreenCanvas(s,r),n=o.getContext("2d");if(!n)throw new Error("Failed to get 2D context");return n.imageSmoothingEnabled=!0,n.imageSmoothingQuality=d,n.drawImage(e,0,0,s,r),e.close?.(),o}function g(t){return new Promise((e,a)=>{let s=new FileReader;s.addEventListener("load",()=>e(s.result?.toString()??"")),s.addEventListener("error",r=>a(r)),s.readAsDataURL(t)})}function w(t){let e=t.indexOf(",");if(e===-1||!t.slice(0,e).includes(";base64"))throw new Error("Invalid base64 data URL");return t.slice(e+1)}async function y(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()}
