import{aR as G,r as f,w as H,p as J,E as W,C as X,W as Y,j as e,q as i,bP as q,cv as B,c as Z,c8 as _,hI as K,n as S,v as k,bJ as z,u as Q,bW as L,bI as ee,bX as te,aA as se,aB as ne,dv as ae,aH as oe,aY as ie,aZ as N,be as re,bS as le,a4 as pe,a5 as ce,aI as de,d as me,b4 as ue,b5 as he,aS as xe,Z as fe,du as ge,co as U,$,hJ as be,gU as je,gD as we}from"./index-DNBEEyYo.js";import{V as ve}from"./ViewTransformation-8wmgMabA.js";import{P as ye}from"./index-DivQJV92.js";const Ne=({isEdit:j,onChange:F,onRemove:A,features:m=[],featureOptions:u,joins:g=[],transformations:h,transformationsList:c,transformationsListLoading:b,featureSelectProps:d,rowSx:I={}})=>{const{formState:{errors:r}}=G(),[D,V]=f.useState(),[w,v]=H(),E=f.useMemo(()=>h!=null&&h.length?h:[],[h]),P=f.useMemo(()=>u??m.map(s=>{const n=J(X(Y(["secondFg","id"],s.fg.id)),W("","secondFgPrefix"))(g);return{label:`${s.fg.name} v${s.fg.version}`,options:s.features.map(({name:o})=>({value:`${n}${o}`,label:`${n}${o}`}))}}),[u,m,g]),M=f.useCallback(s=>{if(!s)return;const[n,o]=s.split(" v");return c.find(t=>t.hopsworksUdf.name===n&&t.version===Number(o))},[c]),y=f.useMemo(()=>c.map(s=>({label:s.hopsworksUdf.name,additionalText:`version ${s.version}`,key:`${s.hopsworksUdf.name} v${s.version}`})).sort((s,n)=>s.key.localeCompare(n.key)),[c]),O=s=>{V(s.name),v()},x=M(D);return e.jsxs(e.Fragment,{children:[e.jsx(i,{flexDirection:"column",children:E.map(({features:s=[],transformation:n},o)=>{var t,a;return e.jsxs(i,{mt:"8px",alignItems:"flex-start",width:"100%",children:[e.jsxs(i,{width:"100%",sx:I,children:[e.jsx(q,{isMulti:!0,preventAdding:!u,disabled:j,value:s,placeholder:"pick feature / features",noDataMessage:"no features available",onChange:l=>{F(o,"features",l)},options:P,...B("feature",(t=r==null?void 0:r.transformations)==null?void 0:t[o]),...d,label:o===0?(d==null?void 0:d.label)??"Feature":""}),e.jsx(Z,{mx:"17px",width:"26px",mt:o===0?"30px":"8px",children:"<-"}),e.jsxs(i,{width:"100%",alignItems:"start",children:[e.jsx(_,{format:"fill",noDataMessage:"no transformations available",value:n==null?void 0:n.name,onChange:l=>{var C;const p=((C=c.find(T=>`${T.hopsworksUdf.name} v${T.version}`===l.key))==null?void 0:C.inputs.length)||0;return F(o,"transformation",{name:l.key,argNum:p})},placeholder:"pick a transformation",options:y,label:o===0?"Transformation function":"",disabled:b||j,...K("transformation",(a=r==null?void 0:r.transformations)==null?void 0:a[o])}),e.jsx(S,{ml:"8px",mt:o===0?"22px":"0px",icon:k.eye,onClick:()=>O(n),tooltip:"View transformation",disabled:b||!(n!=null&&n.name)})]})]}),e.jsx(i,{sx:o===0?{mt:"22px"}:{mt:"0px"},mx:"8px",children:!j&&e.jsx(S,{intent:"ghost",icon:k.cross,onClick:()=>A(o),tooltip:"Delete transformation",disabled:b})})]},`${o}-${s.join("-")}-${n}`)})}),e.jsx(z,{size:"7xl",title:`${x==null?void 0:x.hopsworksUdf.name} transformation code preview`,isOpen:!!x&&w,onClose:v,mainButtonProps:{children:"Done",onClick:v},children:x&&e.jsx(ve,{...x})})]})},Te=[".py"],ke=fe().shape({name:$.required().label("Name"),outputTypes:U().of($.nonNullable()).min(1).required().label("Output types"),transformationFunctionArgumentNames:U().of($).min(1).required().label("Argument names"),statisticsArgumentNames:U().of($).label("Statistics argument names"),snippet:ge.required().label("snippet")}),$e=({isOpen:j,handleToggle:F,transformationsList:A})=>{const{id:m,fsId:u}=Q(),[g,h]=f.useState(!1),c=L(),b=ee(),d=te(t=>t.loading.effects.transformations.create);f.useEffect(()=>{c.transformations.fetch({projectId:+m,featureStoreId:+u})},[c.transformations,m,u]);const{register:I,control:r,reset:D,handleSubmit:V,formState:{errors:w},setValue:v}=se({defaultValues:{name:"",outputTypes:["bigint"],transformationFunctionArgumentNames:[],statisticsArgumentNames:[],snippet:`# Python - Transformation code.
# The name of the function should match the name provided above
# The function argument names should match the argument names proided above`},resolver:ne(ke),shouldUnregister:!1}),{fields:E,append:P,remove:M}=ae({control:r,name:"outputTypes"}),y=()=>{D(),F()},O=f.useCallback(t=>A.reduce((a,l)=>l.hopsworksUdf.name===t?Math.max(a,l.version):a,0),[A]),x=t=>c.transformations.create({projectId:+m,featureStoreId:+u,data:{version:O(t.name)+1,hopsworksUdf:{name:t.name,sourceCode:t.snippet,outputTypes:t.outputTypes,transformationFunctionArgumentNames:t.transformationFunctionArgumentNames,statisticsArgumentNames:t.statisticsArgumentNames}}}).then(()=>(c.transformations.fetch({projectId:+m,featureStoreId:+u}),b.success({title:"Transformation function created",content:"The new transformation is now ready to be used."}),!0)),s=()=>{V(async t=>{await x(t)&&y()})()},n=()=>{h(!1)},o=async t=>{const{fileName:a,path:l}=be(t.attributes.path);je.getContent(+m,`${l}/${a}`,we.DATASET).then(p=>{v("snippet",p==null?void 0:p.data),n()}).catch(p=>{var T,R;const C=((R=(T=p==null?void 0:p.response)==null?void 0:T.data)==null?void 0:R.errorMsg)||"File couldn't be processed";b.error({title:"Error",content:C})})};return e.jsxs(e.Fragment,{children:[!g&&e.jsx(z,{size:"7xl",title:"Add new transformation",isOpen:j,onClose:y,mainButtonProps:{children:"Save",onClick:s},secondaryButtonProps:{children:"Back",onClick:y},children:e.jsxs(i,{flexDirection:"column",height:"100%",flexGrow:1,children:[e.jsxs(i,{flexDirection:"row",gap:"20px",children:[e.jsx(oe,{label:"Name",...I("name"),width:"180px",disabled:d,labelProps:{width:"180px"},placeholder:"Transformation name",...ie("name",w)}),e.jsx(i,{width:"280px",children:e.jsx(N,{control:r,name:"transformationFunctionArgumentNames",render:({field:{onChange:t,value:a}})=>e.jsx(q,{isMulti:!0,label:"Argument names",placeholder:"type and add argument names",width:"280px",disabled:d,value:a,onChange:t,options:[],...B("transformationFunctionArgumentNames",w)})})}),e.jsx(i,{width:"280px",children:e.jsx(N,{control:r,name:"statisticsArgumentNames",render:({field:{onChange:t,value:a}})=>e.jsx(q,{isMulti:!0,label:"Statistics argument names",placeholder:"type and add argument names",width:"280px",disabled:d,value:a,onChange:t,options:[],...B("statisticsArgumentNames",w)})})}),e.jsxs(i,{gap:"10px",alignItems:"end",children:[E.map((t,a)=>e.jsx(N,{control:r,name:`outputTypes.${a}`,render:({field:{onChange:l,value:p}})=>e.jsxs(i,{alignItems:"end",children:[e.jsx(re,{label:a===0?"Output types":"",placeholder:"pick a type",width:"100px",isDisabled:d,value:p,onChange:l,options:["string","bigint","double","boolean","timestamp","date"]}),a!==0&&e.jsx(S,{type:"button",disabled:d,tooltipProps:{ml:"15px"},intent:"ghost",tooltip:"Remove",onClick:()=>M(a),icon:k.cross})]})},t.id)),e.jsx(S,{type:"button",tooltipProps:{ml:"15px"},tooltip:"Add",onClick:()=>{P("bigint")},icon:k.plus,sx:{ml:"15px"}})]})]}),e.jsx(le,{}),e.jsx(pe,{mb:"20px",content:"Transformations are immutable: once created and attached to a Training Dataset, you won't be able to edit the transformation",type:ce.warning}),e.jsx(N,{control:r,name:"snippet",render:({field:{onChange:t,value:a}})=>e.jsx(i,{flexGrow:1,children:e.jsx(de,{readOnly:!1,label:"Transformation snippet (Python)",mode:"python",value:a,onChange:t})})}),e.jsxs(i,{flexDirection:"column",children:[e.jsx(me,{mt:"20px",mb:"8px",children:"Your snippet is contained in a file? Select it to copy its content"}),e.jsx(i,{width:"400px",children:e.jsx(ye,{intent:"secondary",icon:k.folder,mr:"8px",onClick:()=>h(!0),children:"From project"})})]})]})}),g&&e.jsx(ue,{size:"7xl",isOpen:g,onClose:n,children:e.jsx(he,{disableDownload:!0,validExtensions:Te,handleCloseExplorer:n,handleSelectFile:o,mode:xe.oneFile})})]})};export{Ne as F,$e as N};
