import{aG as H,r as f,y as _,p as R,B as X,z as Y,Y as J,j as e,$ as i,bG as G,ch as B,c as Q,bV as W,gF as K,i as I,C,bz as z,u as Z,bH as L,by as ee,bI as te,aC as se,aD as ne,ar as ae,dg as oe,b2 as ie,aN as re,aO as $,bd as le,cC as ce,a3 as pe,a5 as me,b3 as de,f as ue,aV as he,aW as xe,aH as fe,df as be,ca as U,cu as A,gG as ge,gH as je,fE as ye}from"./index-46ma1THH.js";import{V as we}from"./ViewTransformation-BHQuUfcb.js";import{P as ve}from"./index-CcWUz40c.js";const $e=({isEdit:j,onChange:k,onRemove:F,features:d=[],featureOptions:u,joins:b=[],transformations:h,transformationsList:p,transformationsListLoading:g,featureSelectProps:m,rowSx:S={}})=>{const{formState:{errors:r}}=H(),[D,V]=f.useState(),[y,w]=_(),E=f.useMemo(()=>h!=null&&h.length?h:[],[h]),O=f.useMemo(()=>u??d.map(s=>{const n=R(Y(J(["secondFg","id"],s.fg.id)),X("","secondFgPrefix"))(b);return{label:`${s.fg.name} v${s.fg.version}`,options:s.features.map(({name:o})=>({value:`${n}${o}`,label:`${n}${o}`}))}}),[u,d,b]),P=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=>{V(s.name),w()},x=P(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:S,children:[e.jsx(G,{isMulti:!0,preventAdding:!u,disabled:j,value:s,placeholder:"pick feature / features",noDataMessage:"no features available",onChange:l=>{k(o,"features",l)},options:O,...B("feature",(t=r==null?void 0:r.transformations)==null?void 0:t[o]),...m,label:o===0?(m==null?void 0:m.label)??"Feature":""}),e.jsx(Q,{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 k(o,"transformation",{name:l.key,argNum:c})},placeholder:"pick a transformation",options:v,label:o===0?"Transformation function":"",disabled:g||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:C.eye,onClick:()=>M(n),tooltip:"View transformation",disabled:g||!(n!=null&&n.name)})]})]}),e.jsx(i,{sx:o===0?{mt:"22px"}:{mt:"0px"},mx:"8px",children:!j&&e.jsx(I,{intent:"ghost",icon:C.cross,onClick:()=>F(o),tooltip:"Delete transformation",disabled:g})})]},`${o}-${s.join("-")}-${n}`)})}),e.jsx(z,{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"],Ce=ae().shape({name:A.required().label("Name"),outputTypes:U().of(A.nonNullable()).min(1).required().label("Output types"),transformationFunctionArgumentNames:U().of(A).min(1).required().label("Argument names"),statisticsArgumentNames:U().of(A).label("Statistics argument names"),snippet:be.required().label("snippet")}),Ae=({isOpen:j,handleToggle:k,transformationsList:F})=>{const{id:d,fsId:u}=Z(),[b,h]=f.useState(!1),p=L(),g=ee(),m=te(t=>t.loading.effects.transformations.create);f.useEffect(()=>{p.transformations.fetch({projectId:+d,featureStoreId:+u})},[p.transformations,d,u]);const{register:S,control:r,reset:D,handleSubmit:V,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(Ce),shouldUnregister:!1}),{fields:E,append:O,remove:P}=oe({control:r,name:"outputTypes"}),v=()=>{D(),k()},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:+d,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:+d,featureStoreId:+u}),g.success({title:"Transformation function created",content:"The new transformation is now ready to be used."}),!0)),s=()=>{V(async t=>{await x(t)&&v()})()},n=()=>{h(!1)},o=async t=>{const{fileName:a,path:l}=ge(t.attributes.path);je.getContent(+d,`${l}/${a}`,ye.DATASET).then(c=>{w("snippet",c==null?void 0:c.data),n()}).catch(c=>{var T,q;const N=((q=(T=c==null?void 0:c.response)==null?void 0:T.data)==null?void 0:q.errorMsg)||"File couldn't be processed";g.error({title:"Error",content:N})})};return e.jsxs(e.Fragment,{children:[!b&&e.jsx(z,{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:m,labelProps:{width:"180px"},placeholder:"Transformation name",...re("name",y)}),e.jsx(i,{width:"280px",children:e.jsx($,{control:r,name:"transformationFunctionArgumentNames",render:({field:{onChange:t,value:a}})=>e.jsx(G,{isMulti:!0,label:"Argument names",placeholder:"type and add argument names",width:"280px",disabled:m,value:a,onChange:t,options:[],...B("transformationFunctionArgumentNames",y)})})}),e.jsx(i,{width:"280px",children:e.jsx($,{control:r,name:"statisticsArgumentNames",render:({field:{onChange:t,value:a}})=>e.jsx(G,{isMulti:!0,label:"Statistics argument names",placeholder:"type and add argument names",width:"280px",disabled:m,value:a,onChange:t,options:[],...B("statisticsArgumentNames",y)})})}),e.jsxs(i,{gap:"10px",alignItems:"end",children:[E.map((t,a)=>e.jsx($,{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:m,value:c,onChange:l,options:["string","bigint","double","boolean","timestamp","date"]}),a!==0&&e.jsx(I,{type:"button",disabled:m,tooltipProps:{ml:"15px"},intent:"ghost",tooltip:"Remove",onClick:()=>P(a),icon:C.cross})]})},t.id)),e.jsx(I,{type:"button",tooltipProps:{ml:"15px"},tooltip:"Add",onClick:()=>{O("bigint")},icon:C.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:me.warning}),e.jsx($,{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(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:C.folder,mr:"8px",onClick:()=>h(!0),children:"From project"})})]})]})}),b&&e.jsx(he,{size:"7xl",isOpen:b,onClose:n,children:e.jsx(xe,{disableDownload:!0,validExtensions:Te,handleCloseExplorer:n,handleSelectFile:o,mode:fe.oneFile})})]})};export{$e as F,Ae as N};
