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-Cziemgsu.js";import{c as T,a as z}from"./index.esm-dDZ7-lOE.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-D9VkunE8.js";import{B as Z}from"./BaseLayout-7LTMOawH.js";import{F as G}from"./FormFooterLayout-BJQGpdhQ.js";import{R as W}from"./ResourceLink-D2AYI_En.js";import"./FormMenuItem-phSiN9Ls.js";import"./instanceOptions-BVjIWe7l.js";import"./ConfigFieldDescription-1GO19eHS.js";import"./AutoExpandingTextArea-Bc4N4lSt.js";import"./ScrollableTable-D1yV1_jU.js";import"./formFields-BDRvqvBI.js";import"./snapshots-AEdTpwkf.js";import"./StoragePoolSelector-BZZPCPdz.js";import"./CustomSelect-1ZX4nHvi.js";import"./StoragePoolSize-DPO2JD2N.js";import"./DiskSizeSelector--EFhW8D8.js";import"./NotificationRow-BJRTvUVl.js";import"./ScrollableContainer-C6sud4ax.js";import"./ResourceIcon-DRRIqKSs.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};
