import{s as e}from"./preload-helper-CggyeTPP.js";import{t,u as n}from"./transcode-CQf3sO1K.js";import{t as r}from"./onboarding-4piCI0OP.js";import{n as i}from"./entitlement-DT8dRTvb.js";function a(e){return new Promise((t,n)=>{let r=new FileReader;r.onload=()=>t(r.result),r.onerror=()=>n(r.error??Error(`readAsDataURL failed`)),r.readAsDataURL(e)})}async function o(t,n,r=`uniquify`){let i=await a(t);return e.downloads.download({url:i,filename:n,saveAs:!1,conflictAction:r})}var s=null;async function c(){if(!await e.offscreen.hasDocument?.())return s||=e.offscreen.createDocument({url:`offscreen.html`,reasons:[`BLOBS`],justification:`本地 WASM 图像编码（AVIF 等），无网络外传。`}).catch(e=>{if(!String(e).includes(`Only a single offscreen`))throw e}).finally(()=>{s=null}),s}function l(e){return new Promise((t,n)=>{let r=new FileReader;r.onload=()=>{let e=r.result;t(e.slice(e.indexOf(`,`)+1))},r.onerror=()=>n(r.error),r.readAsDataURL(e)})}function u(e,t){let n=atob(e),r=new Uint8Array(n.length);for(let e=0;e<n.length;e++)r[e]=n.charCodeAt(e);return new Blob([r],{type:t})}var d=9e4;async function f(t,n,r){await c();let a=await l(t),o=await i(),s=`${Date.now()}-${Math.random().toString(36).slice(2)}`,f=await Promise.race([e.runtime.sendMessage({__recastOffscreen:!0,id:s,target:n,quality:r.quality,matte:r.matte,keepExif:r.keepExif,mime:t.type,dataB64:a,proof:o}),new Promise(e=>setTimeout(()=>e(null),d))]);if(!f?.ok||!f.dataB64)throw Error(f?.error||`offscreen encode failed`);return{blob:u(f.dataB64,f.mime||`application/octet-stream`),mime:f.mime||`application/octet-stream`,width:0,height:0,scaled:!1}}var p=class extends r{encode(e,r,i){return n(r)?f(e,r,i):t(e,{target:r,quality:i.quality,matte:i.matte,keepExif:i.keepExif})}save(e,t,n){return o(e,t,n)}};export{p as ChromeHost};