import{ay as R,r as f,w as H,f as X,B as _,z as J,W as Q,j as e,d as i,bM as B,bN as q,y as W,bs as Y,bP as K,e as I,T as k,be as G,ae as Z,c1 as A,bH as U,cw as L,u as ee,bf as te,bd as se,bh as ne,au as ae,av as oe,ed as ie,aR as re,aE as le,aF as $,aQ as pe,ca as ce,aa as me,ab as de,aX as ue,p as he,aI as xe,az as fe,ep as be,en as ge}from"./index-CK33qGU_.js";import{V as je}from"./ViewTransformation-B_KH3iXm.js";import{P as we}from"./PrimaryIconButton-DQUEhx3G.js";import{a as ye,i as ve}from"./FileUploaderButton-Bppadu35.js";const $e=({isEdit:j,onChange:F,onRemove:C,features:d=[],featureOptions:u,joins:b=[],transformations:h,transformationsList:c,transformationsListLoading:g,featureSelectProps:m,rowSx:S={}})=>{const{formState:{errors:r}}=R(),[D,V]=f.useState(),[w,y]=H(),E=f.useMemo(()=>h!=null&&h.length?h:[],[h]),P=f.useMemo(()=>u??d.map(s=>{const n=X(J(Q(["secondFg","id"],s.fg.id)),_("","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]),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]),v=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),y()},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:S,children:[e.jsx(B,{isMulti:!0,preventAdding:!u,disabled:j,value:s,placeholder:"pick feature / features",noDataMessage:"no features available",onChange:l=>{F(o,"features",l)},options:P,...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(W,{mx:"17px",width:"26px",mt:o===0?"30px":"8px",children:"<-"}),e.jsxs(i,{width:"100%",alignItems:"start",children:[e.jsx(Y,{format:"fill",noDataMessage:"no transformations available",value:n==null?void 0:n.name,onChange:l=>{var N;const p=((N=c.find(T=>`${T.hopsworksUdf.name} v${T.version}`===l.key))==null?void 0:N.inputs.length)||0;return F(o,"transformation",{name:l.key,argNum:p})},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: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(I,{intent:"ghost",icon:k.cross,onClick:()=>C(o),tooltip:"Delete transformation",disabled:g})})]},`${o}-${s.join("-")}-${n}`)})}),e.jsx(G,{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})})]})},Te=[".py"],ke=Z().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:L.required().label("snippet")}),Ie=({isOpen:j,handleToggle:F,transformationsList:C})=>{const{id:d,fsId:u}=ee(),[b,h]=f.useState(!1),c=te(),g=se(),m=ne(t=>t.loading.effects.transformations.create);f.useEffect(()=>{c.transformations.fetch({projectId:+d,featureStoreId:+u})},[c.transformations,d,u]);const{register:S,control:r,reset:D,handleSubmit:V,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:E,append:P,remove:M}=ie({control:r,name:"outputTypes"}),v=()=>{D(),F()},O=f.useCallback(t=>C.reduce((a,l)=>l.hopsworksUdf.name===t?Math.max(a,l.version):a,0),[C]),x=t=>c.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(()=>(c.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}=ve(t.attributes.path);be.getContent(+d,`${l}/${a}`,ge.DATASET).then(p=>{y("snippet",p==null?void 0:p.data),n()}).catch(p=>{var T,z;const N=((z=(T=p==null?void 0:p.response)==null?void 0:T.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(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(re,{label:"Name",...S("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:[E.map((t,a)=>e.jsx($,{control:r,name:`outputTypes.${a}`,render:({field:{onChange:l,value:p}})=>e.jsxs(i,{alignItems:"end",children:[e.jsx(pe,{label:a===0?"Output types":"",placeholder:"pick a type",width:"100px",isDisabled:m,value:p,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:()=>M(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(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:Te,handleCloseExplorer:n,handleSelectFile:o,mode:fe.oneFile})})]})};export{$e as F,Ie as N};
