import{d as I,a as D,n as x,z as R}from"./vue-DweYBrEc.js";import{T as U,y as N,S as W,j as O,Z as P,$ as H}from"./index-fNs72j9w.js";var a=(s=>(s[s.Waiting=0]="Waiting",s[s.Connecting=1]="Connecting",s[s.Executing=2]="Executing",s[s.Success=3]="Success",s[s.Failed=4]="Failed",s[s.Unknown=5]="Unknown",s))(a||{});const Z=I("terminal",()=>{const s=D({show:!1,showDot:!1,taskList:[],packageManager:"pnpm",showConfig:!1,automaticCleanupTask:"1",phpDevelopmentServer:!1,npmRegistry:"unknown",composerRegistry:"unknown"});function L(){for(const t in s.taskList)(s.taskList[t].status==a.Connecting||s.taskList[t].status==a.Executing)&&(s.taskList[t].status=a.Unknown)}function l(t=!s.show){s.show=t,t&&u(!1)}function u(t=!s.showDot){s.showDot=t}function p(t=!s.showConfig){l(!t),s.showConfig=t}function w(t,e){s[e=="npm"?"npmRegistry":"composerRegistry"]=t}function h(t){s.packageManager=t}function T(t){s.phpDevelopmentServer=t}function y(t){s.automaticCleanupTask=t}function o(t,e){s.taskList[t].status=e,(e==a.Failed||e==a.Unknown)&&s.taskList[t].blockOnFailure&&k(t,!0)}function r(t){if(typeof s.taskList[t].callback!="function")return;const e=s.taskList[t].status;e==a.Failed||e==a.Unknown?s.taskList[t].callback(a.Failed):e==a.Success&&s.taskList[t].callback(a.Success)}function k(t,e=!s.taskList[t].showMessage){s.taskList[t].showMessage=e}function f(t,e){s.show||u(!0),s.taskList[t].message=s.taskList[t].message.concat(e),x(()=>{M(s.taskList[t].uuid)})}function g(t,e=!0,n="",i=()=>{}){if(s.show||u(!0),s.taskList=s.taskList.concat({uuid:N(),createTime:U(),status:a.Waiting,command:t,message:[],showMessage:!1,blockOnFailure:e,extend:n,callback:i}),parseInt(s.automaticCleanupTask)===1&&d(),s.show===!1){for(const m in s.taskList)if(s.taskList[m].status==a.Failed||s.taskList[m].status==a.Unknown){R({type:"error",message:O.global.t("terminal.Newly added tasks will never start because they are blocked by failed tasks"),zIndex:W});break}}c()}function S(t,e=!0,n="",i=()=>{}){g(t+"."+s.packageManager,e,n,i)}function C(t){s.taskList[t].status!=a.Connecting&&s.taskList[t].status!=a.Executing&&s.taskList.splice(t,1),c()}function c(){let t=null;for(const e in s.taskList){if(s.taskList[e].status==a.Waiting){t=parseInt(e);break}if(s.taskList[e].status==a.Connecting||s.taskList[e].status==a.Executing)break;if(s.taskList[e].status!=a.Success&&(s.taskList[e].status==a.Failed||s.taskList[e].status==a.Unknown)){if(s.taskList[e].blockOnFailure)break;continue}}t!==null&&(o(t,a.Connecting),b(t))}function b(t){window.eventSource=new EventSource(P(s.taskList[t].command,s.taskList[t].uuid,s.taskList[t].extend)),window.eventSource.onmessage=function(e){const n=JSON.parse(e.data);if(!n||!n.data)return;const i=E(n.uuid);i!==!1&&(n.data=="command-exec-error"?(o(i,a.Failed),window.eventSource.close(),r(i),c()):n.data=="command-exec-completed"?(window.eventSource.close(),s.taskList[i].status!=a.Success&&o(i,a.Failed),r(i),c()):n.data=="command-link-success"?o(i,a.Executing):n.data=="command-exec-success"?o(i,a.Success):f(i,n.data))},window.eventSource.onerror=function(){window.eventSource.close();const e=F(t);e!==!1&&(o(e,a.Failed),r(e))}}function v(t){s.taskList[t].message=[],o(t,a.Waiting),c()}function d(){s.taskList=s.taskList.filter(t=>t.status!=a.Success)}function E(t){for(const e in s.taskList)if(s.taskList[e].uuid==t)return parseInt(e);return!1}function F(t){if(s.taskList[t])return t;{let e=-1;for(const n in s.taskList)(s.taskList[n].status==a.Connecting||s.taskList[n].status==a.Executing)&&(e=parseInt(n));return e===-1?!1:e}}function M(t){const e=document.querySelector(".exec-message-"+t);e&&e.scrollHeight&&(e.scrollTop=e.scrollHeight)}return{state:s,init:L,toggle:l,toggleDot:u,setTaskStatus:o,setTaskShowMessage:k,addTaskMessage:f,addTask:g,addTaskPM:S,delTask:C,startTask:c,retryTask:v,clearSuccessTask:d,toggleConfigDialog:p,changeRegistry:w,changePackageManager:h,changePHPDevelopmentServer:T,changeAutomaticCleanupTask:y}},{persist:{key:H,pick:["state.showDot","state.taskList","state.automaticCleanupTask","state.npmRegistry","state.composerRegistry"]}});export{a as t,Z as u};
