import{s as h}from"../chunks/index-B1TP9DOB.js";import{q as f,s as y,t as S,v as w,w as I,x as A,y as R}from"../chunks/db-DRCaoMWR.js";import"../chunks/types-D7xJyntT.js";function o(s){self.postMessage(s)}async function b(s,a,t){try{const{articles:r,totalCount:n}=await R(s,a,t);o({type:"loadFromDB:done",page:s,articles:r,totalCount:n})}catch(r){o({type:"loadFromDB:error",error:r instanceof Error?r.message:String(r)})}}async function g(s,a){const t=await h(),[r,n,m]=await Promise.all([f(),y(),S()]),i=[],d=new Set;for(const e of t){const c=await w({sourceId:e.sourceId,sourceSite:e.sourceSite,sourceSlug:e.sourceSlug,title:e.title,titleHash:e.titleHash,commentCount:e.commentCount,viewCount:e.viewCount,author:e.author,issueLinkUrl:e.url,rank:e.rank,pageNumber:e.pageNumber,publishedAt:e.publishedAt,recScore:0,recommended:!1,isRead:!1,createdAt:new Date().toISOString()});if(d.has(c)||(d.add(c),e.sourceId&&n.has(e.sourceId))||e.author&&m.has(e.author))continue;const p=r.has(c);if(a&&p)continue;const u=await I(c);u&&i.push({...u,id:c,isRead:p})}const l=t.map(e=>e.sourceId).filter(Boolean);return l.length>0&&A(l).catch(()=>{}),i}async function k(s,a){for(const t of s)try{const r=await g(t,a);o({type:"scrape:page",page:t,articles:r,modelReady:!1})}catch(r){o({type:"scrape:error",page:t,error:r instanceof Error?r.message:String(r)})}o({type:"scrape:done",totalPages:s.length})}self.onmessage=async s=>{const a=s.data;switch(a.type){case"loadFromDB":await b(a.page,a.pageSize,a.hideRead);break;case"scrape":await k(a.pages,a.hideRead);break;case"scrapeOne":try{const t=await g(a.page,a.hideRead);o({type:"scrape:page",page:a.page,articles:t,modelReady:!1}),o({type:"scrape:done",totalPages:1})}catch(t){o({type:"scrape:error",page:a.page,error:t instanceof Error?t.message:String(t)})}break}};
