import{aR as H,r as f,y as J,p as R,B as _,z as X,Z as Y,j as e,a as i,bH as B,ci as q,c as Z,bW as W,gN as K,i as I,d as k,bA as G,u as Q,bI as L,bz as ee,bJ as te,aC as se,aD as ne,ar as ae,dh as oe,aH as ie,aY as re,aZ as A,bd as le,cD as ce,a3 as pe,a5 as de,aI as me,f as ue,b3 as he,b4 as xe,aS as fe,dg as ge,cb as U,cv as $,gO as be,gP as je,fJ as ye}from"./index-_eM6l4Nf.js";import{V as we}from"./ViewTransformation-D-2ijvBj.js";import{P as ve}from"./index-B-5JhFyZ.js";const Ae=({isEdit:j,onChange:C,onRemove:F,features:m=[],featureOptions:u,joins:g=[],transformations:h,transformationsList:p,transformationsListLoading:b,featureSelectProps:d,rowSx:S={}})=>{const{formState:{errors:r}}=H(),[D,E]=f.useState(),[y,w]=J(),V=f.useMemo(()=>h!=null&&h.length?h:[],[h]),P=f.useMemo(()=>u??m.map(s=>{const n=R(X(Y(["secondFg","id"],s.fg.id)),_("","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]),O=f.useCallback(s=>{if(!s)return;const[n,o]=s.split(" v");return p.find(t=>t.hopsworksUdf.name===n&&t.version===Number(o))},[p]),v=f.useMemo(()=>p.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)),[p]),M=s=>{E(s.name),w()},x=O(D);return e.jsxs(e.Fragment,{children:[e.jsx(i,{flexDirection:"column",children:V.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:S,children:[e.jsx(B,{isMulti:!0,preventAdding:!u,disabled:j,value:s,placeholder:"pick feature / features",noDataMessage:"no features available",onChange:l=>{C(o,"features",l)},options:P,...q("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(W,{format:"fill",noDataMessage:"no transformations available",value:n==null?void 0:n.name,onChange:l=>{var N;const c=((N=p.find(T=>`${T.hopsworksUdf.name} v${T.version}`===l.key))==null?void 0:N.inputs.length)||0;return C(o,"transformation",{name:l.key,argNum:c})},placeholder:"pick a transformation",options:v,label:o===0?"Transformation function":"",disabled:b||j,...K("transformation",(a=r==null?void 0:r.transformations)==null?void 0:a[o])}),e.jsx(I,{ml:"8px",mt:o===0?"22px":"0px",icon:k.eye,onClick:()=>M(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(I,{intent:"ghost",icon:k.cross,onClick:()=>F(o),tooltip:"Delete transformation",disabled:b})})]},`${o}-${s.join("-")}-${n}`)})}),e.jsx(G,{size:"7xl",title:`${x==null?void 0:x.hopsworksUdf.name} transformation code preview`,isOpen:!!x&&y,onClose:w,mainButtonProps:{children:"Done",onClick:w},children:x&&e.jsx(we,{...x})})]})},Te=[".py"],ke=ae().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:C,transformationsList:F})=>{const{id:m,fsId:u}=Q(),[g,h]=f.useState(!1),p=L(),b=ee(),d=te(t=>t.loading.effects.transformations.create);f.useEffect(()=>{p.transformations.fetch({projectId:+m,featureStoreId:+u})},[p.transformations,m,u]);const{register:S,control:r,reset:D,handleSubmit:E,formState:{errors:y},setValue:w}=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:V,append:P,remove:O}=oe({control:r,name:"outputTypes"}),v=()=>{D(),C()},M=f.useCallback(t=>F.reduce((a,l)=>l.hopsworksUdf.name===t?Math.max(a,l.version):a,0),[F]),x=t=>p.transformations.create({projectId:+m,featureStoreId:+u,data:{version:M(t.name)+1,hopsworksUdf:{name:t.name,sourceCode:t.snippet,outputTypes:t.outputTypes,transformationFunctionArgumentNames:t.transformationFunctionArgumentNames,statisticsArgumentNames:t.statisticsArgumentNames}}}).then(()=>(p.transformations.fetch({projectId:+m,featureStoreId:+u}),b.success({title:"Transformation function created",content:"The new transformation is now ready to be used."}),!0)),s=()=>{E(async t=>{await x(t)&&v()})()},n=()=>{h(!1)},o=async t=>{const{fileName:a,path:l}=be(t.attributes.path);je.getContent(+m,`${l}/${a}`,ye.DATASET).then(c=>{w("snippet",c==null?void 0:c.data),n()}).catch(c=>{var T,z;const N=((z=(T=c==null?void 0:c.response)==null?void 0:T.data)==null?void 0:z.errorMsg)||"File couldn't be processed";b.error({title:"Error",content:N})})};return e.jsxs(e.Fragment,{children:[!g&&e.jsx(G,{size:"7xl",title:"Add new transformation",isOpen:j,onClose:v,mainButtonProps:{children:"Save",onClick:s},secondaryButtonProps:{children:"Back",onClick:v},children:e.jsxs(i,{flexDirection:"column",height:"100%",flexGrow:1,children:[e.jsxs(i,{flexDirection:"row",gap:"20px",children:[e.jsx(ie,{label:"Name",...S("name"),width:"180px",disabled:d,labelProps:{width:"180px"},placeholder:"Transformation name",...re("name",y)}),e.jsx(i,{width:"280px",children:e.jsx(A,{control:r,name:"transformationFunctionArgumentNames",render:({field:{onChange:t,value:a}})=>e.jsx(B,{isMulti:!0,label:"Argument names",placeholder:"type and add argument names",width:"280px",disabled:d,value:a,onChange:t,options:[],...q("transformationFunctionArgumentNames",y)})})}),e.jsx(i,{width:"280px",children:e.jsx(A,{control:r,name:"statisticsArgumentNames",render:({field:{onChange:t,value:a}})=>e.jsx(B,{isMulti:!0,label:"Statistics argument names",placeholder:"type and add argument names",width:"280px",disabled:d,value:a,onChange:t,options:[],...q("statisticsArgumentNames",y)})})}),e.jsxs(i,{gap:"10px",alignItems:"end",children:[V.map((t,a)=>e.jsx(A,{control:r,name:`outputTypes.${a}`,render:({field:{onChange:l,value:c}})=>e.jsxs(i,{alignItems:"end",children:[e.jsx(le,{label:a===0?"Output types":"",placeholder:"pick a type",width:"100px",isDisabled:d,value:c,onChange:l,options:["string","bigint","double","boolean","timestamp","date"]}),a!==0&&e.jsx(I,{type:"button",disabled:d,tooltipProps:{ml:"15px"},intent:"ghost",tooltip:"Remove",onClick:()=>O(a),icon:k.cross})]})},t.id)),e.jsx(I,{type:"button",tooltipProps:{ml:"15px"},tooltip:"Add",onClick:()=>{P("bigint")},icon:k.plus,sx:{ml:"15px"}})]})]}),e.jsx(ce,{}),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:de.warning}),e.jsx(A,{control:r,name:"snippet",render:({field:{onChange:t,value:a}})=>e.jsx(i,{flexGrow:1,children:e.jsx(me,{readOnly:!1,label:"Transformation snippet (Python)",mode:"python",value:a,onChange:t})})}),e.jsxs(i,{flexDirection:"column",children:[e.jsx(ue,{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(ve,{intent:"secondary",icon:k.folder,mr:"8px",onClick:()=>h(!0),children:"From project"})})]})]})}),g&&e.jsx(he,{size:"7xl",isOpen:g,onClose:n,children:e.jsx(xe,{disableDownload:!0,validExtensions:Te,handleCloseExplorer:n,handleSelectFile:o,mode:fe.oneFile})})]})};export{Ae as F,$e as N};
