import{j as e,p as U,h as _,F as q,r as o,d as t,s as z,K as C,q as b}from"./index-lOezWVmi.js";import{P as Q}from"./ProgressBar-RG8c1Ouf.js";import{S as V}from"./StoragePoolSelector-IgEwUopy.js";import{a as B}from"./storage-volumes-CuHKF2BE.js";const K=()=>e.jsx(e.Fragment,{children:e.jsx("div",{className:"p-notification--information",children:e.jsx("div",{className:"p-notification__content",children:e.jsx("h3",{className:"p-notification__title",children:"Some image formats need to be modified in order to work with Incus."})})})}),L=l=>l.replace(/[^A-Za-z0-9.-]/g,"-"),N=l=>/^[A-Za-z0-9.-]+$/.test(l)||!l,R=({onCancel:l,onFinish:y})=>{const I=U(),F=_(),{project:u}=q(),[i,A]=o.useState(null),[n,x]=o.useState(""),[j,m]=o.useState(!1),[c,E]=o.useState(""),[a,p]=o.useState(null),[f,P]=o.useState(null),[h,d]=o.useState(null),S=(u==null?void 0:u.name)??"",k=()=>{f==null||f.abort(),l()},w=()=>{if(!i)return;d(null),m(!0);const s=new AbortController;P(s),B(c,i,n,S,p,s).then(r=>{I.set(r.metadata.id,()=>{y(n,c)},g=>{d(t.failure("Custom ISO upload failed",new Error(g)))},()=>{m(!1),p(null),F.invalidateQueries({queryKey:[b.storage,c,b.volumes,S]})})}).catch(r=>{var v;const g=new Error((v=r.response)==null?void 0:v.data.error);d(t.failure("Custom ISO upload failed",g)),m(!1),p(null)})},O=s=>{if(s.target.files){const r=s.target.files[0];A(r),x(L(r.name))}};return e.jsxs(e.Fragment,{children:[h?e.jsx(t.Notification,{title:h.title,severity:"negative",onDismiss:()=>{d(null)},children:h.message}):e.jsx(K,{}),e.jsxs("div",{className:z("custom-iso-form",{"u-hide":!!a}),children:[e.jsx(t.Input,{name:"iso",type:"file",id:"iso-image",label:"Local file",onChange:O,stacked:!0}),e.jsx(t.Input,{name:"name",type:"text",id:"name",label:"Alias",value:n,onChange:s=>{x(s.target.value)},disabled:i===null,error:n&&!N(n)?e.jsx("div",{className:"alias-error",children:"Only alphanumeric characters, periods or hyphens are allowed in this field"}):void 0,stacked:!0}),e.jsx(V,{value:c,setValue:E,selectProps:{id:"storagePool",label:"Storage pool",disabled:i===null,stacked:!0}})]}),a&&e.jsxs(e.Fragment,{children:[e.jsx(Q,{percentage:Math.floor(a.percentage)}),e.jsxs("p",{children:[C(a.loaded)," loaded of"," ",C(a.total??0)]}),a.loaded===a.total&&e.jsx(t.Spinner,{className:"u-loader",text:"Validating ISO"})]}),e.jsxs("footer",{className:"p-modal__footer",children:[e.jsx(t.Button,{appearance:"base",onClick:k,className:"u-no-margin--bottom",children:"Cancel"}),e.jsx(t.ActionButton,{appearance:"positive",loading:j,disabled:!i||!c||!N(n)||j,className:"u-no-margin--bottom",onClick:w,children:"Upload"})]})]})};export{R as U};
