import{r as d,a1 as m,a2 as v}from"./Dyw2fVvW.js";const i=d(null),g=async()=>(await(async()=>{i.value||(i.value=await $fetch("/api/config").then(c=>c.config))})(),i),h="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let I=(a=21)=>{let c="",e=crypto.getRandomValues(new Uint8Array(a|=0));for(;a--;)c+=h[e[a]&63];return c};const l=a=>{var e;if("dynamic"in a)return typeof a.dynamic=="object"&&"command"in a.dynamic?l({script:a.dynamic}):l({command:a.dynamic});if("script"in a&&a.script.label)return a.script.label;const c="command"in a?a.command:(e=a.script)==null?void 0:e.command;return typeof c=="object"?"npm"in c?c.npm:"shell"in c?c.shell:"Custom Function":c??"Custom Function"},u=(a,c)=>{var r;if("dynamic"in a)return typeof a.dynamic=="object"&&"command"in a.dynamic?u({script:a.dynamic},c):u({command:a.dynamic},c);const e="command"in a?a.command:(r=a.script)==null?void 0:r.command;let t="";return typeof e=="object"?("package"in e&&e.package===!0&&(t+=`cd ${c} && `),"npm"in e?t+=`npm run ${e.npm}`:"shell"in e?t+=e.shell:t+="::custom_function::"):typeof e=="string"?t+=e:t+="::custom_function::",t};var o=(a=>(a.Closed="closed",a.Opened="opened",a))(o||{});const k=a=>new EventSource("/event-stream?clientId=0"),n=d([]),p=d(!0),y=a=>n.value.findIndex(c=>c.id===a),S=a=>n.value.find(c=>c.id===a),x=async()=>{const a=await $fetch("/api/active-script");p.value=!1;const c=await g();n.value=a.scripts.map(e=>({package:c.value.packages[e.packageId]||null,packageId:e.packageId??null,commandIndex:e.commandIndex??null,command:e.command,createdAt:e.createdAt,id:e.id,logs:e.logs.map(t=>({date:new Date(t.date),text:t.text,type:t.type})),status:e.status}))};x();v([n],([a])=>{p.value||$fetch("/api/active-script",{method:"POST",body:{scripts:a}})},{deep:!0});const f=d();f.value=k(),f.value.addEventListener("message",a=>{const c=JSON.parse(a.data),e=y(c.id);if(n.value[e]){if(c.message){const t={text:c.message,date:new Date,type:c.type};n.value[e].logs=n.value[e].logs.concat(t).slice(-100)}c.status&&(n.value[e].status=c.status)}});const b=()=>({scripts:n,runScript:async e=>{var s;const t=await g();e.id??(e.id=I());const r="command"in e?e.command:(s=t.value.packages[e.packageId??""])==null?void 0:s.scripts[e.commandIndex];r&&(n.value=n.value.concat({package:"packageId"in e&&t.value.packages[e.packageId]||null,packageId:"packageId"in e?e.packageId??null:null,commandIndex:"commandIndex"in e?e.commandIndex:null,command:r,createdAt:new Date,id:e.id,logs:[],status:o.Opened}).slice(-10),m.success(`Running script ${l({dynamic:r})}.`),await $fetch("/api/run-script",{method:"POST",body:e,timeout:1e3}))},killScript:async e=>{e!==void 0&&await $fetch("/api/kill-script",{method:"POST",body:{id:e}}).catch(t=>{m("Error Occurred",{description:t.data.message})})},getScriptIndex:y,getScript:S});export{o as S,u as a,g as b,l as g,I as n,b as u};
