import{i as w,m as f}from"./messages-CEPPz4B5.js";import{n as y}from"./region-DAq9HKph.js";const b="src/offscreen/index.html";async function l(){await chrome.offscreen.hasDocument()||await chrome.offscreen.createDocument({url:b,reasons:[chrome.offscreen.Reason.BLOBS],justification:"Canvas rendering and clipboard writes for screenshot beautification."})}async function M(){await chrome.offscreen.hasDocument()&&await chrome.offscreen.closeDocument()}async function d(){const[e]=await chrome.tabs.query({active:!0,currentWindow:!0});if(!e||e.windowId==null)throw new Error("No active tab");return chrome.tabs.captureVisibleTab(e.windowId,{format:"png"})}const g=2e4;function v({viewportH:e,pageH:a}){const n=Math.min(a,g);if(n<=e)return[{scrollTo:0,sliceTop:0,sliceHeight:n}];const t=[];let r=0;for(;r+e<n;)t.push({scrollTo:r,sliceTop:0,sliceHeight:e}),r+=e;const i=n-r,s=n-e;return t.push({scrollTo:s,sliceTop:e-i,sliceHeight:i}),t}const u=32767,h=268435456;function T(e,a){return e>u||a>u?{ok:!1,reason:`Page exceeds Chrome's ${u}px canvas limit on one side.`}:e*a>h?{ok:!1,reason:`Page exceeds Chrome's max canvas area (~${(h/1e6).toFixed(0)}M pixels).`}:{ok:!0}}const E=e=>new Promise(a=>setTimeout(a,e));async function A(){const[e]=await chrome.tabs.query({active:!0,currentWindow:!0});if(!(e!=null&&e.id))throw new Error("No active tab");await chrome.tabs.sendMessage(e.id,{kind:"fullpage-begin"}).catch(()=>{});try{const a=await chrome.tabs.sendMessage(e.id,{kind:"scroll-to",y:0});if(a.kind!=="scroll-done")throw new Error("Geometry probe failed");const n=Math.min(a.pageH,g),t=T(a.pageW*a.dpr,n*a.dpr);if(!t.ok)throw new Error(t.reason);const r=v({viewportH:a.viewportH,pageH:a.pageH}),i=[];let s=0;for(const c of r){if((await chrome.tabs.sendMessage(e.id,{kind:"scroll-to",y:c.scrollTo})).kind!=="scroll-done")throw new Error("Scroll-to failed");await E(550);const p=await d();i.push({dataUrl:p,sliceTop:c.sliceTop*a.dpr,sliceHeight:c.sliceHeight*a.dpr,y:s}),s+=c.sliceHeight*a.dpr}await l();const k=a.viewportW*a.dpr,o=await chrome.runtime.sendMessage({kind:"stitch-request",slices:i,width:k,totalHeight:s});if(o.kind==="stitch-result"&&o.ok)return o.dataUrl;throw new Error(o.kind==="stitch-result"?o.error:"Stitch failed")}finally{await chrome.tabs.sendMessage(e.id,{kind:"fullpage-end"}).catch(()=>{})}}chrome.runtime.onMessage.addListener((e,a,n)=>{if(!w(e))return!1;const t=e;return t.kind==="ping"?((async()=>(await l(),n(f())))(),!0):t.kind==="capture-viewport"?((async()=>{try{const r=await d();n({kind:"capture-result",ok:!0,dataUrl:r})}catch(r){n({kind:"capture-result",ok:!1,error:r.message})}})(),!0):t.kind==="start-region-select"?((async()=>{const[r]=await chrome.tabs.query({active:!0,currentWindow:!0});if(!(r!=null&&r.id)){n({kind:"region-cancelled"});return}await chrome.tabs.sendMessage(r.id,{kind:"start-region-select"}),n(f())})(),!0):t.kind==="region-selected"?((async()=>{try{const r=await d();await l();const i=await chrome.runtime.sendMessage({kind:"crop-request",dataUrl:r,region:y(t.region)});i.kind==="crop-result"&&i.ok?chrome.runtime.sendMessage({kind:"capture-result",ok:!0,dataUrl:i.dataUrl}).catch(()=>{}):i.kind==="crop-result"&&chrome.runtime.sendMessage({kind:"capture-result",ok:!1,error:i.error}).catch(()=>{})}catch(r){chrome.runtime.sendMessage({kind:"capture-result",ok:!1,error:r.message}).catch(()=>{})}})(),!1):t.kind==="capture-fullpage"?((async()=>{try{const r=await A();n({kind:"fullpage-result",ok:!0,dataUrl:r})}catch(r){n({kind:"fullpage-result",ok:!1,error:r.message})}})(),!0):t.kind==="beautify-request"||t.kind==="beautify-request-v2"||t.kind==="copy-request"||t.kind==="stitch-request"?((async()=>{await l();const r=await chrome.runtime.sendMessage(t);n(r)})(),!0):t.kind==="region-cancelled"?(chrome.runtime.sendMessage(t).catch(()=>{}),!1):(t.kind==="offscreen-ready"&&console.log("[bg] offscreen ready"),!1)});var m;(m=chrome.runtime.onSuspend)==null||m.addListener(()=>{M()});
