import{REDOCLY_TEAMS_RBAC as w}from"@redocly/theme";import{basename as L,join as F}from"node:path";import{toMarkdown as R}from"../../../plugins/markdown/search/to-markdown.js";import{canAccessResource as N,canDownloadApiDefinition as _}from"../../../utils/rbac.js";import{PUBLIC_API_DEFINITIONS_FOLDER as q}from"../../../constants/common.js";import{SearchIndexer as z}from"../search-indexer.js";import{getLocaleFromRelativePath as C}from"../../../fs/utils/get-locale-from-relative-path.js";import{extractDocumentSearchFacets as B}from"./search-facets.js";import{formatDocumentMetadata as G}from"../../search/utils.js";import{getLLMsTxtMdSlug as J,llmsTxtLink as j}from"../../search/llmstxt/index.js";import{replaceFileExtension as V}from"../store-definition-bundles.js";const O=new Map,le=({parser:s,options:c,info:t,tagOperations:i,relativePath:n,openapiContentItem:e,metadata:r,getSearchFacets:o,includeInLLMsTxt:u,excludeFromSearch:d})=>async(a,f,k)=>{if(d)return;const U=await a.getNavText?.()||L(a.fsPath),x=new z(s,c,a.baseSlug||a.slug),l=x.addItem(e);if(!l)return;const S=await k.getConfig(),b=Array.isArray(l.title)?l.title.join(" "):l.title;let y,p,m=O.get(n);if((e.type==="tag"||e.type==="section"&&e.infoDefinition)&&!m){const{all:g,publiclyAccessible:$}=X(i.tagged,x,S),{all:D,publiclyAccessible:A}=Z(i.untagged,x,S);O.set(n,{taggedSearchDocuments:new Map(g),untaggedSearchDocuments:D,publiclyAccessibleTaggedSearchDocuments:new Map($),publiclyAccessibleUntaggedSearchDocuments:A})}m=O.get(n);const h="#";switch(e.type){case"operation":y=p=Q({title:b,security:s.definition.security,document:l,version:l.version||t.version,headingLevel:h});break;case"section":if(e.infoDefinition){m=O.get(n),p=await E({parser:s,info:t,staticData:f,relativePath:n,headingLevel:h,pageName:U,config:S}),p+=`
`;for(const[$,D]of m?.publiclyAccessibleTaggedSearchDocuments?.entries()||[]){if(!D.length)return;const A=s.definition.tags?.find(M=>M.name===$);p+=T({title:A?.["x-displayName"]||$,description:A?.description,operationSearchDocuments:D,headingLevel:`${h}#`})}const g=m?.publiclyAccessibleUntaggedSearchDocuments||[];g.length&&(p+=T({title:"Other",description:void 0,operationSearchDocuments:g,headingLevel:`${h}#`})),y=await E({parser:s,info:t,staticData:f,relativePath:n,headingLevel:h,pageName:U,config:S})}else e.ast&&(p=y=R(e.ast));break;case"tag":p=T({title:b,description:e.description,operationSearchDocuments:m?.publiclyAccessibleTaggedSearchDocuments.get(e.name)||[],headingLevel:h}),y=T({title:b,description:e.description,operationSearchDocuments:[],headingLevel:h});break}return{async getLLMsTxts(){return[{title:e.name,description:e.type==="tag"?e.description:void 0,content:p||"",slug:a.slug,fsPath:a.fsPath,includeInLLMsTxt:u}]},async getSearchDocuments(){if(e.type==="operation"||e.type==="section"&&(e.infoDefinition||e.ast)||e.type==="tag"){const g=C(a.fsPath),$=B({...l,...r},t,o);return[{title:b,description:Array.isArray(l.text)?l.text.join(" "):l.text,content:y||"",url:l.url??a.slug,fsPath:a.fsPath,locale:g,product:l.product?.name,rbacTeams:l.rbacTeams,facets:$}]}return[]}}};async function E({parser:s,info:c,staticData:t,relativePath:i,pageName:n,headingLevel:e,config:r}){const o=G(c["x-metadata"]);let u=c.title?`${e} ${c.title}

`:`${e} ${n}

`;return u+=c.description?`${c.description}

`:"",u+=c.summary?`${c.summary}

`:"",u+=c.version?`Version: ${c.version}
`:"",u+=c.license?`License: ${c.license.name}
`:"",e=`${e}#`,u+=o.length?`Metadata:
${o}
`:"",u+=`
`,u+=Y({parser:s,headingLevel:e}),u+=K({parser:s,headingLevel:e}),u+=await H({info:c,staticData:t,relativePath:i,pageName:n,headingLevel:e,config:r}),u}function T({title:s="",description:c,operationSearchDocuments:t,headingLevel:i}){let n=`${i} ${s}

`;return c&&(n+=`${c}

`),t.length&&t.forEach(e=>{const r=Array.isArray(e.title)?e.title.join(" "):e.title;n+=`${i}# ${r}${e.deprecated?" (deprecated)":""}

`,n+=` - ${j({title:`${e.httpMethod?.toUpperCase()} ${e.httpPath}`,description:Array.isArray(e.text)?e.text.join(" "):e.text,slug:J(e.url)})}`,n+=`
`}),n}function Y({parser:s,headingLevel:c}){const{servers:t}=s.definition;if(t&&t.length){let i=`${c} Servers

`;return t.forEach(n=>{i+=n.description?`${n.description}
`:"",i+=`\`\`\`
${n.url}
\`\`\`

`,n.variables&&(i+=`Variables:
`,Object.entries(n.variables).forEach(([e,r])=>{i+=`- \`${e}\`${r.description?`: ${r.description}`:""}
`,i+=r.default?`Default: ${JSON.stringify(r.default)}
`:"",i+=r.enum?`Enum: ${r.enum.map(o=>JSON.stringify(o)).join(", ")}
`:""}),i+=`
`)}),i}return""}async function H({info:s,staticData:c,relativePath:t,pageName:i,headingLevel:n,config:e}){const r=F(c.props?.outdir||"",q,V(t,".yaml"));if(_(r,e.rbac||{},e.requiresLogin||!1,{isAuthenticated:!1,teams:["anonymous"]})){let o=`${n} Download OpenAPI description

`;return o+=j({title:s.title||i||"OpenAPI definition",description:void 0,slug:r}),o}return""}function K({parser:s,headingLevel:c}){if(!s.definition.components?.securitySchemes)return"";let t=`${c} Security

`;const{securitySchemes:i}=s.definition.components;return Object.keys(i).forEach(n=>{const e=i[n];e&&(t+=`${c}# ${n}

`,t+=e.description?`${e.description}

`:"",e.type&&(t+=`Type: ${e.type}
`),e.openIdConnectUrl&&(t+=`OpenID Connect URL: ${e.openIdConnectUrl}
`),e.in&&(t+=`In: ${e.in}
`),e.name&&(t+=`Name: ${e.name}
`),e.scheme&&(t+=`Scheme: ${e.scheme}
`),e.bearerFormat&&(t+=`Bearer Format: ${e.bearerFormat}
`),e.flows?.implicit?.authorizationUrl&&(t+=`Authorization URL: ${e.flows.implicit?.authorizationUrl}
`),e.flows?.implicit?.scopes&&(t+=`Scopes:
`,Object.entries(e.flows?.implicit?.scopes||{}).forEach(([r,o])=>{t+=`- \`${r}\`: ${o}
`})),e.flows?.password?.tokenUrl&&(t+=`Token URL: ${e.flows.password?.tokenUrl}
`),e.flows?.password?.scopes&&(t+=`Scopes:
`,Object.entries(e.flows?.password?.scopes||{}).forEach(([r,o])=>{t+=`- \`${r}\`: ${o}
`})),t+=`
`)}),t}function Q({title:s,security:c,document:t,version:i,headingLevel:n}){const{text:e,httpMethod:r,httpPath:o,deprecated:u}=t,d=W(t.parameters||[],`${n}#`);let a=s?`${n} ${s}${u?" (deprecated)":""}

`:"";return a+=e?`${e}

`:"",a+=`Endpoint: ${r?.toUpperCase()} ${o}
`,a+=i?`Version: ${i}
`:"",a+=t.security?.length?`Security: ${t.security.join(", ")}
`:c?.length?`Security: ${c.map(f=>f.id).join(", ")}
`:"",a+=`
`,a+=d?`${d}
`:"",a}function W(s,c){const t={};for(const n of s){const e=`${n.place}${n.mediaType?` (${n.mediaType})`:""}`;t[e]=[...t[e]||[],n]}return Object.entries(t).map(([n,e])=>{const r=e.map(o=>{const u="    ",d=[...o.path||[],o.name],a=Array.isArray(o.description)?o.description.join(" "):o.description;let f=`  - \`${d.join(".")}\` (${o.type}${o.required?", required":""})
`;return f+=a?`${u}${a.trim()}
`:"",o.enum?f+=`${u}Enum: ${o.enum.map(k=>JSON.stringify(k)).join(", ")}
`:o.example&&(f+=`${u}Example: ${o.example}
`),f});return`${c} ${v(n)}:

${r.join(`
`)}`}).join(`
`)}function X(s,c,t){const i=[],n=[];return s.forEach((e,r)=>{const o=[],u=[];e.forEach(d=>{const a=c.addItem(d);a&&(o.push(a),P({[w]:d[w],slug:a.url},t)&&u.push(a))}),i.push([r,o]),n.push([r,u])}),{all:i,publiclyAccessible:n}}function Z(s,c,t){const i=[],n=[];return s.forEach(e=>{const r=c.addItem(e);r&&(i.push(r),P({[w]:e[w],slug:r.url},t)&&n.push(r))}),{all:i,publiclyAccessible:n}}function v(s){return s.charAt(0).toUpperCase()+s.slice(1)}function P(s,c){return N(s,{isAuthenticated:!1,teams:["anonymous"]},c.rbac||{},c.requiresLogin||!1)}export{le as getAiDocumentsStore};
