import{basename as S}from"node:path";import{REDOCLY_TEAMS_RBAC as y}from"@redocly/config";import{isResourcePubliclyAccessible as C}from"../../../utils/rbac.js";import{getLlmsTxtMdPathBySlug as A}from"../../../utils/llmstxt/get-llms-txt-md-path-by-slug.js";import{getLocaleFromRelativePath as L}from"../../../fs/utils/get-locale-from-relative-path.js";import{extractDocumentSearchFacets as j}from"./search-facets.js";import{removeMarkdownLinks as m}from"../../openapi-docs/utils.js";import{processSchema as d}from"./schema-processor.js";import{llmsTxtLink as k}from"../../search/llmstxt/index.js";const Z=({actions:n,document:e,metadata:i})=>async(t,s,r)=>{const a=t.getSidebar?.(t)||[],c=await r.getConfig(),{allSearchDocuments:o,publiclyAccessibleSearchDocuments:l}=b(t,a,e,n,i,c);if(o.length)return{async getLLMsTxts(){return l.map(f=>({slug:f.url,fsPath:t.fsPath,title:f.title,content:f.content,includeInLLMsTxt:f.includeInLLMsTxt}))},async getSearchDocuments(){return o}}};function b(n,e,i,t,s,r,a){const c=[],o=[];for(const l of e){if(l.type==="group"){const h=l.apiItemData?.type==="channel"?l:a,{allSearchDocuments:D,publiclyAccessibleSearchDocuments:P}=b(n,l.items||[],i,t,s,r,h);c.push(...D),o.push(...P)}const f=I(n,l,i,t,s,r);f&&(c.push(f),B(l,r,a)&&o.push(f))}return{allSearchDocuments:c,publiclyAccessibleSearchDocuments:o}}function I(n,e,i,t,s,r){const a=F(e,i);if(!a)return null;const c=e.link||"",o=e.httpVerb||"",l=L(n.fsPath),f=j(t,s),h=M(n,e,i,a,o,c,r);return{url:c||n.slug,fsPath:n.fsPath,content:h.trim(),title:m(a.title||"")||S(n.fsPath),locale:l,product:n.product?.name,facets:f,includeInLLMsTxt:!e.apiItemData}}function M(n,e,i,t,s,r,a){let c="";const o=m(t.title||""),l=m(t.description||"")+m(t.summary||"");return o&&(c+=`# ${o}

`),l&&(c+=`${l}

`),s&&(c+=`Method: ${s.toUpperCase()}
`),c+=O(i),c+=v(i),e.type==="link"&&!e.apiItemData&&(c+=T(n,a)),e.apiItemData?.type==="channel"&&(c+=x(e,i,r,n)),e.apiItemData?.type==="operation"&&(c+=N(e,i)),c}function O(n){let e="";return n.info?.version&&(e+=`Version: ${n.info.version}
`),n.info?.license?.name&&(e+=`License: ${n.info.license.name}
`),e}function v(n){if(!n.servers||Object.keys(n.servers).length===0)return"";let e=`
## Servers

`;for(const[i,t]of Object.entries(n.servers))e+=`### ${i}
`,t.description&&(e+=`${t.description}
`),e+=`Host: ${t.host}
`,e+=`Protocol: ${t.protocol}
`,t.protocolVersion&&(e+=`Protocol Version: ${t.protocolVersion}
`),t.pathname&&(e+=`Path: ${t.pathname}
`),e+=`
`;return e}function T(n,e){const i=n.getSidebar?.(n)||[],t=$(i,e);if(Object.keys(t).length===0)return"";let s="";for(const[,r]of Object.entries(t))if(r.length>0)for(const a of r){const c=a.label||"Untitled",o=a.link||a.routeSlug||"";s+=`#### ${c}

`,o&&(s+=` - ${k({title:c,description:void 0,slug:A(o)})}

`)}return s}function x(n,e,i,t){const s=n.apiItemData?.id;if(!s)return"";const r=e.channels?.[s];if(!r)return"";let a="";if(r.parameters&&Object.keys(r.parameters).length>0){a+=`## Parameters

`;for(const[c,o]of Object.entries(r.parameters))a+=`### ${c}
`,typeof o=="object"&&o!==null&&"description"in o&&o.description&&(a+=`${o.description}
`),typeof o=="object"&&o!==null&&"enum"in o&&Array.isArray(o.enum)&&o.enum.length>0&&(a+=`Enum: ${o.enum.join(", ")}
`),typeof o=="object"&&o!==null&&"default"in o&&o.default&&(a+=`Default: ${o.default}
`),a+=`
`}if(r.messages&&Object.keys(r.messages).length>0){a+=`## Messages

`;for(const[c,o]of Object.entries(r.messages))a+=R(o,c,i,t)}return a}function R(n,e,i,t){let r=`### ${n.title||n.name||e}
`;if(n.description&&(r+=`${n.description}
`),n.summary&&(r+=`${n.summary}
`),n.payload){const a=d(n.payload,"message payload",i.replace(t.slug+"/","").replace(/^\//,""),e);a.length>0&&(r+=`
#### Payload Schema

`,r+=g(a))}if(n.headers){const a=d(n.headers,"message headers",i.replace(t.slug+"/","").replace(/^\//,""),e);a.length>0&&(r+=`
#### Headers Schema

`,r+=g(a))}return r}function g(n){let e="";for(const i of n)if(typeof i=="object"&&i!==null&&"name"in i&&"type"in i){const t=i;e+=`- **${t.name}** (${t.type}${t.required?", required":""})
`,t.description&&(e+=`  ${t.description}
`),t.enum&&Array.isArray(t.enum)&&t.enum.length>0&&(e+=`  Enum: ${t.enum.join(", ")}
`),e+=`
`}return e}function N(n,e){const i=n.apiItemData?.id;if(!i)return"";const t=e.operations?.[i];if(!t)return"";let s=`## Operation Details

`;if(t.action&&(s+=`Action: ${t.action}
`),t.description&&(s+=`${t.description}
`),t.summary&&(s+=`${t.summary}
`),t.messages&&t.messages.length>0){s+=`
### Messages

`;for(const r of t.messages){const a=r.title||r.name||"Message";s+=`#### ${a}
`,r.description&&(s+=`${r.description}
`),r.summary&&(s+=`${r.summary}
`),s+=`
`}}return s}function $(n,e){const i={};for(const t of n)t.type==="group"?t.apiItemData?.type==="channel"?q(t,i,e):V(t,i,e):t.apiItemData?.type==="channel"&&E(t,i,e);return i}function q(n,e,i){const t=n.label||"Channel";if(p(u(n),i)&&(e[t]||(e[t]=[]),e[t].push(n),n.items))for(const s of n.items)s.apiItemData?.type==="operation"&&p(u(s),i)&&e[t].push(s)}function V(n,e,i){if(n.items){const t=$(n.items,i);for(const[s,r]of Object.entries(t))e[s]||(e[s]=[]),e[s].push(...r)}}function E(n,e,i){const t=n.label||"Channel";if(p(u(n),i)&&(e[t]||(e[t]=[]),e[t].push(n),n.items))for(const s of n.items)s.apiItemData?.type==="operation"&&p(u(s),i)&&e[t].push(s)}function F(n,e){const{apiItemData:i,type:t}=n;return i?.type?w(i,e):t==="link"?_(e):null}function w(n,e){switch(n.type){case"channel":return e.channels?.[n.id]||null;case"operation":return e.operations?.[n.id]||null;default:return null}}function _(n){return{description:n.info?.description||"",title:n.info?.title||"",summary:""}}function B(n,e,i){const t=p(u(n),e);if(n.apiItemData?.type==="operation"&&i){const s=p(u(i),e);return t&&s}return t}function u(n){return{slug:n.link||"",[y]:n[y]}}function p(n,e){return e.access?.requiresLogin?!0:C(n,e)}export{Z as getAiDocumentsStore};
