import{readFile as w,readdir as y,appendFile as E}from"node:fs/promises";import m from"node:path";import{existsSync as F}from"node:fs";import{SEARCH_GROUP_FACET_FIELD as I,SEARCH_PRODUCT_FIELD as C,SEARCH_VERSION_FIELD as $}from"../../../../../constants/common.js";import{SEARCH_INDEX_FIELDS as A,SEARCH_DATA_EXPORT_FOLDER as _,SEARCH_MAX_INMEMORY_DOCUMENTS_COUNT as O}from"../../../../constants/plugins/search.js";import{telemetryTraceStep as v}from"../../../../telemetry/helpers/trace-step.js";import{envConfig as N}from"../../../../config/env-config.js";import{ensureDir as h}from"../../../../utils/index.js";import{FlexSearchIndex as j}from"./search-index.js";class J{#e=new Map;#s;#t=0;async initIndexSchema(e){this.#s={document:{id:"id",tag:"tags",index:A},worker:!1,tokenize:"forward",context:{depth:2,resolution:9}}}#n(e,s){const n=this.#e.get(e);if(n)return n.find(o=>o.id===s)}#o(e,s){let n=this.#n(e,s);return n||(n=new j(s,this.#s),this.#e.set(e,[...this.#e.get(e)??[],n])),n}async addDocuments(e,s){if(e.length){const{group:n,locale:o,outDir:a}=s,i=this.#o(o,n);for(const c of e)i.add(c),N.isBuildMode&&(this.#t++,this.#t>=O&&await this.exportDocuments(a))}}getIndexesSize(){return this.#e.size}async search(e,s,n){return await v("search",async o=>{const{query:a,locale:i,filter:c,loadMore:r}=e,l=this.#e.get(i)??[];let f=[],d="",x=["v:default"];if(o?.setAttribute("locale",i),c)for(const t of c)t.field===I?f=[...f,...t.values]:t.field===C?d=`p:${t.values[0]}`:t.field===$&&t.values.length&&t.values.length===2&&(x=[...x,`v:${t.values[0]}:${t.values[1]}`]);const g={auth:s,query:a,offset:0,SEARCH_GROUP_FACET_FIELD:I,product:d,versions:x},p=[];let u={facets:{},documents:{}};if(r){const t=this.#n(i,r.groupKey);t&&p.push(t.search({...g,offset:r.offset}))}else for(const t of l)f&&f.length?f.includes(t.id)&&p.push(t.search(g)):p.push(t.search(g));const R=await Promise.all(p);for(const t of R){u.documents={...u.documents,...t.documents};for(const[D,S]of Object.entries(t.facets))u.facets[D]?u.facets[D]=[...u.facets[D],...S]:u.facets[D]=S}return u})}async exportDocuments(e){const s='{"documents":[',n=h(m.join(e,_));for(const[o,a]of this.#e){const i=h(m.join(n,o));for(const c of a){const r=Array.from(c.documents);if(r.length===0)continue;const l=h(m.join(i,`${c.id}.json`)),f=!F(l),d=JSON.stringify(r).substring(1).slice(0,-1),x=f?s+d:","+d;await E(l,x,{encoding:"utf8"}),c.clearDocuments()}}this.#t=0}async exportIndexes(e){const s=h(m.join(e,_));for(const[n,o]of this.#e){const a=h(m.join(s,n));for(const i of o){const c=h(m.join(a,`${i.id}.json`)),r={};await i.export((f,d)=>{r[f]=d});const l=`],"index":${JSON.stringify(r)}}`;await E(c,l,{encoding:"utf8"})}}this.#e.clear()}async import(e){const s=`${e}/${_}`;if(!F(s))return;const n=await y(s);for(const o of n){const a=await y(`${s}/${o}`);for(const i of a){const c=`${s}/${o}/${i}`,r=JSON.parse(await w(c,"utf-8")),l=m.parse(i).name;await this.#o(o,l).import(r)}}}countFacets(e,s,n){const o={};for(const[a,i]of n)o[a]=i.values.map(c=>({value:c,count:0,isCounterVisible:!1}));return o}cleanupFacetValues(e){}}export{J as FlexSearch};
