import{REDOCLY_ROUTE_RBAC as p,REDOCLY_TEAMS_RBAC as l}from"@redocly/config";import{DEFAULT_LOCALE_PLACEHOLDER as b}from"../../../../constants/index.js";import{extractTeamsForSearch as A,getRbacTeamsForSearch as E,logger as u}from"../../../utils/index.js";import{getSearchDocumentGroup as x}from"../utils.js";async function I(D,r,f){u.info("Preparing search documents and create indexes...");const h=u.startTiming(),T=r.getConfig().rbac,v=[b,...D.localeFolders.map(s=>s.toLowerCase())];for(const s of v){const F=r.getAllRoutesForLocale(s),i=new Map;for(const e of F){const{product:a}=e,C=await e.getStaticData?.(e,{...r,contentDir:r.contentDir,parseMarkdoc:(...c)=>r.parseMarkdoc(...c)})||{};let n=await e.getSearchDocuments?.(e,{...C,[l]:e[l],[p]:e[p]},r);const o=e.versions?.find(c=>c.active),L=E(e,T??{});if(n&&n.length)for(const c in n){let t=n[c];const d=A(t?.[l]),g=d?.length?d:L,S=t.tags||[];t={...t,...o&&{version:o.version,isDefaultVersion:o.default,versionFolderId:o.folderId},...a&&{product:a},rbacTeams:g,tags:[...S,...g,...o?o.default?["v:default"]:[`v:${o.folderId}:${o.version}`]:["v:default"],...a?[`p:${a.name}`]:[]],url:t.url&&(t.path&&t.path.length>1?t.url:t.url.split("#")[0])};const m=x(t.facets);if(m){const R=i.get(m)??[];i.set(m,[...R,t])}}}for(const[e,a]of i)await f.addDocuments(e,s,a)}f.cleanupFacetValues(r),u.infoTime(h,"Search indexes created")}export{I as prepareSearchDocuments};
