import"./modulepreload-polyfill-B5Qt9EMX.js";const I=600*1e3,E=6e4,k=900*1e3;let i=null,g="idle",o=null,p=Promise.resolve(),m=null;const c=new Map,d=new Map;let u=null;const h=()=>crypto.randomUUID(),y=e=>{chrome.runtime.sendMessage(e).catch(()=>{})},w=()=>{u!==null&&(clearTimeout(u),u=null)},T=()=>{w()},l=()=>{w(),u=self.setTimeout(()=>{if(u=null,d.size>0||o||g==="recognizing"){l();return}i?.terminate(),i=null,m=null,p=Promise.resolve(),c.clear(),o=null,s("idle"),chrome.runtime.sendMessage({type:"lite-ocr-release"}).catch(()=>{})},k)},s=(e,t)=>{g=e,y({type:"lite-ocr-status",status:e,error:t})},a=(e,t,r)=>{const n=d.get(e);n&&(d.delete(e),clearTimeout(n.timeout),r?n.reject(r):n.resolve(t??""))},f=e=>{s("error",e.message),o=null;for(const t of d.keys())a(t,void 0,e);i?.terminate(),i=null},L=()=>i||(i=new Worker(new URL("/assets/liteOcrWorker-LltOlMTW.js",import.meta.url),{type:"module"}),i.onmessage=e=>{const t=e.data;t.status==="progress"?(s("loading"),y({type:"lite-ocr-progress",progress:t.progress})):t.status==="ready"?(s("ready"),a(t.id)):t.status==="result"?(s("ready"),a(t.id,t.output)):(a(t.id,void 0,new Error(t.error)),s("error",t.error))},i.onerror=e=>f(new Error(e.message||"Lite 识别组件加载失败。")),i.onmessageerror=()=>f(new Error("Lite 识别组件通信失败。")),i),v=(e,t)=>new Promise((r,n)=>{d.set(e.id,{resolve:r,reject:n,timeout:self.setTimeout(()=>a(e.id,void 0,new Error("Lite 识别超时，请重试")),t)}),L().postMessage(e)}),M=async()=>{if(T(),g==="ready")return;o||(s("loading"),o=v({action:"init",id:h()},I).then(()=>{}).catch(t=>{throw o=null,l(),t}));const e=o;await e,o===e&&(o=null)},P=async e=>{await M(),s("recognizing");const r=await(await fetch(e)).blob();return v({action:"predict",id:h(),image:new File([r],"screenshot.png",{type:r.type})},E)},_=e=>{if(T(),m?.dataUrl===e)return l(),Promise.resolve(m.output);const t=c.get(e);if(t)return t;const r=p.then(()=>P(e));return c.set(e,r),p=r.then(()=>{},()=>{}),r.then(n=>{n&&(m={dataUrl:e,output:n}),c.delete(e),l()},()=>{c.delete(e),l()}),r};chrome.runtime.onMessage.addListener((e,t,r)=>{if(!(e?.type!=="lite-ocr-init"&&e?.type!=="lite-ocr-predict"))return(e.type==="lite-ocr-init"?M().then(()=>(l(),"")):_(e.dataUrl)).then(n=>r({ok:!0,output:n})).catch(n=>r({ok:!1,error:n instanceof Error?n.message:String(n)})),!0});
