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