import S from"flexsearch";import{REDOCLY_TEAMS_RBAC as m}from"@redocly/config";import{DISABLE_DEEP_LINK_IF_FIELDS_EXIST as I,HIGHLIGHTED_TEXT_MAX_LENGTH as g,SEARCH_LIMIT as _}from"../../constants.js";class w{_index;_documents=new Map;_documentsCounter=1;id;constructor(t,e){this.id=t,this._index=new S.Document(e)}get documents(){return this._documents.entries()}get documentsCount(){return this._documentsCounter}async export(t){await this._index.export(t)}async import(t){const{documents:e,index:n}=t;this._documents.clear();for(const[s,i]of e)this._documents.set(s,i);for(const[s,i]of Object.entries(n))await this._index.import(s,i)}add(t){t.forEach(e=>{e.tags||(e.tags=[]),this._documents.set(this._documentsCounter,e[m]?e:{...e,[m]:void 0}),this._index.add(this._documentsCounter,e),this._documentsCounter++})}async search(t){const{query:e,offset:n,auth:s,product:i,versions:r}=t,o=[],a=s.teams,h={limit:_,offset:n};let c=new Map,f=await this._index.searchAsync(e,{...h,tag:a});if(i){const l=await this._index.searchAsync(e,{...h,tag:i});f=this.innerJoinSearchResults(f,l)}if(r&&r.length){const l=await this._index.searchAsync(e,{...h,tag:r});f=this.innerJoinSearchResults(f,l)}for(const l of f)for(const u of l.result){const d=c.get(u)?.fields||[];c.set(u,{fields:l.field?[...d,l.field]:[...d]})}let p=0;for(const[l,u]of c.entries()){if(p>=_)break;const d=this._documents.get(l);d&&!this.skipDefaultVersion(d,r)&&(o.push({document:this.prepareDocument(d,e,u.fields),highlight:this.prepareHighlight(d,e,u.fields)}),p++)}return{documents:{[this.id]:o},facets:{}}}prepareDocument(t,e,n){return this.resolveDeepLink(t,e,n)}prepareHighlight(t,e,n){const s={};let i=!1;for(const r of n)if(r==="path")s.path=t.path?t.path.map(o=>this.hightlight(e,o)):[];else if(r.includes("parameters")){if(!i){const o=r.split(":")[1],a=t.parameters?.find(h=>{const c=h[o];return this.someMatchesPredicate(e,typeof c=="boolean"?c.toString():c)});a&&(s.parameters=[{name:this.hightlight(e,a.name),description:this.hightlight(e,a.description),place:this.hightlight(e,a.place),path:a.path?a.path.map(h=>this.hightlight(e,h)):[]}],i=!0)}}else s[r]=this.hightlight(e,t[r]);return s}hightlight(t,e){const s=t.split(/\s+/g).reduce((i,r)=>r?i?.replace?.(new RegExp(`(^|\\s)${r}`,"i"),o=>`<mark>${o}</mark>`):i,e);if(!s)return e||"";if(s.length<g)return s;{const i=s.indexOf("<mark>"),r=s.indexOf("</mark>")+7,o=g/2+t.length/2,a=i<o,h=r+o<s.length,c=s.substring(a?0:i-o,h?r+o:s.length);return`${a?"":"..."}${c}${h?"...":""}`}}someMatchesPredicate=(t,e)=>{if(e){const n=t.split(/\s+/g);return Array.isArray(e)?e.some(s=>n.some(i=>s.toLowerCase().includes(i.toLowerCase()))):n.some(s=>e.toLowerCase().includes(s.toLowerCase()))}else return!1};innerJoinSearchResults(t,e){const n=[];for(const s of t){const i=[];for(const r of e)for(const o of s.result){const a=r.result.find(h=>h===o);a&&i.push(a)}i.length&&n.push({result:i,field:s.field})}return n}skipDefaultVersion(t,e){return!!(e&&e.length&&t.isDefaultVersion&&t.versionFolderId&&t.version&&e.some(n=>n.includes(t.versionFolderId??""))&&!e.some(n=>n.includes(t.version??"")))}resolveDeepLink(t,e,n){for(const i of I)if(n.some(r=>r===i))return t;let s;for(const i of n)if(i.includes("parameters")){const r=i.split(":")[1],o=t.parameters?.find(a=>{const h=a[r];return this.someMatchesPredicate(e,typeof h=="boolean"?h.toString():h)});if(o){s=o.deepLink;break}}if(s){const i=s.split("#")[1];return{...t,url:`${t.url}#${i}`}}return t}}export{w as FlexSearchIndex};
