import{GraphQLEnumType as _,GraphQLInputObjectType as Q,GraphQLInterfaceType as S,GraphQLList as j,GraphQLNonNull as R,GraphQLObjectType as D,GraphQLScalarType as M,GraphQLUnionType as P,Kind as a}from"graphql";const V={queries:(n,e)=>n.getQueryTypes(e),mutations:(n,e)=>n.getMutationTypes(e),subscriptions:(n,e)=>n.getSubscriptionTypes(e),directives:(n,e)=>n.getDirectiveTypes(e),interfaces:(n,e)=>n.getTypes(a.INTERFACE_TYPE_DEFINITION,e),objects:(n,e)=>n.getTypes(a.OBJECT_TYPE_DEFINITION,e),unions:(n,e)=>n.getTypes(a.UNION_TYPE_DEFINITION,e),scalars:(n,e)=>n.getTypes(a.SCALAR_TYPE_DEFINITION,e),enums:(n,e)=>n.getTypes(a.ENUM_TYPE_DEFINITION,e),inputs:(n,e)=>n.getTypes(a.INPUT_OBJECT_TYPE_DEFINITION,e)};function w(n,e,i){const t=V[n];return t?t(e,i).map(s=>E(s?.name??"",n,e)):[]}function E(n,e,i){switch(e){case"queries":return g(i.getQueryField(n),e);case"mutations":return g(i.getMutationField(n),e);case"subscriptions":return g(i.getSubscriptionField(n),e);case"directives":const t=i.getDirective(n);return{name:t?.name,group:e,description:t?.description||void 0,locations:t?.locations.map(f=>f.toString()),isRepeatable:!!t?.isRepeatable,args:F(t?.args)};case"interfaces":return m(i.getType(n),e);case"objects":return m(i.getType(n),e);case"unions":const r=i.getType(n),s=r?Y(r):[];return{name:r?.name,group:e,description:r?.description||void 0,types:s};case"scalars":const o=i.getType(n);return{name:o?.name,group:e,description:o?.description||void 0};case"enums":const u=i.getType(n),d=u?A(u):[];return{name:u?.name,group:e,description:u?.description||void 0,values:d};case"inputs":return m(i.getType(n),e);default:return}}function g(n,e){return{name:n?.name,group:e,description:n?.description??void 0,response:n?$(n.type):void 0,args:F(n?.args)}}function m(n,e){return{name:n?.name,group:e,description:n?.description||void 0,fields:n?y(n.getFields(),1,[]):void 0}}function Y(n){return n.getTypes().map(({name:e,description:i})=>({name:e,description:i||void 0}))}function A(n){return n.getValues().map(({name:e,description:i,value:t})=>({name:e,description:i||void 0,value:t}))}function U({label:n,groupName:e,store:i,isTypeGroup:t,items:r}){const s=e.toLowerCase();return t?w(s,i,r):E(n,s,i)}function $(n,e=1,i=[]){const{type:t,list:r,notNull:s}=l(n),o=t.toString();if(i.includes(o)||i.length>=e)return o;if(t instanceof D)return{name:t.name,description:t.description||void 0,list:r,notNull:s,fields:y(t.getFields(),e,[...i,o])};if(t instanceof S)return{name:t.name,description:t.description||void 0,list:r,notNull:s,fields:y(t.getFields(),e,[...i,o])};if(t instanceof P)return{name:t.name,description:t.description||void 0,list:r,notNull:s,types:t.getTypes().map(u=>u.name)};if(t instanceof _){const u=t.getValues().map(d=>({name:d.name,description:d.description||void 0,value:d.value}));return{name:t.name,description:t.description||void 0,list:r,notNull:s,values:u}}else{if(t instanceof M)return o;if(t instanceof Q)return{name:t.name,description:t.description||void 0,list:r,notNull:s,fields:y(t.getFields(),e,[...i,o])}}}function F(n,e=3){if(!n)return;const i={};return n.forEach(t=>{i[t.name]={type:$(t.type,e),description:t.description??void 0}}),i}function y(n,e,i){if(!n)return;const t={};return Object.entries(n).forEach(([r,s])=>{t[r]={type:$(s.type,e,i),description:s.description,defaultValue:s.defaultValue}}),t}function l(n){return n instanceof R?{type:l(n.ofType).type,notNull:!0}:n instanceof j?{type:l(n.ofType).type,list:!0}:{type:n}}function p(n,e=2){return"#".repeat(Math.min(6,e+n-1))}function B(n,e=1){const{name:i,group:t,description:r,args:s,response:o,locations:u,isRepeatable:d,types:f,values:I,fields:N}=n;let c=`${p(1,e)} ${i}

**Type:** GraphQL ${t}

`;return r&&(c+=`**Description:**
${r}

`),f&&(c+=`${p(2,e)} Types
${h(f)}

`),I&&(c+=`${p(2,e)} Values
${h(I)}

`),N&&(c+=`${p(2,e)} Fields
${T(N)}

`),s&&Object.keys(s).length>0&&(c+=`${p(2,e)} Arguments
${T(s)}

`),o&&(typeof o=="string"?c+=`${p(2,e)} Response
**Type:** ${o}
`:(c+=`${p(2,e)} Response
**Type:** ${o.name}
**Description:**
${o.description}

`,c+=o.fields?`${p(3,e)} Fields
${T(o.fields)}
`:"")),c+=d?`${p(2,e)} Repeatable
`:"",c+=u?`${p(2,e)} Locations
${u.map(O=>`- ${O}`).join(`
`)}
`:"",c}function h(n){return n.map(e=>`- ${e.name}${e.description?`: ${e.description}`:""}`).join(`
`)}function T(n,e=0){const i="  ".repeat(e);return n?Object.entries(n).map(([t,r])=>typeof r.type=="string"?`${i}- ${t} (${r.type})${r.description?`: ${r.description}`:""}`:`${i}- ${t} (${r.type?.name}${r.type?.notNull?", Not Null":""}): ${r.description}
${T(r.type?.fields??{},e+1)}`).join(`
`):""}export{B as convertDocumentToMarkdown,U as getSearchDocument};
