import{getNamedType as p,isEnumType as v,isInputObjectType as y,isInterfaceType as g,isIntrospectionType as u,isObjectType as T,isScalarType as m,isSpecifiedScalarType as S,isUnionType as b,print as c,printType as N,specifiedDirectives as O}from"graphql";const j=new Set(["queries","mutations","subscriptions"]);function C(e,r){const t=r.toLowerCase();return e.find(i=>i.name.toLowerCase().includes(t))}function a(e,r){return e.getQueryType()?.getFields()[r]??e.getMutationType()?.getFields()[r]??e.getSubscriptionType()?.getFields()[r]}function w(e){return[p(e.type).name,...e.args.map(r=>p(r.type).name)]}function d(e){const r=new Set;if(T(e)||g(e)){for(const t of Object.values(e.getFields())){r.add(p(t.type).name);for(const i of t.args)r.add(p(i.type).name)}for(const t of e.getInterfaces())r.add(t.name)}else if(b(e))for(const t of e.getTypes())r.add(t.name);else if(y(e))for(const t of Object.values(e.getFields()))r.add(p(t.type).name);return r}function $(e){const r=new Set([e.getQueryType(),e.getMutationType(),e.getSubscriptionType()].filter(n=>!!n).map(n=>n.name)),t={queries:l(e.getQueryType()),mutations:l(e.getMutationType()),subscriptions:l(e.getSubscriptionType()),objects:[],interfaces:[],unions:[],enums:[],inputs:[],scalars:[],directives:[]};for(const n of Object.values(e.getTypeMap())){if(u(n)||r.has(n.name))continue;const o={name:n.name,description:n.description??void 0};m(n)?S(n)||t.scalars.push(o):T(n)?t.objects.push(o):g(n)?t.interfaces.push(o):b(n)?t.unions.push(o):v(n)?t.enums.push(o):y(n)&&t.inputs.push(o)}const i=new Set(O.map(n=>n.name));return t.directives=e.getDirectives().filter(n=>!i.has(n.name)).map(n=>({name:n.name,description:n.description??void 0})),t}function l(e){return e?Object.values(e.getFields()).map(r=>({name:r.name,description:r.description??void 0,type:r.type.toString(),args:r.args.length?`(${r.args.map(t=>{const i=t.astNode?.defaultValue?` = ${c(t.astNode.defaultValue)}`:"";return`${t.name}: ${t.type}${i}`}).join(", ")})`:void 0})):[]}function q(e,r,t){const i=h(e,t);if(!i)return[];const n=new Set([i,...x(e,i)]);return r.filter(o=>{if(!j.has(o.kind))return n.has(o.name);const s=a(e,o.name);return s?w(s).some(f=>n.has(f)):!1})}function F(e,r){const t=new Set,i=[...M(e,r)];for(let n=0;n<i.length;n+=1){const o=i[n];if(t.has(o))continue;t.add(o);const s=e.getType(o);if(s)for(const f of d(s))t.has(f)||i.push(f)}return[...t].filter(n=>n!==r&&D(e,n))}function M(e,r){const t=e.getType(r);if(t)return d(t);const i=a(e,r);if(i)return new Set(w(i));const n=e.getDirective(r);return n?new Set(n.args.map(o=>p(o.type).name)):new Set}function x(e,r){const t=new Map;for(const o of Object.values(e.getTypeMap()))if(!u(o))for(const s of d(o)){let f=t.get(s);f||(f=new Set,t.set(s,f)),f.add(o.name)}const i=new Set,n=[r];for(let o=0;o<n.length;o+=1)for(const s of t.get(n[o])??[])i.has(s)||(i.add(s),n.push(s));return i}function h(e,r){const t=r.toLowerCase();return Object.keys(e.getTypeMap()).find(i=>i.toLowerCase()===t)}function D(e,r){const t=e.getType(r);return!t||u(t)?!1:!(m(t)&&S(t))}function I(e,r){const t=e.getType(r);if(t)return N(t);const i=a(e,r);if(i?.astNode)return c(i.astNode);const n=e.getDirective(r);if(n?.astNode)return c(n.astNode);throw new Error(`No GraphQL type, operation, or directive named "${r}" found.`)}function L(e,r){const t=I(e,r),i=F(e,r).map(n=>e.getType(n)).filter(n=>!!n).map(n=>N(n));return{sdl:[t,...i].join(`

`),referencedCount:i.length}}export{q as filterMembersReferencingType,C as findGraphqlSchemaByName,$ as groupSchemaTypes,I as printTypeOrField,L as printTypeOrFieldWithReferences,F as referencedTypeClosure};
