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