import{f as j,d as r,k as v,o as y,l as g,r as h,g as S,M as B,c as E,j as e,q as i,D as q,t as F,Z as k,_ as R,H as w}from"./index-CzUo07dn.js";import{c as G,a as A}from"./index.esm-BBoKC3tX.js";import{b as $,g as H}from"./clusterGroups-DoorAX9j.js";import{S as L}from"./SelectableMainTable-1U_EJECQ.js";import{N as M}from"./NotificationRow-IVGrYQV6.js";import{B as z}from"./BaseLayout-D6pt0Et9.js";import{A as D}from"./AutoExpandingTextArea-DbenJele.js";import{R as K}from"./ResourceLink-5fvv3Myt.js";const U=({group:t})=>{var d;const l=j(),n=r.useNotify(),p=v(),{group:b}=y(),f=g(),x=h.useState(null),{data:c=[],error:m}=S({queryKey:[i.cluster,i.members],queryFn:F});m&&n.failure("Loading cluster members failed",m);const C=G().shape({name:A().test("deduplicate","A cluster group with this name already exists",a=>(t==null?void 0:t.name)===a||B(a,"",x,"cluster/groups")).required()}),s=E({initialValues:{description:(t==null?void 0:t.description)??"",members:(t==null?void 0:t.members)??[],name:(t==null?void 0:t.name)??""},validationSchema:C,onSubmit:a=>{(t?k:R)({name:a.name,description:a.description,members:a.members}).then(()=>{const o=t?"saved":"created";l(`/ui/cluster/group/${a.name}`),p.success(e.jsxs(e.Fragment,{children:["Cluster group"," ",e.jsx(K,{type:"cluster-group",value:a.name,to:`/ui/cluster/group/${a.name}`})," ",o,"."]}))}).catch(o=>{s.setSubmitting(!1);const N=t?"save":"creation";n.failure(`Cluster group ${N} failed`,o)}).finally(()=>{f.invalidateQueries({queryKey:[i.cluster,i.groups]})})}}),u=()=>{w("form-contents","p-bottom-controls")};return h.useEffect(u,[(d=n.notification)==null?void 0:d.message]),q("resize",u),e.jsxs(z,{title:t?"Edit cluster group":"Create cluster group",contentClassName:"cluster-group-form",children:[e.jsx(r.Form,{onSubmit:s.handleSubmit,className:"form",children:e.jsx(r.Row,{className:"form-contents",children:e.jsxs(r.Col,{size:12,children:[e.jsx(M,{}),e.jsxs("div",{className:"cluster-group-metadata",children:[e.jsx(r.Input,{id:"name",type:"text",label:"Group name",placeholder:"Enter name",required:!0,disabled:!!t,onBlur:s.handleBlur,onChange:s.handleChange,value:s.values.name,error:s.touched.name?s.errors.name:null}),e.jsx(D,{id:"description",name:"description",label:"Description",placeholder:"Enter description",onBlur:s.handleBlur,onChange:s.handleChange,value:s.values.description,error:s.touched.description?s.errors.description:null})]}),e.jsxs("div",{className:"choose-label",children:["Choose members from the list"," ",e.jsxs("span",{className:"u-text--muted",children:["(",s.values.members.length," selected)"]})," "]}),e.jsx(L,{headers:$(),rows:H(c,b),sortable:!0,className:"cluster-group-select-members",filteredNames:c.map(a=>a.server_name),itemName:"member",parentName:"cluster",selectedNames:s.values.members,setSelectedNames:a=>void s.setFieldValue("members",a),processingNames:[]})]})})}),e.jsxs("div",{className:"p-bottom-controls",id:"form-footer",children:[e.jsx("hr",{}),e.jsx(r.Row,{className:"u-align--right",children:e.jsxs(r.Col,{size:12,children:[e.jsx(r.Button,{appearance:"base",onClick:()=>l("/ui/cluster"),children:"Cancel"}),e.jsx(r.ActionButton,{appearance:"positive",loading:s.isSubmitting,disabled:!s.isValid||!s.values.name,onClick:()=>void s.submitForm(),children:t?"Save changes":"Create"})]})})]})]})};export{U as C};
