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