import{a8 as N,c6 as R,v as p,N as _,Q as H,J as K,c7 as L,g,i as U,q as v,c8 as z}from"./B08CJtNn.js";import{u as G}from"./eHv8qUqa.js";const X=N("BackgroundQueue",()=>{const c=p(!1),l=p(0),i=p([]),d=p([]),{kapi:I}=_(),m=H(),{t:f}=K(),y=G(),k=async(t,n,s,e=!1)=>{if(!t||!n||!s)return{error:new Error("PID, SID, and User ID are required")};const r=`${t}-${n}`,o=i.value.find(a=>a.key===r);if(o&&(o.status==="running"||o.foundComponents.length>0&&!e))return{error:null};try{const{runId:a}=await I("System").then(u=>u.triggerComponentFinder(Number(t),Number(n),s));return h(t,n,a,s),{error:null}}catch(a){return{error:a instanceof Error?a:new Error(String(a))}}},h=(t,n,s,e)=>{const r=`${t}-${n}`,o=i.value.find(u=>u.key===r),a=new Date().getTime()+6e5;o?(o.lastJobId=o.currentJobId,o.currentJobId=s,o.finderStep=0,o.status="waiting",o.foundComponents=[],o.groupedComponents={},o.suggestionTypes=y.selectCurrentSystem?.suggestionTypes||[],o.maxWaitTime=a,o.startedAt=new Date,o.userId=e):i.value.push({key:r,currentJobId:s,lastJobId:s,finderStep:0,status:"waiting",foundComponents:[],groupedComponents:{},suggestionTypes:y.selectCurrentSystem?.suggestionTypes||[],maxWaitTime:a,startedAt:new Date,userId:e}),m.add({title:f("JobStarted"),description:f("pageComponentFinder.pageComponentFinderStarted"),icon:v.icoLoadingBlocks,color:"success"}),c.value=!0,l.value++},C=(t,n,s,e,r)=>{const o=`${t}-${n}`,a=`${t}-${n}-${r}`,u=d.value.find(M=>M.key===o),J=e||"undefined";u?(u.keyId=a,u.status="start",u.crawlerType=r,u.startedAt=new Date,u.userId=s,u.crawlId=J):d.value.push({key:o,keyId:a,status:"start",crawlerType:r.replace("_",""),startedAt:new Date,userId:s,crawlId:J}),c.value=!0,l.value++},w=t=>t.maxWaitTime<Date.now()&&t.status==="running"?(t.status=t.foundComponents.length>0?"finished":"unfinished",!0):!1;L(()=>{i.value.forEach(t=>{w(t)})},6e5);const S=(t,n,s)=>{const e=i.value.find(r=>r.currentJobId===t);if(e&&e.status!=="finished"){if(w(e))return;if(e.status=n,e.finderStep=Math.min(e.finderStep,5),s){if(c.value=!0,s.componentIdentifier.startsWith("cat_")&&e.finderStep<5){e.finderStep=Math.min(e.finderStep+1,5);return}e.suggestionTypes.length>0&&e.suggestionTypes.includes(s.componentIdentifier)&&e.foundComponents.push(s)}}},b=(t,n,s)=>{const e=`${t}-${n}`;setTimeout(()=>{const r=d.value.find(o=>o.key===e);r?.crawlId==="undefined"&&(r.crawlId=s)},500)},T=(t,n)=>{const s=d.value.find(e=>e.crawlId===t);s&&((n==="finished"||n==="finish"||n==="abort")&&(s.finishedAt=new Date),s.status=n)},A=t=>t.reduce((n,s)=>((n[s.componentIdentifier]||=[]).push(s),n),{}),$=t=>{const n=i.value.find(s=>s.currentJobId===t);if(n&&n.status!=="finished"){const s=n.currentJobId;n.status="finished",n.lastJobId=s,n.finderStep=5,n.groupedComponents=A(n.foundComponents),c.value=!0,m.add({title:f("JobFinished"),description:f("pageComponentFinder.pageComponentFinderFinished"),icon:v.icoFinished,color:"success"})}},F=(t,n)=>{const s=`${t}-${n}`;return i.value.find(e=>e.key===s)},D=g(()=>i.value.filter(t=>t.status==="running").length),P=g(()=>{const t=i.value.map(e=>({key:e.key,status:e.status,startedAt:e.startedAt,runId:e.currentJobId})),n=d.value.map(e=>{const r={start:"running",run:"running",running:"running",abort:"unfinished",results:"finished",finish:"finished",finished:"finished"};return{key:e.key,status:r[e.status],startedAt:e.startedAt,crawlerType:e.crawlerType,runId:0,finishedAt:e.finishedAt||void 0}});return[...t,...n].reduce((e,r)=>{e[r.key]||(e[r.key]={});const o=r.crawlerType?r.crawlerType.replace("_",""):"finder";return e[r.key]={...e[r.key],[o]:{status:r.status,startedAt:r.startedAt,runId:r.runId,finishedAt:r.finishedAt||void 0}},e},{})}),x=()=>{l.value=0,c.value=!1},Q=()=>{i.value=[],d.value=[]},W=(t,n)=>{const s=`${t}-${n}`;return d.value.filter(e=>e.key===s)},B=g(()=>d.value.filter(t=>t.status==="running"||t.status==="start").length),E=U(),q=g(()=>{const{pid:t,sid:n}=E.params;return!t||!n?[]:d.value.filter(e=>e.key===`${t}-${n}`).map(e=>({status:e.status,crawlId:e.crawlId,crawlerType:e.crawlerType}))});return{triggerPageComponentFinder:k,pageComponentFinderJobs:i,finishPageComponentFinderJob:$,updatePageComponentFinderJob:S,addPageComponentFinderJob:h,newJobs:c,newJobsSeen:x,globalJobs:P,countActiveJobs:D,getPageComponentFinderJob:F,seenJobCount:l,flushJobQueue:Q,addCrawlerJob:C,crawlerJobs:d,updateCrawlerJob:T,addCrawlerId:b,getCrawlerJobStates:W,countAllRunningCrawlerJobs:B,crawlerStatusForCurrentProject:q}},{persist:{key:`${R("sqm")}-backgroundQueue`,storage:z.sessionStorage()}});export{X as u};
