import"./chunks/modulepreload-polyfill-B5Qt9EMX.js";import{g as $}from"./chunks/index-BJDgL5eq.js";import{n as b,o as C}from"./chunks/db-CwROBlWC.js";import{a as y}from"./chunks/error-log-iDKIFPsV.js";import"./chunks/default-board-urls-BG17b9Kz.js";import"./chunks/idb-kv-Dhbo4CRD.js";async function k(c=[]){const d=$(),r=[];let o=0;const l=new Set(c),i=d.filter(t=>!l.has(t.slug)&&!l.has(t.displayName)),w=b().catch(()=>new Set),h=i.length;let p=0;console.log(`[offscreen] Starting background scrape of ${h} sites...`);function u(t,g,e){try{chrome.runtime.sendMessage({type:"scrape-progress",completed:p,total:h,currentSite:t,articlesCount:o,scrapingSites:g||[],failedSite:e||"",errors:r.slice()})}catch{}}u("",i.map(t=>t.displayName).slice(0,5));const f=10,m=[];for(let t=0;t<i.length;t+=f)m.push(i.slice(t,t+f));for(const t of m){const g=t.map(e=>e.displayName);u("",g),await Promise.allSettled(t.map(async e=>{try{console.log(`[offscreen] Scraping ${e.slug}...`);const a=await e.scrapeAll();if(console.log(`[offscreen] ${e.slug}: got ${a.length} articles`),e.lastErrors&&e.lastErrors.length>0){for(const s of e.lastErrors)r.push(s);y(e.lastErrors.map(s=>`[scrape] ${s}`)).catch(()=>{})}const n=await w,N=new Date().toISOString(),S=await C(a.map(s=>({sourceId:s.sourceId,sourceSite:s.sourceSite,sourceSlug:s.sourceSlug,title:s.title,titleHash:s.titleHash,commentCount:s.commentCount,viewCount:s.viewCount,author:s.author,issueLinkUrl:s.url,originalUrl:s.url,rank:s.rank,pageNumber:s.pageNumber,publishedAt:s.publishedAt,recScore:0,recommended:!1,isRead:!1,createdAt:N})),n);return p++,o+=S,u(e.displayName,void 0,e.lastErrors?.length?e.displayName:void 0),S}catch(a){const n=`${e.slug}: ${a instanceof Error?a.message:String(a)}`;return r.push(n),console.warn(`[offscreen] ${n}`),y([`[scrape] ${n}`]).catch(()=>{}),p++,u(e.displayName,void 0,e.displayName),0}}))}return console.log(`[offscreen] Background scrape complete: ${o} articles, ${r.length} errors`),{count:o,errors:r}}chrome.runtime.onMessage.addListener((c,d,r)=>{if(c.type==="scrape-all-sites")return k(c.disabledSites||[]).then(({count:o,errors:l})=>{r({type:"scrape-all-done",articlesCount:o,errors:l})}).catch(o=>{r({type:"scrape-all-error",error:o instanceof Error?o.message:String(o)})}),!0});
