import{ac as g}from"./index-wMBu2Jqu.js";const k=10,P=200;function h(t){if(t===null)return{type:"null"};if(Array.isArray(t))return{type:"array",length:t.length};if(typeof t=="object"){const i=[];let s=!1;for(const a in t)if(Object.prototype.hasOwnProperty.call(t,a))if(i.length<k)i.push(a);else{s=!0;break}return{type:"object",keys:i,truncated:s}}return typeof t=="string"?{type:"string",length:t.length,preview:t.slice(0,P)}:{type:typeof t,value:t}}function m(t,i,s){const a=s.status==="rejected"?s.reason instanceof Error?s.reason:h(s.reason):h(s.value);console.debug(`Aggregated ${t} discovery unusable for cluster ${i}; falling back to legacy:`,a)}function v(t,i){return g(t,{cluster:i}).then(s=>s.json()).then(s=>!s||typeof s!="object"||Array.isArray(s)?null:s).catch(s=>(console.debug(`Legacy ${t} discovery fetch or parse failed for cluster ${i}:`,s),null))}function b(t,i){t.forEach(s=>{var r;const a=(r=s.metadata)==null?void 0:r.name;if(s.versions&&s.versions.length>0){const l=s.versions[0];if(l&&typeof l.version=="string"){const n=l.version;(l.resources||[]).forEach(e=>{var p,A;if(!e.verbs||!e.verbs.includes("list")||!e.resource||!((p=e.responseKind)!=null&&p.kind)||(A=e.responseKind)!=null&&A.kind.endsWith("List"))return;const o=e.resource,c=e.singularResource||"",f=e.responseKind.kind,y=e.scope==="Namespaced",u={apiVersion:a?`${a}/${n}`:n,version:n,groupName:a,pluralName:o,singularName:c,kind:f,isNamespaced:y};i.set(u.apiVersion+u.pluralName,u)})}}})}function R(t,i,s,a){t.forEach(r=>{if(!r.verbs||!r.verbs.includes("list")||!r.name||!r.kind||r.kind.endsWith("List"))return;const l=r.name,n=r.singularName||"",e=r.kind,o=!!r.namespaced,f={apiVersion:i?`${i}/${s}`:s,version:s,groupName:i,pluralName:l,singularName:n,kind:e,isNamespaced:o};a.set(f.apiVersion+f.pluralName,f)})}async function j(t){const i=new Map;for(const s of t){let a=!1;try{const r=g("/api",{cluster:s,headers:{Accept:"application/json;v=v2;g=apidiscovery.k8s.io;as=APIGroupDiscoveryList"}}).then(f=>f.json()),l=g("/apis",{cluster:s,headers:{Accept:"application/json;v=v2;g=apidiscovery.k8s.io;as=APIGroupDiscoveryList"}}).then(f=>f.json()),[n,e]=await Promise.allSettled([r,l]);let o=!1;n.status==="fulfilled"&&n.value&&Array.isArray(n.value.items)&&(b(n.value.items,i),o=!0);let c=!1;e.status==="fulfilled"&&e.value&&Array.isArray(e.value.items)&&(b(e.value.items,i),c=!0),o||m("/api",s,n),c||m("/apis",s,e),(!o||!c)&&(a=!0)}catch(r){console.debug(`Aggregated API discovery threw for cluster ${s}; falling back to legacy:`,r),a=!0}if(a)try{const[r,l]=await Promise.all([v("/api",s),v("/apis",s)]);if(r&&Array.isArray(r.versions)){const n=r.versions.map(async e=>{try{if(typeof e=="string"&&e.length>0){const c=await(await g(`/api/${e}`,{cluster:s})).json();c&&Array.isArray(c.resources)&&R(c.resources,void 0,e,i)}}catch(o){console.debug(`Failed to fetch core API resources for cluster ${s}:`,{version:e},o)}});await Promise.allSettled(n)}if(l&&Array.isArray(l.groups)){const n=l.groups.map(async e=>{var o,c,f;try{const y=e==null?void 0:e.name;let d=(o=e==null?void 0:e.preferredVersion)==null?void 0:o.version;if(!d&&(e!=null&&e.versions)&&e.versions.length>0&&((c=e.versions[0])!=null&&c.version)&&(d=e.versions[0].version),typeof y=="string"&&y.length>0&&typeof d=="string"&&d.length>0){const p=await(await g(`/apis/${y}/${d}`,{cluster:s})).json();p&&Array.isArray(p.resources)&&R(p.resources,y,d,i)}}catch(y){console.debug(`Failed to fetch group API resources for cluster ${s}:`,{group:e==null?void 0:e.name,version:(f=e==null?void 0:e.preferredVersion)==null?void 0:f.version},y)}});await Promise.allSettled(n)}}catch(r){console.debug(`Failed to fetch legacy API resources for cluster ${s}:`,r)}}return Array.from(i.values())}export{j as a};
