const l=async(n,t=.8,c=1600)=>new Promise((s,r)=>{const a=new FileReader;a.readAsDataURL(n),a.onload=o=>{var m;const e=new Image;e.src=(m=o.target)==null?void 0:m.result,e.onload=()=>{const i=document.createElement("canvas");let g=e.width,d=e.height;g>c&&(d=Math.round(d*c/g),g=c),i.width=g,i.height=d;const w=i.getContext("2d");if(!w)return r(new Error("Canvas not available"));w.fillStyle="#FFFFFF",w.fillRect(0,0,g,d),w.drawImage(e,0,0,g,d),i.toBlob(h=>{if(!h)return r(new Error("Compression failed"));const u=new File([h],n.name.replace(/\.[^/.]+$/,"")+".jpg",{type:"image/jpeg",lastModified:Date.now()});s(u)},"image/jpeg",t)},e.onerror=i=>r(i)},a.onerror=o=>r(o)}),v=async n=>n.type==="image/svg+xml"&&n.size<51200?n:l(n),F=async(n,t)=>{const c=await new Promise((a,o)=>{const e=new Image;e.addEventListener("load",()=>a(e)),e.addEventListener("error",m=>o(m)),e.setAttribute("crossOrigin","anonymous"),e.src=n}),s=document.createElement("canvas"),r=s.getContext("2d");if(!r)throw new Error("No 2d context");return s.width=t.width,s.height=t.height,r.drawImage(c,t.x,t.y,t.width,t.height,0,0,t.width,t.height),new Promise((a,o)=>{s.toBlob(e=>{if(!e){o(new Error("Canvas empty"));return}a(e)},"image/jpeg")})};export{F as g,v as o};
