import{writeFileSync as v}from"node:fs";import C from"node:path";import{REDOCLY_TEAMS_RBAC as $}from"@redocly/config";import{combineUrls as q}from"@redocly/theme/core/utils";import{PUBLIC_API_DEFINITIONS_FOLDER as w}from"../../constants/common.js";import{GRAPHQL_TEMPLATE_ID as O,GRAPHQL_SPEC_SLUG as Q,PUBLIC_RBAC_SCOPE_ITEM as H}from"../../../constants/common.js";import{removeTrailingSlash as I}from"../../../utils/url/remove-trailing-slash.js";import{getTemplatePath as N}from"./get-template-path.js";import{searchResolver as F}from"./search/search-resolver.js";import{graphqlDocLoader as M,graphqlDocsLoader as U}from"./graphql-doc-loader.js";import{getAiDocumentsStore as _}from"./search/ai/get-ai-search-document.js";import{fromCurrentDir as k}from"../../utils/paths.js";import{ensureDir as x}from"../../utils/fs.js";import{telemetryTraceStep as B}from"../../../cli/telemetry/helpers/trace-step.js";const j="graphql-docs-",R="graphql-spec-download";async function nt(){return{id:"graphql",requiredEntitlements:["graphql"],loaders:{"graphql-doc":M,"load-graphql-docs":U},processContent:async(t,{fs:l,cache:n,isPathIgnored:i,withPathPrefix:p})=>{await B("build.plugin.graphql_docs",async()=>{t.createRequestHandler(R,k(import.meta.url,"./spec-download.api.js")),t.addApiRoute({slug:Q+"/*",requestHandlerId:R,httpMethod:"all",[$]:H,getStaticData:async()=>({props:{}})});const m=t.createTemplate(O,N("./template/GraphQLDocs.js"));for(const{relativePath:o}of l.scan(/(\.gql|\.graphql)$/)){if(await i(o))continue;const h=await n.load(o,"graphql-doc");if(!h.data)continue;K(h.data.content,t.outdir,o);const{menu:A,content:G,settings:u,metadata:T,store:d}=h.data,L=`${j}${o}`;await t.createSharedData(L,G);const c=[{key:"graphQlSettings",id:L}],b=A.getGroups(),y=A.getSidebarItems(),g={type:"graphql",title:b[0].name,...T},s=[{slugSuffix:"",fsPath:o,templateId:m,getStaticData:S(void 0,u,p),sharedData:c,getAiDocumentsStore:_({label:u.info?.title??"GraphQL Overview",groupName:"overview",metadata:g,actions:t,store:d,includeInLLMsTxt:!0})}];for(const r of b){const f=`${r.id}`;s.push({slugSuffix:f,fsPath:o,templateId:m,getStaticData:S(r,u,p),sharedData:c});for(const e of r.typeGroups){const a=`${e.id}`;s.push({slugSuffix:a,fsPath:o,templateId:m,getStaticData:S(e,u,p),sharedData:c,getAiDocumentsStore:_({label:e.name,groupName:e.name,metadata:g,actions:t,store:d,isTypeGroup:!0,items:new Set(e.items)})});for(const E of e.items){const P=`${e.id}/${E}`;s.push({slugSuffix:P,fsPath:o,templateId:m,getStaticData:S(e,u,p),sharedData:c,getAiDocumentsStore:_({label:E,groupName:e.name,metadata:g,actions:t,store:d})})}}}if(!s.length)return;const D=s[s.length-1];D.metadata=g,D.getSidebar=r=>{function f(e){return e.map(a=>({...a,slug:a.slug&&I(q(r.baseSlug,a.slug)),routeSlug:a.routeSlug&&I(q(r.baseSlug,a.routeSlug)),link:a.link&&I(q(r.baseSlug,a.link)),items:a.items&&f(a.items)}))}return f(y)},D.getSearchDocuments=F(t,d);for(const r of s)t.addRoute(r)}})}}}function S(t,l,n){return async function(i){return{props:{seo:{title:t?.name??l.info?.title??"GraphQL Overview"},settings:{...l,location:{section:t?.id},sidebar:{hide:!0},baseUrlPath:n(i.baseSlug)},disableAutoScroll:!0}}}}function K(t,l,n){const i=w.slice(1);v(x(C.resolve(l,`${i}/${n}`)),t,"utf8")}export{nt as graphqlDocsPlugin,K as storeGqlSchema};
