import{b9 as q,b2 as G,a_ as K,iX as U,r,a7 as Y,aj as Z,an as L,b8 as A,ao as T,b as B,j as a,b0 as h,aX as D,aH as E,Q as tt,bx as at,ay as et,aC as st,hT as ot,a$ as nt,o as rt,cR as ct,fj as it}from"./index-ea7817a2.js";import{b as dt}from"./index-077fa3a4.js";import{J as I}from"./JobsService-82e820b4.js";import{u as lt}from"./useDownload-09eaf3eb.js";q.DATE_TIME;const ft=({projectId:n,isCopy:j=!1,item:t,isOpenPopup:l,handleTogglePopup:e,handleStopExecutions:M,isLog:i=!1,isStop:d=!1})=>{var J;const u=G(),p=K(),[y,{isLoading:R}]=U(),[C,{isLoading:V}]=dt(),[o,H]=r.useState(null),[w,F]=r.useState(!0),$=Y().shape({appName:Z.label("Name")}),[c,g]=r.useState("stdout"),x=L({defaultValues:{appName:"",defaultArgs:"",...!!t&&!i&&!d&&{appName:`${t.name}_${A(new Date,"HH_mm_ss")}`}},resolver:T($),reValidateMode:"onChange",mode:"onChange"}),f=L({defaultValues:{args:((J=t.config)==null?void 0:J.defaultArgs)||""},mode:"onChange"}),N=r.useCallback(async()=>{const s=await I.getExecutionsLogs({projectId:t.projectId??n,jobName:t.jobName,executionId:t.executionId,logsType:"out"}),m=await I.getExecutionsLogs({projectId:t.projectId??n,jobName:t.jobName,executionId:t.executionId,logsType:"err"}),S={stdout:s.data.log,stdoutPath:s.data.path,stderr:m.data.log,stderrPath:m.data.path};S&&(H(S),F(!1))},[t.executionId,t.jobName,t.projectId,n]);r.useEffect(()=>{i&&t&&N()},[N,i,t]);const _=lt(),P=async()=>{_(n,c==="stdout"?o.stdoutPath:o.stderrPath,it.DATASET)},z=()=>{const s=c==="stdout"&&o?o.stdout:o.stderr;if(s!=null&&s.startsWith("Log is too big to display in browser."))return P},{handleSubmit:k,formState:{errors:Q}}=x,{handleSubmit:v}=f,W=r.useCallback(()=>k(async s=>{const{name:m}=await y({pId:n,data:{...t.config,appName:s.appName},name:s.appName}).unwrap();e(),u(B.jobs.overview,{jobName:m})})(),[k,y,e,t.config,u,n]),X=r.useCallback(()=>v(async s=>{n&&t&&e&&C({pId:`${n}`,name:t.name,data:s.args}).unwrap().then(()=>{p.info({title:"Info",content:"Job started"}),e(),u(B.jobs.executions,{jobName:t.name})})})(),[e,t,u,p,n,v,C]),O=r.useMemo(()=>[{title:"stdout",isActive:c==="stdout",onClick:()=>g("stdout")},{title:"stderr",isActive:c==="stderr",onClick:()=>g("stderr")}],[c]);let b=" ";return o&&(b=c==="stdout"&&o?o.stdout:o.stderr,b||(b="Logs unavailable")),a.jsxs(a.Fragment,{children:[!j&&!i&&!d&&a.jsx(h,{title:"Run job",isOpen:l,onClose:e,secondaryButtonProps:{children:"Cancel",onClick:e},mainButtonProps:{children:"Run",onClick:X,isLoading:V},children:a.jsx(D,{...f,children:a.jsx(E,{defaultValue:t.config.defaultArgs,label:"Arguments",placeholder:"enter arguments",labelProps:{width:"100%"},...f.register("args")})})}),j&&!i&&!d&&a.jsx(h,{title:"Make a copy",isOpen:l,onClose:e,mainButtonProps:{children:"Create copy",onClick:W,isLoading:R},secondaryButtonProps:{children:"Cancel",onClick:e},children:a.jsxs(D,{...x,children:[a.jsx(tt,{flexDirection:"column",children:a.jsx(at,{sx:{fontSize:"12px",fontFamily:"Inter",fontWeight:500,mb:"20px"},children:"Copy will create a new job with same file and configuration"})}),a.jsx(E,{defaultValue:`${t.name}${A(new Date,"dd-MM-yyyy")}`,type:"text",width:"100%",height:"100%",label:"Name of the new job",labelProps:{width:"100%"},...x.register("appName"),...et("appName",Q)})]})}),i&&!d&&a.jsxs(st,{size:"7xl",isOpen:l,onClose:e,hasCloseButton:!0,title:a.jsx(ot,{title:"Logs",tabs:O,width:"90%"}),bodyProps:{pt:0,mt:"10px"},children:[w&&a.jsx(nt,{}),o&&!w&&a.jsx(rt,{m:"20px",mt:0,children:a.jsx(ct,{title:c,wrapLongLines:!0,copyButton:!0,downloadButton:!0,downloadCallback:z(),content:b,language:"haskell"})})]}),d&&a.jsx(h,{isOpen:l,onClose:e,title:"Stop execution",secondaryText:"Are you sure that you want to stop this execution?",mainButtonProps:{children:"Stop execution",onClick:()=>M},secondaryButtonProps:{children:"Back",onClick:e}})]})};export{ft as J};
