import{aa as u}from"./index-AreTu2xo.js";function h(v,o){v.forEach(a=>{var i;const p=(i=a.metadata)==null?void 0:i.name;if(a.versions&&a.versions.length>0){const c=a.versions[0];if(c&&typeof c.version=="string"){const t=c.version;(c.resources||[]).forEach(e=>{var d,y;if(!e.verbs||!e.verbs.includes("list")||!e.resource||!((d=e.responseKind)!=null&&d.kind)||(y=e.responseKind)!=null&&y.kind.endsWith("List"))return;const n=e.resource,s=e.singularResource||"",r=e.responseKind.kind,f=e.scope==="Namespaced",l={apiVersion:p?`${p}/${t}`:t,version:t,groupName:p,pluralName:n,singularName:s,kind:r,isNamespaced:f};o.set(l.apiVersion+l.pluralName,l)})}}})}function g(v,o,a,p){v.forEach(i=>{if(!i.verbs||!i.verbs.includes("list")||!i.name||!i.kind||i.kind.endsWith("List"))return;const c=i.name,t=i.singularName||"",e=i.kind,n=!!i.namespaced,r={apiVersion:o?`${o}/${a}`:a,version:a,groupName:o,pluralName:c,singularName:t,kind:e,isNamespaced:n};p.set(r.apiVersion+r.pluralName,r)})}async function P(v){const o=new Map;for(const a of v){let p=!1;try{const i=u("/api",{cluster:a,headers:{Accept:"application/json;v=v2;g=apidiscovery.k8s.io;as=APIGroupDiscoveryList"}}).then(r=>r.json()),c=u("/apis",{cluster:a,headers:{Accept:"application/json;v=v2;g=apidiscovery.k8s.io;as=APIGroupDiscoveryList"}}).then(r=>r.json()),[t,e]=await Promise.allSettled([i,c]);let n=!1;t.status==="fulfilled"&&t.value&&Array.isArray(t.value.items)&&(h(t.value.items,o),n=!0);let s=!1;e.status==="fulfilled"&&e.value&&Array.isArray(e.value.items)&&(h(e.value.items,o),s=!0),(!n||!s)&&(p=!0)}catch{p=!0}if(p)try{const i=u("/api",{cluster:a}).then(n=>n.json()).catch(()=>null),c=u("/apis",{cluster:a}).then(n=>n.json()).catch(()=>null),[t,e]=await Promise.all([i,c]);if(t&&Array.isArray(t.versions)){const n=t.versions.map(async s=>{try{if(typeof s=="string"&&s.length>0){const f=await(await u(`/api/${s}`,{cluster:a})).json();f&&Array.isArray(f.resources)&&g(f.resources,void 0,s,o)}}catch(r){console.debug(`Failed to fetch core API resources for cluster ${a}:`,{version:s},r)}});await Promise.allSettled(n)}if(e&&Array.isArray(e.groups)){const n=e.groups.map(async s=>{var r,f,m;try{const l=s==null?void 0:s.name;let d=(r=s==null?void 0:s.preferredVersion)==null?void 0:r.version;if(!d&&(s!=null&&s.versions)&&s.versions.length>0&&((f=s.versions[0])!=null&&f.version)&&(d=s.versions[0].version),typeof l=="string"&&l.length>0&&typeof d=="string"&&d.length>0){const A=await(await u(`/apis/${l}/${d}`,{cluster:a})).json();A&&Array.isArray(A.resources)&&g(A.resources,l,d,o)}}catch(l){console.debug(`Failed to fetch group API resources for cluster ${a}:`,{group:s==null?void 0:s.name,version:(m=s==null?void 0:s.preferredVersion)==null?void 0:m.version},l)}});await Promise.allSettled(n)}}catch(i){console.debug(`Failed to fetch legacy API resources for cluster ${a}:`,i)}}return Array.from(o.values())}export{P as a};
