import{aS as m,_ as S}from"./index-e345ed95.js";const f=m({id:"common",state:()=>({eventSource:null,log_cache:[],sse_connected:!1,log_cache_max_len:1e3,startTime:-1,pluginMarketData:[]}),actions:{createEventSource(){if(this.eventSource)return;const o=new AbortController,{signal:t}=o,n={"Content-Type":"multipart/form-data",Authorization:"Bearer "+localStorage.getItem("token")};fetch("/api/live-log",{method:"GET",headers:n,signal:t,cache:"no-cache"}).then(e=>{if(!e.ok)throw new Error(`SSE connection failed: ${e.status}`);console.log("SSE stream opened"),this.sse_connected=!0;const r=e.body.getReader(),s=new TextDecoder,i=({done:c,value:d})=>{if(c){console.log("SSE stream closed"),setTimeout(()=>{this.eventSource=null,this.createEventSource()},2e3);return}return s.decode(d).split(`

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