import{s as e}from"./preload-helper-CggyeTPP.js";import{d as t,u as n}from"./transcode-CQf3sO1K.js";import{t as r}from"./encode-DeoZio-E.js";var i=128*1024*1024;function a(e){if(typeof e.id!=`string`||!e.id)return`bad_request: id`;let r=e.target;if(typeof r!=`string`||!(t(r)||n(r)))return`bad_request: target`;if(typeof e.quality!=`number`||!(e.quality>=0&&e.quality<=1))return`bad_request: quality`;if(typeof e.matte!=`string`||e.matte.length>32)return`bad_request: matte`;if(typeof e.keepExif!=`boolean`)return`bad_request: keepExif`;if(typeof e.mime!=`string`||e.mime.length>128)return`bad_request: mime`;if(typeof e.dataB64!=`string`||!e.dataB64)return`bad_request: dataB64`;if(e.dataB64.length>i)return`bad_request: too_large`;if(e.proof!==void 0){let t=e.proof;if(typeof t!=`object`||!t)return`bad_request: proof`;for(let e of[`license`,`voucher`]){let n=t[e];if(n!=null&&(typeof n!=`string`||n.length>8192))return`bad_request: proof`}}return null}function o(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})}function s(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)})}e.runtime.onMessage.addListener((t,n,i)=>{let c=t;if(c?.__recastOffscreen&&n.id===e.runtime.id)return(async()=>{try{let e=a(c);if(e){i({ok:!1,id:c.id,error:e});return}let{blob:t,mime:n}=await r(o(c.dataB64,c.mime),{target:c.target,quality:c.quality,matte:c.matte,keepExif:c.keepExif},c.proof);i({ok:!0,id:c.id,mime:n,dataB64:await s(t)})}catch(e){i({ok:!1,id:c.id,error:e.message})}})(),!0});