import{b8 as K,b1 as Y,aZ as Z,iY as q,r,a7 as Q,ai as X,am as J,b7 as h,an as T,e as B,j as a,a$ as p,aW as E,aG as S,a as tt,bw as at,ax as et,aB as st,hU as ot,a_ as nt,O as rt,cO as ct,ff as it}from"./index-d6499a22.js";import{b as dt}from"./index-68a12294.js";import{J as I}from"./JobsService-aa6e1c55.js";import{u as lt}from"./useDownload-7167ff3b.js";const{DATE_TIME_FULL:ut}=K.DATE_TIME,ht=({projectId:n,isCopy:w=!1,item:t,isOpenPopup:l,handleTogglePopup:e,handleStopExecutions:M,isLog:c=!1,isStop:d=!1})=>{var A;const u=Y(),y=Z(),[j,{isLoading:F}]=q(),[C,{isLoading:_}]=dt(),[o,$]=r.useState(null),[g,H]=r.useState(!0),R=Q().shape({appName:X.label("Name")}),[i,N]=r.useState("stdout"),f=J({defaultValues:{appName:"",defaultArgs:"",...!!t&&!c&&!d&&{appName:`${t.name}_${h(new Date,"HH_mm_ss")}`}},resolver:T(R),reValidateMode:"onChange",mode:"onChange"}),x=J({defaultValues:{args:((A=t.config)==null?void 0:A.defaultArgs)||""},mode:"onChange"}),k=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"}),D={stdout:s.data.log,stdoutPath:s.data.path,stderr:m.data.log,stderrPath:m.data.path};D&&($(D),H(!1))},[t.executionId,t.jobName,t.projectId,n]);r.useEffect(()=>{c&&t&&k()},[k,c,t]);const V=lt(),O=async()=>{V(n,i==="stdout"?o.stdoutPath:o.stderrPath,it.DATASET)},P=()=>{const s=i==="stdout"&&o?o.stdout:o.stderr;if(s!=null&&s.startsWith("Log is too big to display in browser."))return O},{handleSubmit:L,formState:{errors:U}}=f,{handleSubmit:v}=x,W=r.useCallback(()=>L(async s=>{const{name:m}=await j({pId:n,data:{...t.config,appName:s.appName},name:s.appName}).unwrap();e(),u(B.jobs.overview,{jobName:m})})(),[L,j,e,t.config,u,n]),z=r.useCallback(()=>v(async s=>{n&&t&&e&&C({pId:`${n}`,name:t.name,data:s.args}).unwrap().then(()=>{y.info({title:"Info",content:"Job started"}),e(),u(B.jobs.executions,{jobName:t.name})})})(),[e,t,u,y,n,v,C]),G=r.useMemo(()=>[{title:"stdout",isActive:i==="stdout",onClick:()=>N("stdout")},{title:"stderr",isActive:i==="stderr",onClick:()=>N("stderr")}],[i]);let b=" ";return o&&(b=i==="stdout"&&o?o.stdout:o.stderr,b||(b="Logs unavailable")),a.jsxs(a.Fragment,{children:[!w&&!c&&!d&&a.jsx(p,{title:"Run job",isOpen:l,onClose:e,secondaryButtonProps:{children:"Cancel",onClick:e},mainButtonProps:{children:"Run",onClick:z,isLoading:_},children:a.jsx(E,{...x,children:a.jsx(S,{defaultValue:t.config.defaultArgs,label:"Arguments",placeholder:"enter arguments",labelProps:{width:"100%"},...x.register("args")})})}),w&&!c&&!d&&a.jsx(p,{title:"Make a copy",isOpen:l,onClose:e,mainButtonProps:{children:"Create copy",onClick:W,isLoading:F},secondaryButtonProps:{children:"Cancel",onClick:e},children:a.jsxs(E,{...f,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(S,{defaultValue:`${t.name}${h(new Date,"dd-MM-yyyy")}`,type:"text",width:"100%",height:"100%",label:"Name of the new job",labelProps:{width:"100%"},...f.register("appName"),...et("appName",U)})]})}),c&&!d&&a.jsxs(st,{size:"7xl",isOpen:l,onClose:e,hasCloseButton:!0,title:a.jsx(ot,{title:"Logs",tabs:G,width:"90%"}),bodyProps:{pt:0,mt:"10px"},children:[g&&a.jsx(nt,{}),o&&!g&&a.jsx(rt,{m:"20px",mt:0,children:a.jsx(ct,{title:h(new Date,ut),wrapLongLines:!0,copyButton:!0,downloadButton:!0,downloadCallback:P(),content:b,language:"haskell"})})]}),d&&a.jsx(p,{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{ht as J};
