var e=e=>{let t=new DataView(e);if(t.byteLength<4||t.getUint16(0,!1)!==65496)return 1;let n=2;for(;n+4<=t.byteLength&&t.getUint8(n)===255;){let e=t.getUint8(n+1),r=t.getUint16(n+2,!1);if(r<2)break;if(e===225&&n+4+r<=t.byteLength){let e=n+4;if(String.fromCharCode(t.getUint8(e),t.getUint8(e+1),t.getUint8(e+2),t.getUint8(e+3),t.getUint8(e+4),t.getUint8(e+5))!==`Exif\0\0`)break;let r=e+6,i=t.getUint16(r,!1)===18761,a=r+t.getUint32(r+4,i);if(a+2>t.byteLength)break;let o=t.getUint16(a,i);a+=2;for(let e=0;e<o;e++){let n=a+e*12;if(n+12>t.byteLength)break;if(t.getUint16(n,i)===274)return t.getUint16(n+8,i)||1}}n+=2+r}return 1},t=async(e,t)=>{let n=e.naturalWidth||e.width,r=e.naturalHeight||e.height,i=t>=5&&t<=8,a=i?r:n,o=i?n:r,s=document.createElement(`canvas`);s.width=a,s.height=o;let c=s.getContext(`2d`,{willReadFrequently:!0});if(!c)throw Error(`canvas_not_supported`);c.save(),t===2&&(c.translate(a,0),c.scale(-1,1)),t===3&&(c.translate(a,o),c.rotate(Math.PI)),t===4&&(c.translate(0,o),c.scale(1,-1)),t===5&&(c.rotate(.5*Math.PI),c.scale(1,-1)),t===6&&(c.translate(a,0),c.rotate(.5*Math.PI)),t===7&&(c.translate(a,0),c.rotate(.5*Math.PI),c.scale(-1,1)),t===8&&(c.translate(0,o),c.rotate(-.5*Math.PI)),c.filter=`contrast(1.22) brightness(1.06) saturate(1.02)`,c.drawImage(e,0,0,n,r),c.restore();let l=document.createElement(`canvas`);l.width=a,l.height=o;let u=l.getContext(`2d`);return u?(u.filter=`blur(0.25px) contrast(1.12) brightness(1.03)`,u.drawImage(s,0,0),l):s},n=async(n,r=5e6)=>{if(!n)throw Error(`missing_file`);if(!/^image\/(png|jpe?g)$/i.test(String(n.type||``)))throw Error(`unsupported_image_type`);if(n.size>2e7)throw Error(`file_too_large`);let i=await n.arrayBuffer(),a=String(n.type||``).toLowerCase().includes(`jpeg`)?e(i):1,o=URL.createObjectURL(new Blob([i],{type:n.type||`image/jpeg`}));try{let e=new Image;e.decoding=`async`;let i=new Promise((t,n)=>{e.onload=()=>t(),e.onerror=()=>n(Error(`image_decode_failed`))});e.src=o,await i;let s=await t(e,a),c=n.type&&/^image\/png$/i.test(n.type)?`image/png`:`image/jpeg`,l=c===`image/jpeg`?.92:void 0,u=await new Promise(e=>s.toBlob(t=>e(t||new Blob),c,l));if(u.size>r){let e=await new Promise(e=>s.toBlob(t=>e(t||new Blob),`image/jpeg`,.86));if(e.size>r)throw Error(`file_too_large`);return{blob:e,mime:`image/jpeg`}}return{blob:u,mime:c}}finally{try{URL.revokeObjectURL(o)}catch{}}};export{n as t};