import{g as S,q as d,bX as b,j as r,A as V,v as m,bY as P,ad as T,ae as f,af as y,M as E,y as F,d as h,k as N,l as w,r as k,c as A,ai as D,ag as O,w as q}from"./index-DRe7Uehz.js";import{f as C}from"./images-BCke6YSK.js";import{S as I}from"./SnapshotForm-DaAZBH3o.js";import{t as K,a as Q,b as U,g as _}from"./snapshots-Ch4qmIVo.js";import{c as z,a as g}from"./index.esm-9c567zNd.js";import{c as B}from"./operations-Bh89JbNy.js";import{R as j}from"./ResourceLink-DsyYS3_L.js";const se=({volume:e})=>{const{data:t}=S({queryKey:[d.storage,e.pool,e.type,e.name,e.project],queryFn:()=>b(e.pool,e.project,e.type,e.name),enabled:e.type!=="image"}),{data:s=[]}=S({queryKey:[d.images,e.project],queryFn:()=>C(e.project),enabled:e.type==="image"}),o=(()=>{var n,i;return e.type==="image"?(n=s.find(p=>p.fingerprint===e.name))==null?void 0:n.size:(i=t==null?void 0:t.usage)==null?void 0:i.used})();return r.jsx(r.Fragment,{children:o?V(o):"-"})},J=e=>{const{volume:t,name:s,expiresAt:a}=e;return new Promise((o,n)=>{fetch(`/1.0/storage-pools/${t.pool}/volumes/custom/${t.name}/snapshots?project=${t.project}`,{method:"POST",body:JSON.stringify({name:s,expires_at:a})}).then(m).then(o).catch(n)})},L=(e,t)=>new Promise((s,a)=>{fetch(`/1.0/storage-pools/${e.pool}/volumes/${e.type}/${e.name}/snapshots/${t.name}?project=${e.project}`,{method:"DELETE"}).then(m).then(s).catch(a)}),ae=(e,t,s)=>{const a=[];return new Promise(o=>{Promise.allSettled(t.map(async n=>await L(e,{name:n}).then(i=>{s.set(i.metadata.id,()=>T(a),p=>f(a,p),()=>y(a,t.length,o))}).catch(i=>{f(a,i instanceof Error?i.message:""),y(a,t.length,o)})))})},oe=(e,t)=>new Promise((s,a)=>{fetch(`/1.0/storage-pools/${e.pool}/volumes/${e.type}/${e.name}?project=${e.project}`,{method:"PUT",body:JSON.stringify({restore:t.name})}).then(m).then(s).catch(a)}),ne=e=>{const{volume:t,snapshot:s,newName:a}=e;return new Promise((o,n)=>{fetch(`/1.0/storage-pools/${t.pool}/volumes/${t.type}/${t.name}/snapshots/${s.name}?project=${t.project}`,{method:"POST",body:JSON.stringify({name:a})}).then(m).then(o).catch(n)})},re=e=>{const{volume:t,snapshot:s,expiresAt:a}=e;return new Promise((o,n)=>{fetch(`/1.0/storage-pools/${t.pool}/volumes/${t.type}/${t.name}/snapshots/${s.name}?project=${t.project}`,{method:"PUT",body:JSON.stringify({expires_at:a})}).then(m).then(o).catch(n)})},ie=e=>{const{pool:t,type:s,volumeName:a,project:o}=e;return new Promise((n,i)=>{fetch(`/1.0/storage-pools/${t}/volumes/${s}/${a}/snapshots?project=${o}&recursion=2`).then(m).then(p=>n(p.metadata.map(c=>({...c,name:P(c.name).snapshotName})))).catch(i)})},R=(e,t,s)=>["deduplicate","Snapshot name already in use",a=>s&&a===s||E(a,e.project,t,`storage-pools/${e.pool}/volumes/custom/${e.name}/snapshots`)],M=(e,t,s)=>z().shape({name:g().test(...R(e,t,s)).matches(/^[A-Za-z0-9-_.:]+$/,{message:"Please enter only alphanumeric characters, underscores (_), periods (.), hyphens (-), and colons (:) in this field"}),expirationDate:g().nullable().optional().test(...K()).test(...Q()),expirationTime:g().nullable().optional().test(...U())}),X=({name:e,volume:t})=>r.jsx(j,{type:"snapshot",value:e,to:`/ui/project/${t.project}/storage/pool/${t.pool}/volumes/custom/${t.name}/snapshots`}),Y=({close:e,volume:t})=>{const s=F(),a=h.useNotify(),o=N(),n=w(),i=k.useState(null),p=A({initialValues:{name:"",expirationDate:null,expirationTime:null},validateOnMount:!0,validationSchema:M(t,i),onSubmit:(c,{resetForm:$})=>{a.clear();const x=c.expirationDate&&c.expirationTime?D(_(c.expirationDate,c.expirationTime)):O;J({volume:t,name:c.name,expiresAt:x}).then(l=>{s.set(l.metadata.id,()=>{n.invalidateQueries({predicate:u=>u.queryKey[0]===d.volumes||u.queryKey[0]===d.storage}),o.success(r.jsxs(r.Fragment,{children:["Snapshot"," ",r.jsx(X,{name:B(l.metadata),volume:t})," ","created for volume"," ",r.jsx(j,{type:"volume",value:t.name,to:`/ui/project/${t.project}/storage/pool/${t.pool}/volumes/custom/${t.name}`}),"."]})),e(),$()},u=>{o.failure(`Snapshot ${c.name} creation failed`,new Error(u)),p.setSubmitting(!1)})}).catch(l=>{a.failure("Snapshot creation failed",l),p.setSubmitting(!1),e()})}});return r.jsx(I,{isEdit:!1,formik:p,close:e})},pe=({volume:e,isCTA:t,isDisabled:s,className:a})=>{const{openPortal:o,closePortal:n,isOpen:i,Portal:p}=q();return r.jsxs(r.Fragment,{children:[i?r.jsx(p,{children:r.jsx(Y,{volume:e,close:n})}):null,t?r.jsx(h.Button,{appearance:"base",hasIcon:!0,dense:!0,onClick:o,type:"button","aria-label":"Add Snapshot",title:s?`Snapshot creation is blocked for project ${e.project}`:"Add Snapshot",disabled:s,className:a,children:r.jsx(h.Icon,{name:"add-canvas"})}):r.jsx(h.Button,{appearance:"positive",className:a,onClick:o,disabled:s,children:s?r.jsx(h.Tooltip,{message:`Snapshot creation has been disabled for volumes in the project ${e.project}`,children:"Create snapshot"}):"Create snapshot"})]})};export{se as S,pe as V,X as a,oe as b,ae as c,L as d,ie as f,M as g,ne as r,re as u};
