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