import{b3 as Q,r as f,c as G,p as H,A as J,y as R,a1 as _,j as e,B as i,aZ as B,cb as q,b as K,bT as X,cd as Y,f as S,Z as k,bE as z,aj as W,cs as C,c6 as U,cV as L,u as ee,aD as te,aE as se,aL as ne,az as ae,aA as oe,eJ as ie,aM as re,b8 as le,b9 as $,bm as ce,cA as pe,af as me,ag as de,aJ as ue,Q as he,b2 as xe,b4 as fe,eS as be,aH as ge}from"./index-dP1W1_bd.js";import{V as je}from"./ViewTransformation-xgCYfmc6.js";import{P as we}from"./index-MHdXqM53.js";import{e as ye,l as Te}from"./FileUploaderButton-frSujVRY.js";const $e=({isEdit:j,onChange:A,onRemove:F,features:d=[],featureOptions:u,joins:b=[],transformations:h,transformationsList:p,transformationsListLoading:g,featureSelectProps:m,rowSx:I={}})=>{const{formState:{errors:r}}=Q(),[D,E]=f.useState(),[w,y]=G(),V=f.useMemo(()=>h!=null&&h.length?h:[],[h]),M=f.useMemo(()=>u??d.map(s=>{const n=H(R(_(["secondFg","id"],s.fg.id)),J("","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]),T=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]),O=s=>{E(s.name),y()},x=P(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:I,children:[e.jsx(B,{isMulti:!0,preventAdding:!u,disabled:j,value:s,placeholder:"pick feature / features",noDataMessage:"no features available",onChange:l=>{A(o,"features",l)},options:M,...q("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(K,{mx:"17px",width:"26px",mt:o===0?"30px":"8px",children:"<-"}),e.jsxs(i,{width:"100%",alignItems:"start",children:[e.jsx(X,{format:"fill",noDataMessage:"no transformations available",value:n==null?void 0:n.name,onChange:l=>{var N;const c=((N=p.find(v=>`${v.hopsworksUdf.name} v${v.version}`===l.key))==null?void 0:N.inputs.length)||0;return A(o,"transformation",{name:l.key,argNum:c})},placeholder:"pick a transformation",options:T,label:o===0?"Transformation function":"",disabled:g||j,...Y("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:g||!(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:()=>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&&w,onClose:y,mainButtonProps:{children:"Done",onClick:y},children:x&&e.jsx(je,{...x})})]})},ve=[".py"],ke=W().shape({name:C.required().label("Name"),outputTypes:U().of(C.nonNullable()).min(1).required().label("Output types"),transformationFunctionArgumentNames:U().of(C).min(1).required().label("Argument names"),statisticsArgumentNames:U().of(C).label("Statistics argument names"),snippet:L.required().label("snippet")}),Se=({isOpen:j,handleToggle:A,transformationsList:F})=>{const{id:d,fsId:u}=ee(),[b,h]=f.useState(!1),p=te(),g=se(),m=ne(t=>t.loading.effects.transformations.create);f.useEffect(()=>{p.transformations.fetch({projectId:+d,featureStoreId:+u})},[p.transformations,d,u]);const{register:I,control:r,reset:D,handleSubmit:E,formState:{errors:w},setValue:y}=ae({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:oe(ke),shouldUnregister:!1}),{fields:V,append:M,remove:P}=ie({control:r,name:"outputTypes"}),T=()=>{D(),A()},O=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:O(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=()=>{E(async t=>{await x(t)&&T()})()},n=()=>{h(!1)},o=async t=>{const{fileName:a,path:l}=Te(t.attributes.path);be.getContent(+d,`${l}/${a}`,ge.DATASET).then(c=>{y("snippet",c==null?void 0:c.data),n()}).catch(c=>{var v,Z;const N=((Z=(v=c==null?void 0:c.response)==null?void 0:v.data)==null?void 0:Z.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:T,mainButtonProps:{children:"Save",onClick:s},secondaryButtonProps:{children:"Back",onClick:T},children:e.jsxs(i,{flexDirection:"column",height:"100%",flexGrow:1,children:[e.jsxs(i,{flexDirection:"row",gap:"20px",children:[e.jsx(re,{label:"Name",...I("name"),width:"180px",disabled:m,labelProps:{width:"180px"},placeholder:"Transformation name",...le("name",w)}),e.jsx(i,{width:"280px",children:e.jsx($,{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:m,value:a,onChange:t,options:[],...q("transformationFunctionArgumentNames",w)})})}),e.jsx(i,{width:"280px",children:e.jsx($,{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:m,value:a,onChange:t,options:[],...q("statisticsArgumentNames",w)})})}),e.jsxs(i,{gap:"10px",alignItems:"end",children:[V.map((t,a)=>e.jsx($,{control:r,name:`outputTypes.${a}`,render:({field:{onChange:l,value:c}})=>e.jsxs(i,{alignItems:"end",children:[e.jsx(ce,{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(S,{type:"button",disabled:m,tooltipProps:{ml:"15px"},intent:"ghost",tooltip:"Remove",onClick:()=>P(a),icon:k.cross})]})},t.id)),e.jsx(S,{type:"button",tooltipProps:{ml:"15px"},tooltip:"Add",onClick:()=>{M("bigint")},icon:k.plus,sx:{ml:"15px"}})]})]}),e.jsx(pe,{}),e.jsx(me,{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($,{control:r,name:"snippet",render:({field:{onChange:t,value:a}})=>e.jsx(i,{flexGrow:1,children:e.jsx(ue,{readOnly:!1,label:"Transformation snippet (Python)",mode:"python",value:a,onChange:t})})}),e.jsxs(i,{flexDirection:"column",children:[e.jsx(he,{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(we,{intent:"secondary",icon:k.folder,mr:"8px",onClick:()=>h(!0),children:"From project"})})]})]})}),b&&e.jsx(xe,{size:"7xl",isOpen:b,onClose:n,children:e.jsx(ye,{disableDownload:!0,validExtensions:ve,handleCloseExplorer:n,handleSelectFile:o,mode:fe.oneFile})})]})};export{$e as F,Se as N};
