import*as g from"@redocly/openapi-docs";import{REDOCLY_TEAMS_RBAC as d}from"@redocly/config";import{combineUrls as u}from"@redocly/theme/core/utils";import{SEARCH_DOCUMENT_METADATA_KEY as y}from"../../constants/plugins/search.js";import{removeMarkdownLinks as c,stripFormatting as a}from"./utils.js";import{normalizeFrontmatterKeywords as x}from"../markdown/utils/normalize-frontmatter-keywords.js";const l=g.default||g;class T{#n=[];#i;#s;#e;#r;constructor(e,t,n){this.#i=e,this.#s=t,this.#e=n}addItem(e){const{result:t}=x(e.keywords||e.operationDefinition?.keywords);t?.excludes&&e.type==="section"&&e.id===""&&(this.#r=t.excludes);try{let n;switch(e.type){case"tag":n=this.#c(e);break;case"operation":const o=this.getOperation(e);n=this.#d(o,e[d]);break;case"section":n=this.#p(e);break;case"mcp":n=this.#o(e);break}if(!n)return;const s=[...new Set([...t?.excludes?t.excludes:[],...this.#r?this.#r:[]])];return(t||s.length)&&(n[y]={curated:!0,...t,excludes:s}),this.#n.push(n),n}catch(n){console.error("Cannot add item to search indexer",n.message)}}addInfo(e,t){const n=this.#e,s={id:n,url:n,text:a(c(e.description||"")),title:a(`${e.title} (${e.version})`),metadata:t};return this.#n.push(s),s}#o(e){const t=u(this.#e,e.href);return{id:t,url:t,text:a(c(e.description||"")),title:e.name}}getResult(){return this.#n}getOperation(e){return l.getOperation(this.#i,e.operationDefinition,e.parent,{...this.#s,internal_skipSamples:!0},e.href)}#a(e){return e.name.toString()+e.description+e.place}#d(e,t){if(e.type!=="operation")return;let n={};for(let r of e.parameters){if(r[d])continue;const i=r.schema?.example||r.example,p={name:r.name,description:a(c(r.description)),place:r.in+" parameters",mediaType:void 0,type:r.schema?.type.toString()||"unknown",deepLink:l.generateDeepLink(r),[d]:r[d],required:r.required,example:i?JSON.stringify(i):void 0,enum:r.schema?.enum?.length?r.schema.enum:void 0};n[this.#a(p)]=p}this.#t(n,e.requestBody?.content?.mediaTypes[0]?.schema,e.requestBody?.content?.mediaTypes[0]?.name,"request fields",!1);for(let r of e.responses){const i=`response ${r.code} fields`;this.#t(n,r.content?.mediaTypes[0]?.schema,r.content?.mediaTypes[0]?.name,i,!0)}let s=u(this.#e,e.href);return{id:s,url:s,title:a(e.name),text:a(c(e.description||"")),httpMethod:e.httpVerb,httpPath:e.path,deprecated:e.deprecated,security:e.security.map(r=>r.schemes.map(i=>i.id)).flat().filter(Boolean),parameters:Object.values(n),badges:e.badges.length?e.badges:void 0,[d]:t}}#t(e,t,n,s,o,r=[]){if(!(!t||t.isCircular)){if(t?.fields)for(let i of t.fields){if(t[d]||i.kind==="additionalProperties"||i.schema?.readOnly&&!o||i.schema?.writeOnly&&o)continue;const p=i.schema?.example||i.example,m=i.schema?.enum,h={name:i.name,description:a(i.description),place:s,mediaType:n,path:r,deepLink:l.generateDeepLink(i),type:i.schema?.type.toString()||"unknown",required:i.required||t.schema.required?.includes(i.name)||!1,example:p?JSON.stringify(p):void 0,enum:m?.length?m:void 0},f=this.#a(h);e[f]==null&&(e[f]=h,this.#t(e,i.schema,n,s,o,r.concat([i.name])))}t?.items&&this.#t(e,t.items,n,s,o,r)}}#c(e){const t=u(this.#e,e.href);return{id:t,url:t,text:a(c(e.description||"")),title:a(e.name)}}#p(e){const t=u(this.#e,e.href);return{id:t,url:t,text:a(c(e.description||"")),title:a(e.name)}}}export{T as SearchIndexer};
