import{f as R,d as i,k as b,l as C,r,J as f,u as E,g as N,M as q,D,c as L,P as $,Q as w,R as y,S as B,q as j,j as e,H as Q}from"./index-CzUo07dn.js";import{c as T,a as z}from"./index.esm-BBoKC3tX.js";import{P as A,c as H,i as J,d as K,n as O,p as M,a as V,r as I,b as U}from"./ProjectForm-DnLS-jke.js";import{B as Z}from"./BaseLayout-D6pt0Et9.js";import{F as G}from"./FormFooterLayout-CkQ9739Z.js";import{R as W}from"./ResourceLink-5fvv3Myt.js";import"./FormMenuItem-D-3tk9Lf.js";import"./instanceOptions-BVjIWe7l.js";import"./ConfigFieldDescription-DYgLBIgn.js";import"./AutoExpandingTextArea-DbenJele.js";import"./ScrollableTable-COas1_r_.js";import"./formFields-BDRvqvBI.js";import"./snapshots-DWXTDgvk.js";import"./StoragePoolSelector-B0M_DZ36.js";import"./CustomSelect-D8P9H5Av.js";import"./StoragePoolSize-DCNPXe9o.js";import"./DiskSizeSelector-C28v9MJO.js";import"./NotificationRow-IVGrYQV6.js";import"./ScrollableContainer-CSFAM_C4.js";import"./ResourceIcon-CQum9kVZ.js";const gt=()=>{var u;const s=R(),c=i.useNotify(),n=b(),g=C(),h=r.useState(null),[p,P]=r.useState(f(A)),{hasProjectsNetworksZones:S,hasStorageBuckets:_}=E(),{data:l}=N({queryKey:[j.profiles,"default","default"],queryFn:()=>y("default","default")}),x=T().shape({name:z().test("deduplicate","A project with this name already exists",t=>q(t,"",h,"projects")).required()}),m=()=>{Q("form-contents","p-bottom-controls")};r.useEffect(m,[(u=c.notification)==null?void 0:u.message,p]),D("resize",m);const d=t=>{s(`/ui/project/${t.name}/instances`),n.success(e.jsxs(e.Fragment,{children:["Project"," ",e.jsx(W,{type:"project",value:t.name,to:`/ui/project/${t.name}/instances`})," ","created."]}))},o=L({initialValues:{name:"",restricted:!1,readOnly:!1,entityType:"project",default_instance_storage_pool:l?$(l):""},enableReinitialize:!0,validationSchema:x,onSubmit:t=>{const k=t.restricted?{...H(t),...J(t),...K(t),...O(t)}:{};S||(t.features_networks_zones=void 0),_||(t.features_storage_buckets=void 0),w(JSON.stringify({...M(t),config:{...V(t),...I(t),...k}})).then(async()=>{if(!t.default_instance_storage_pool||t.features_profiles===!1){d(t);return}const a=await y("default",t.name);a.devices={root:{path:"/",pool:t.default_instance_storage_pool,type:"disk"}},B(a,t.name).then(()=>{d(t)}).catch(F=>{s(`/ui/project/${t.name}/instances`),n.failure(`Successfully created ${a.name}, Failed to attach storage pool`,F)})}).catch(a=>{o.setSubmitting(!1),c.failure("Project creation failed",a)}).finally(()=>{g.invalidateQueries({queryKey:[j.projects]})})}});return e.jsxs(Z,{title:"Create a project",contentClassName:"create-project",children:[e.jsx(U,{formik:o,section:p,updateSection:t=>P(f(t)),isEdit:!1}),e.jsxs(G,{children:[e.jsx(i.Button,{appearance:"base",onClick:()=>s(-1),children:"Cancel"}),e.jsx(i.ActionButton,{appearance:"positive",loading:o.isSubmitting,disabled:!o.isValid||!o.values.name,onClick:()=>void o.submitForm(),children:"Create"})]})]})};export{gt as default};
