import{r as l,a1 as s,a2 as v}from"./BdHKhsMk.js";import{u as f}from"./D1SnSy9H.js";const y="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let I=(a=21)=>{let c="",e=crypto.getRandomValues(new Uint8Array(a|=0));for(;a--;)c+=y[e[a]&63];return c};const d=a=>{var e;if("dynamic"in a)return typeof a.dynamic=="object"&&"command"in a.dynamic?d({script:a.dynamic}):d({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"},i=(a,c)=>{var r;if("dynamic"in a)return typeof a.dynamic=="object"&&"command"in a.dynamic?i({script:a.dynamic},c):i({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 p=(a=>(a.Closed="closed",a.Opened="opened",a))(p||{});const k=a=>new EventSource("/event-stream?clientId=0"),n=l([]),g=l(!0),o=a=>n.value.findIndex(c=>c.id===a),S=a=>n.value.find(c=>c.id===a),h=async()=>{const a=await $fetch("/api/active-script");g.value=!1;const c=await f();n.value=a.scripts.map(e=>({package:c.value.packages[e.packageId]||null,packageId:e.packageId??null,commandId:e.commandId??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}))};h();v([n],([a])=>{g.value||$fetch("/api/active-script",{method:"POST",body:{scripts:a}})},{deep:!0});const u=l();u.value=k(),u.value.addEventListener("message",a=>{const c=JSON.parse(a.data),e=o(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 x=()=>({scripts:n,runScript:async e=>{var m;const t=await f();e.id??(e.id=I());const r="command"in e?e.command:(m=t.value.packages[e.packageId??""])==null?void 0:m.scripts[e.commandId];r&&(n.value=n.value.concat({package:"packageId"in e&&t.value.packages[e.packageId]||null,packageId:"packageId"in e?e.packageId??null:null,commandId:"commandId"in e?e.commandId:null,command:r,createdAt:new Date,id:e.id,logs:[],status:p.Opened}).slice(-10),s.success(`Running script ${d({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=>{s("Error Occurred",{description:t.data.message})})},getScriptIndex:o,getScript:S});export{p as S,i as a,d as g,I as n,x as u};
