import{R as p,bI as h,bK as L,bL as b,bM as S,eR as E,db as P,F,W as q,X as O,eS as A,a as R,aT as f,Q as T,a4 as U,q as I}from"./index-Dz5JRQhj.js";import{f as C,a as K}from"./image-registries-8O1zsvZX.js";import{I as v,p as x}from"./imageServers-B4hb8Ggx.js";const j=["can_delete"],_=async(s,e)=>{const t=new URLSearchParams;return t.set("recursion","1"),t.set("project",s),P(t,e,j),fetch(`${p}/1.0/images?${t.toString()}`).then(h).then(n=>n.metadata)},k=async s=>{const e=new URLSearchParams;return e.set("recursion","1"),e.set("all-projects","1"),P(e,s,j),fetch(`${p}/1.0/images?${e.toString()}`).then(h).then(t=>t.metadata)},M=async(s,e)=>{const t=new URLSearchParams;return t.set("project",e),fetch(`${p}/1.0/images/${encodeURIComponent(s.fingerprint)}?${t.toString()}`,{method:"DELETE"}).then(h).then(n=>n)},D=async(s,e,t)=>{const n=[];return new Promise((a,r)=>{Promise.allSettled(s.map(async i=>{const l={fingerprint:i},u={name:i,type:"image",href:`${p}/ui/project/${encodeURIComponent(e)}/local-images`};return M(l,e).then(c=>{t.set(c.metadata.id,()=>{L(n,u)},y=>{b(n,y,u)},()=>{S(n,s.length,a)})}).catch(c=>{b(n,c instanceof Error?c.message:"",u),S(n,s.length,a)})})).catch(r)})},H=async(s,e,t)=>{const n=new URLSearchParams;n.set("project",t),await fetch(`${p}/1.0/images/aliases?${n.toString()}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({target:s,name:e})}).then(h)},W=async(s,e)=>{const t=new URLSearchParams;return t.set("project",e),fetch(`${p}/1.0/images?${t.toString()}`,{method:"POST",headers:{"Content-Type":"application/json"},body:s}).then(h).then(n=>n)},Y=async(s,e,t,n)=>{const a=new URLSearchParams;return a.set("project",n),E.post(`${p}/1.0/images?${a.toString()}`,s,{headers:{"Content-Type":"application/octet-stream","X-Incus-public":JSON.stringify(e)},onUploadProgress:r=>{t({percentage:r.progress?Math.floor(r.progress*100):0,loaded:r.loaded,total:r.total})}}).then(r=>r.data)},B="https://images.linuxcontainers.org",G=s=>`${s.replace(/\/$/,"")}/streams/v1/images.json`,J=async(s=[])=>{const e=s.length>0?s.map(r=>({url:r.url,name:r.name})):[{url:B,name:void 0}],t=await Promise.all(e.map(async({url:r,name:i})=>N(G(r),r,i))),n=t.flatMap(r=>r.images),a=t.map(r=>r.error).filter(r=>r!=="");return{images:n,error:a.join(". ")}},N=async(s,e,t)=>new Promise(n=>{fetch(s).then(h).then(a=>{const r=Object.entries(a.products).map(i=>{const{os:l,...u}=i[1],c=F(l);return{...u,os:c,server:e,serverName:t}});n({images:r,error:""})}).catch(a=>{n({images:[],error:a.message})})}),V=async(s,e)=>{const t=await C(s),n=o=>e?.config.restricted?(e?.config["restricted.registries"]?.split(",")??[]).includes(o.name):!0,a={},r=await Promise.allSettled(t.filter(n).map(async o=>{const m=await K(o.name,s);a[o.name]=m.filter(g=>!(o.builtin&&g.aliases===null)).map(g=>{const d=q(g),$=g.aliases?.find(w=>w.name==="lts");return{...d,isLts:$!==void 0,registryBuiltIn:o.builtin,registryName:o.name,server:o.config?.url,title:d.os+d.release_title+d.release+d.server}}).sort(O)})),i=[];r.forEach(o=>{o.status!=="fulfilled"&&i.push(o.reason)});const l=(o,m,g)=>m===g.findIndex(d=>d.title===o.title),u=o=>{delete o.type,o.versions={vm:{items:{"disk1.img":{ftype:"disk1.img"}}}}},c=["ubuntu","ubuntu-minimal","ubuntu-daily","ubuntu-minimal-daily"];c.forEach(o=>{const m=o in a?a[o]:[];a[o]=m.filter(l),a[o].map(u),a[o].reverse().sort(A)});const y=[...a.ubuntu,...a["ubuntu-minimal"],..."images"in a?a.images:[],...a["ubuntu-daily"],...a["ubuntu-minimal-daily"]];return Object.entries(a).forEach(([o,m])=>{!c.includes(o)&&o!=="images"&&y.push(...m)}),{images:y,error:i.join(". ")}},Z=(s,e)=>{const{isFineGrained:t}=R();return f({queryKey:[I.images,s],queryFn:async()=>_(s,t),enabled:(e??!0)&&t!==null})},ee=s=>{const{isFineGrained:e}=R();return f({queryKey:[I.images],queryFn:async()=>k(e),enabled:e!==null})},se=()=>{const{isFineGrained:s}=R(),{hasImageRegistries:e,settings:t}=T(),{project:n}=U(),a=t?.config?.[v],r=x(a),i=f({queryKey:[I.images,"selector",e,a],queryFn:async()=>J(r),enabled:!e}),l=f({queryKey:[I.images,"selector",e,n?.name],queryFn:async()=>V(!!s,n),enabled:e&&s!==null&&n!==null});return e?l:i};export{Z as a,D as b,H as c,M as d,W as e,ee as f,Y as i,B as l,se as u};
