import{R as p,bE as h,bG as E,bH as b,bI as S,eM as F,d7 as P,F as L,W as q,X as O,eN as A,a as R,aP as f,Q as T,a4 as U,q as I}from"./index-C88DWJny.js";import{f as C,a as v}from"./image-registries-T9CKqPfx.js";import{I as x,p as K}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)},G=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)},z=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 G(l,e).then(c=>{t.set(c.metadata.id,()=>{E(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)})},D=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),F.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)},M="https://images.linuxcontainers.org",N=s=>`${s.replace(/\/$/,"")}/streams/v1/images.json`,B=async(s=[])=>{const e=s.length>0?s.map(r=>({url:r.url,name:r.name})):[{url:M,name:void 0}],t=await Promise.all(e.map(async({url:r,name:i})=>J(N(r),r,i))),n=t.flatMap(r=>r.images),a=t.map(r=>r.error).filter(r=>r!=="");return{images:n,error:a.join(". ")}},J=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=L(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 v(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?.[x],r=K(a),i=f({queryKey:[I.images,"selector",e,a],queryFn:async()=>B(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,z as b,D as c,G as d,W as e,ee as f,Y as i,M as l,se as u};
