import{aZ as S,_ as m}from"./index-6dc81347.js";const T=S({id:"common",state:()=>({eventSource:null,log_cache:[],sse_connected:!1,log_cache_max_len:1e3,startTime:-1,pluginMarketData:[]}),actions:{async createEventSource(){if(await(async()=>{try{const a=await m.get("/api/log-history");a.data.data.logs?this.log_cache.push(...a.data.data.logs):this.log_cache=[]}catch(a){console.error("Failed to fetch log history:",a)}})(),this.eventSource)return;const h=new AbortController,{signal:e}=h,c={"Content-Type":"multipart/form-data",Authorization:"Bearer "+localStorage.getItem("token")};fetch("/api/live-log",{method:"GET",headers:c,signal:e,cache:"no-cache"}).then(a=>{if(!a.ok)throw new Error(`SSE connection failed: ${a.status}`);console.log("SSE stream opened"),this.sse_connected=!0;const i=a.body.getReader(),g=new TextDecoder;let n="";const l=s=>{n+=s;try{const t=JSON.parse(n);return n="",t}catch{return null}},d=({done:s,value:t})=>{const f=t?t.byteLength:0;if(console.log(`Received ${f} bytes from live log`),s){console.log("SSE stream closed"),setTimeout(()=>{this.eventSource=null,this.createEventSource()},2e3);return}return g.decode(t).split(`

`).forEach(u=>{if(u.trim())if(u.startsWith("data:")){const o=u.substring(5).trim();let p={};try{p=JSON.parse(o)}catch{console.warn("Invalid JSON:",o);const _=l(o);if(_)p=_;else return}p.type==="log"&&(this.log_cache.push(p),this.log_cache.length>this.log_cache_max_len&&this.log_cache.shift())}else{const o=l(u);o&&o.type==="log"&&(this.log_cache.push(o),this.log_cache.length>this.log_cache_max_len&&this.log_cache.shift())}}),i.read().then(d)};i.read().then(d)}).catch(a=>{console.error("SSE error:",a),this.log_cache.push("SSE Connection failed, retrying in 5 seconds..."),setTimeout(()=>{this.eventSource=null,this.createEventSource()},1e3)}),this.eventSource=h},closeEventSourcet(){this.eventSource&&(this.eventSource.abort(),this.eventSource=null)},getLogCache(){return this.log_cache},getStartTime(){if(this.startTime!==-1)return this.startTime;m.get("/api/stat/start-time").then(r=>{this.startTime=r.data.data.start_time})},async getPluginCollections(r=!1){if(!r&&this.pluginMarketData.length>0)return Promise.resolve(this.pluginMarketData);const h=r?"/api/plugin/market_list?force_refresh=true":"/api/plugin/market_list";return m.get(h).then(e=>{var a,i,g,n,l,d,s;let c=[];for(let t in e.data.data)c.push({name:t,desc:e.data.data[t].desc,author:e.data.data[t].author,repo:e.data.data[t].repo,installed:!1,version:(a=e.data.data[t])!=null&&a.version?e.data.data[t].version:"未知",social_link:(i=e.data.data[t])==null?void 0:i.social_link,tags:(g=e.data.data[t])!=null&&g.tags?e.data.data[t].tags:[],logo:(n=e.data.data[t])!=null&&n.logo?e.data.data[t].logo:"",pinned:(l=e.data.data[t])!=null&&l.pinned?e.data.data[t].pinned:!1,stars:(d=e.data.data[t])!=null&&d.stars?e.data.data[t].stars:0,updated_at:(s=e.data.data[t])!=null&&s.updated_at?e.data.data[t].updated_at:""});return this.pluginMarketData=c,c}).catch(e=>Promise.reject(e))}}});export{T as u};
