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

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

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

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

`,r&&(s+=` - ${L({title:c,description:void 0,slug:j(r)})}

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

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

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

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

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

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

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