import{r as e}from"./notifications-BrcI-_N-.js";var t=new Map,n=new Map,r=new Set,i=[],a=!0,o=0,s=new Map,c=()=>{try{let e=new Uint8Array(10);return crypto.getRandomValues(e),`t_${Array.from(e).map(e=>e.toString(16).padStart(2,`0`)).join(``)}`}catch{return`t_${Date.now().toString(16)}${Math.random().toString(16).slice(2,8)}`}},l=()=>{a=!0,r.forEach(e=>{try{e()}catch{}})},u=e=>(r.add(e),()=>{r.delete(e)}),d=()=>(a&&=(i=Array.from(t.values()).sort((e,t)=>t.startedAt===e.startedAt?(s.get(t.id)||0)-(s.get(e.id)||0):t.startedAt-e.startedAt),!1),i),f=e=>t.get(e),p=e=>{let n;for(let r of t.values())r.tool===e&&(!n||r.startedAt>n.startedAt)&&(n=r);return n},m=e=>{let n=c(),r={id:n,tool:e.tool,toolLabel:e.toolLabel,path:e.path,title:e.title.slice(0,60),status:`running`,startedAt:Date.now(),snapshot:e.snapshot,seen:!1};return t.set(n,r),s.set(n,++o),l(),n},h=(e,t)=>{n.set(e,t)},g=(e,n)=>{let r=t.get(e);if(!r)return;let i=r.snapshot,a=typeof n==`function`?n(i):{...i,...n};t.set(e,{...r,snapshot:a}),l()},_={running:`进行中`,done:`已完成`,failed:`失败`,cancelled:`已取消`},v=(r,i,a={})=>{let o=t.get(r);if(!o)return;let s={...o,status:i,finishedAt:Date.now()};if(a.error!==void 0&&(s.error=a.error),a.pointsCharged!==void 0&&(s.pointsCharged=a.pointsCharged),t.set(r,s),n.delete(r),l(),a.notify===!1||i===`cancelled`)return;let c=_[i];e({key:`task:${r}`,title:a.notifyTitle||`${s.toolLabel}${c}`,description:a.notifyDescription||(i===`done`?`${s.title}${typeof s.pointsCharged==`number`&&s.pointsCharged>0?`（消耗 ${s.pointsCharged} 积分）`:``}`:s.error||`任务未能完成`),path:s.path,ttlMs:i===`done`?6e3:7e3,browser:!0})},y=e=>{let t=n.get(e);try{t?.abort()}catch{}v(e,`cancelled`,{notify:!1})},b=e=>{let n=t.get(e);!n||n.seen||(t.set(e,{...n,seen:!0}),l())},x=e=>{let r=t.get(e);r&&r.status!==`running`&&(t.delete(e),n.delete(e),s.delete(e),l())},S=()=>{let e=0;for(let n of t.values())(n.status===`done`||n.status===`failed`)&&!n.seen&&(e+=1);return e},C=()=>{let e=0;for(let n of t.values())n.status===`running`&&(e+=1);return e},w=new Map,T=e=>{let n=String(e.tool||``).trim();if(!n)return;let r=w.get(n);if(e.status===`running`){if(r){let e=t.get(r.taskId);if(e&&e.status===`running`)return}let i=m({tool:`web:${n}`,toolLabel:e.label,path:e.path,title:e.title||e.label,snapshot:{}});w.set(n,{taskId:i,lastStatus:`running`});return}if(r&&r.lastStatus===e.status)return;let i=r?.taskId&&t.get(r.taskId)?.status===`running`?r.taskId:null;if(!i){r?r.lastStatus=e.status:w.set(n,{taskId:``,lastStatus:e.status});return}if(w.set(n,{taskId:i,lastStatus:e.status}),e.status===`cancelled`){v(i,`cancelled`,{notify:!1});return}v(i,e.status,{error:e.error,pointsCharged:e.pointsCharged??null})};export{S as a,v as c,d,b as f,C as i,p as l,u as m,h as n,m as o,g as p,T as r,x as s,y as t,f as u};