@devisfuture/mega-collection 2.4.3 → 2.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,1010 @@
1
- import {a,b as b$1}from'../chunk-MB56OSRQ.mjs';var k="deferSearchMutationIndexUpdates";function O(f){for(let e=0;e<f.length-1;e++){let t=e;for(let s=e+1;s<f.length;s++)f[s].size<f[t].size&&(t=s);if(t!==e){let s=f[e];f[e]=f[t],f[t]=s;}}}function W(f,e){let t=[];for(let s of e){let i=f.get(s);if(!i)return null;t.push(i);}return O(t),t}function D(f,e){for(let t=0;t<e.length;t++)if(!e[t].has(f))return false;return true}function V(f,e,t,s){let i=W(f,e);if(i===null)return null;let n=i[0];return {smallestPostingList:n,matches:r=>n.has(r)&&D(r,i)&&!!t[r]?.includes(s)}}function U(f){let e=Math.min(3,Math.max(2,f.length)),t=f.length-e+1,s=new Array(t);for(let i=0;i<t;i++)s[i]=f.substring(i,i+e);return s}function G(f,e){let t=f.get(e);if(t)return t;let s=new Set;return f.set(e,s),s}function Q(f){let e=U(f);if(e.length<=12)return new Set(e);let t=new Set,s=e.length-1,i=11;for(let n=0;n<=i;n++){let r=Math.round(n*s/i);t.add(e[r]);}return t}function b(f,e,t){let s=Math.min(3,e.length);for(let i=2;i<=s;i++){let n=e.length-i;for(let r=0;r<=n;r++){let l=e.substring(r,r+i);G(f,l).add(t);}}}function R(f,e,t,s,i={}){let{restrictionLookup:n=null,take:r=Number.POSITIVE_INFINITY}=i,l=V(f,e,t,s);if(l===null)return [];let a=[];for(let o of l.smallestPostingList)if(!(n!==null&&!n[o])&&l.matches(o)&&(a.push(o),a.length>=r))break;return a}function T(f,e,t,s,i){let{candidateIndices:n,restrictionLookup:r=null,take:l=Number.POSITIVE_INFINITY}=i;if(n.length===0)return [];let a=V(f,e,t,s);if(a===null)return [];let o=[];if(r===null||n.length<=a.smallestPostingList.size){for(let c=0;c<n.length;c++){let d=n[c];if(a.matches(d)&&(o.push(d),o.length>=l))break}return o}for(let c of a.smallestPostingList)if(r[c]&&a.matches(c)&&(o.push(c),o.length>=l))break;return o}function S(f,e,t){let s=Math.min(3,e.length);for(let i=2;i<=s;i++){let n=e.length-i;for(let r=0;r<=n;r++){let l=e.substring(r,r+i),a=f.get(l);a&&(a.delete(t),a.size===0&&f.delete(l));}}}var z=class{constructor(e={ngramIndexes:new Map,normalizedFieldValues:new Map}){this.storage=e;this.registeredFields=new Set;this.fieldDescriptors=new Map;}registerFields(e){if(e?.length)for(let t of e)this.registerField(t);}hasRegisteredFields(){return this.registeredFields.size>0}hasField(e){return this.registeredFields.has(e)}hasIndexes(){return this.storage.ngramIndexes.size>0}clearIndexes(){this.storage.ngramIndexes.clear(),this.storage.normalizedFieldValues.clear();}buildIndexes(e){this.clearIndexes();for(let t of this.registeredFields)this.buildIndex(e,t);}addItems(e,t){if(!(e.length===0||this.storage.ngramIndexes.size===0))for(let s of this.storage.ngramIndexes.keys())this.addItemsToField(s,e,t);}updateItem(e,t,s){if(this.storage.ngramIndexes.size!==0)for(let i of this.storage.ngramIndexes.keys())this.updateItemInField(i,e,t,s);}removeItem(e,t){if(this.storage.ngramIndexes.size!==0)for(let s of this.storage.ngramIndexes.keys())this.removeItemFromField(s,e,t);}moveItem(e,t,s){if(!(this.storage.ngramIndexes.size===0||t===s))for(let i of this.storage.ngramIndexes.keys())this.moveItemForField(i,e,t,s);}searchAllIndexedFieldIndices(e,t,s,i){let n=new Set,r=[];for(let l of this.storage.ngramIndexes.keys())for(let a of this.searchIndexedFieldIndices(l,e,t,s,i))n.has(a)||(n.add(a),r.push(a));return r}searchIndexedField(e,t,s,i,n,r,l=Number.POSITIVE_INFINITY){let a=this.searchIndexedFieldIndices(t,s,i,n,r,l),o=[];for(let u=0;u<a.length;u++){let c=e[a[u]];c&&o.push(c);}return o}searchIndexedFieldIndices(e,t,s,i,n,r=Number.POSITIVE_INFINITY){let l=this.storage.ngramIndexes.get(e);if(!l)return [];let a=this.storage.normalizedFieldValues.get(e)??[];return n!=null?T(l,s,a,t,{candidateIndices:n,restrictionLookup:i,take:r}):R(l,s,a,t,{restrictionLookup:i,take:r})}searchFieldLinear(e,t,s){let i=this.searchFieldLinearIndices(e,t,s),n=[];for(let r=0;r<i.length;r++){let l=e[i[r]];l&&n.push(l);}return n}searchFieldLinearIndices(e,t,s,i){let n=this.fieldDescriptors.get(t);if(!n)return [];let{collectionKey:r,nestedKey:l}=n,a=[];if(i){for(let o=0;o<i.length;o++){let u=i[o],d=e[u][r];if(!Array.isArray(d))continue;let h=false;for(let g=0;g<d.length;g++){let m=d[g][l];if(typeof m=="string"&&m.toLowerCase().includes(s)){h=true;break}}h&&a.push(u);}return a}for(let o=0;o<e.length;o++){let c=e[o][r];if(!Array.isArray(c))continue;let d=false;for(let h=0;h<c.length;h++){let g=c[h][l];if(typeof g=="string"&&g.toLowerCase().includes(s)){d=true;break}}d&&a.push(o);}return a}matchesAnyField(e,t){for(let s of this.registeredFields){let i=this.fieldDescriptors.get(s);if(!i)continue;let{collectionKey:n,nestedKey:r}=i,l=e[n];if(Array.isArray(l))for(let a=0;a<l.length;a++){let o=l[a][r];if(typeof o=="string"&&o.toLowerCase().includes(t))return true}}return false}registerField(e){let t=this.createDescriptor(e);t&&(this.registeredFields.add(e),this.fieldDescriptors.set(e,t));}createDescriptor(e){let t=e.indexOf(".");return t===-1?null:{collectionKey:e.substring(0,t),nestedKey:e.substring(t+1)}}getNormalizedValues(e){let t=this.storage.normalizedFieldValues.get(e);if(t)return t;let s=[];return this.storage.normalizedFieldValues.set(e,s),s}buildIndex(e,t){let s=this.fieldDescriptors.get(t);if(!s)return;let{collectionKey:i,nestedKey:n}=s,r=new Map,l=new Array(e.length);for(let a=0,o=e.length;a<o;a++){let c=e[a][i];if(!Array.isArray(c))continue;let d=[];for(let g=0;g<c.length;g++){let m=c[g][n];typeof m=="string"&&d.push(m.toLowerCase());}if(d.length===0)continue;let h=d.join(`
2
- `);l[a]=h,b(r,h,a);}this.storage.ngramIndexes.set(t,r),this.storage.normalizedFieldValues.set(t,l);}addItemsToField(e,t,s){let i=this.fieldDescriptors.get(e),n=this.storage.ngramIndexes.get(e);if(!i||!n)return;let r=this.getNormalizedValues(e),{collectionKey:l,nestedKey:a}=i;for(let o=0;o<t.length;o++){let c=t[o][l];if(!Array.isArray(c))continue;let d=[],h=s+o;for(let m=0;m<c.length;m++){let p=c[m][a];typeof p=="string"&&d.push(p.toLowerCase());}if(d.length===0)continue;let g=d.join(`
3
- `);r[h]=g,b(n,g,h);}}updateItemInField(e,t,s,i){let n=this.storage.ngramIndexes.get(e);if(!n)return;let r=this.getNormalizedValues(e),l=this.getNormalizedItemValue(e,s),a=this.getNormalizedItemValue(e,t);if(l!==a){if(l&&S(n,l,i),!a){delete r[i];return}r[i]=a,b(n,a,i);}}removeItemFromField(e,t,s){let i=this.storage.ngramIndexes.get(e);if(!i)return;let n=this.getNormalizedValues(e),r=n[s]??this.getNormalizedItemValue(e,t);r&&S(i,r,s),delete n[s];}moveItemForField(e,t,s,i){let n=this.storage.ngramIndexes.get(e);if(!n)return;let r=this.getNormalizedValues(e),l=r[s]??this.getNormalizedItemValue(e,t);if(!l){delete r[s];return}S(n,l,s),b(n,l,i),r[i]=l,delete r[s];}getNormalizedItemValue(e,t){let s=this.fieldDescriptors.get(e);if(!s)return null;let{collectionKey:i,nestedKey:n}=s,r=t[i];if(!Array.isArray(r))return null;let l=[];for(let a=0;a<r.length;a++){let o=r[a][n];typeof o=="string"&&l.push(o.toLowerCase());}return l.length===0?null:l.join(`
4
- `)}};var P=()=>({indexedFields:new Set,flatIndexes:new Map,nestedStorage:{ngramIndexes:new Map,normalizedFieldValues:new Map},deferredMutationVersion:null,filterByPreviousResult:false,previousResultIndices:null,previousResultLookup:null,previousQuery:null,stats:{totalQueries:0,indexedQueries:0,fallbackQueries:0,fallbackFields:new Map}});var L=class{constructor(e={}){this.cachedIndexedFieldsList=null;this.cachedLinearSearchFieldsList=null;this.emittedWarningKeys=new Set;this.warnings=[];this.normalizedValuesCache=new Map;this.combinedNormalizedValuesCache=null;this.minQueryLength=e.minQueryLength??1,this.silent=e.silent??false,this.state=e.state??new a(e.data??[]),this.namespace=this.state.createNamespace("search"),this.nestedCollection=new z(this.runtime.nestedStorage),this.nestedCollection.registerFields(e.nestedFields),this.state.subscribe(i=>this.handleStateMutation(i)),e.filterByPreviousResult&&(this.runtime.filterByPreviousResult=true);let t=e.fields?.length,s=this.nestedCollection.hasRegisteredFields();if(t)for(let i of e.fields)this.indexedFields.add(i);this.dataset.length>0&&(t||s)&&this.rebuildConfiguredIndexes();}get dataset(){return this.state.getOriginData()}get runtime(){return this.state.getOrCreateScopedValue(this.namespace,"runtime",P)}get flatIndexes(){return this.runtime.flatIndexes}get indexedFields(){return this.runtime.indexedFields}shouldDeferMutationIndexUpdates(){return this.state.getScopedValue(b$1,k)===true}markDeferredMutationState(){this.runtime.deferredMutationVersion=this.state.getMutationVersion(),this.flatIndexes.clear(),this.nestedCollection.clearIndexes(),this.cachedIndexedFieldsList=null,this.cachedLinearSearchFieldsList=null,this.normalizedValuesCache.clear(),this.combinedNormalizedValuesCache=null,this.clearPreviousSearchState();}ensureConfiguredIndexesReady(){this.runtime.deferredMutationVersion!==null&&(this.runtime.deferredMutationVersion=null,this.dataset.length>0&&(this.indexedFields.size>0||this.nestedCollection.hasRegisteredFields())&&this.rebuildConfiguredIndexes());}rebuildConfiguredIndexes(){this.flatIndexes.clear(),this.nestedCollection.clearIndexes(),this.cachedIndexedFieldsList=null,this.cachedLinearSearchFieldsList=null,this.normalizedValuesCache.clear(),this.combinedNormalizedValuesCache=null;for(let e of this.indexedFields)this.buildIndexFromData(this.dataset,e);this.nestedCollection.hasRegisteredFields()&&this.nestedCollection.buildIndexes(this.dataset);}buildIndexFromData(e,t){let s=this.state.getMutationVersion(),i=new Map,n=new Array(e.length);for(let r=0,l=e.length;r<l;r++){let a=e[r][t];if(typeof a!="string")continue;let o=a.toLowerCase();n[r]=o,b(i,o,r);}this.flatIndexes.set(t,{ngramMap:i,normalizedValues:n,version:s});}search(e,t,s){return typeof t=="string"?this.searchField(e,t,s):this.searchAll(e,t)}searchAll(e,t){return this.searchAllFields(e,t)}normalizeQuery(e){return e.trim().toLowerCase()}normalizeSearchWindow(e){let t=Math.max(0,Math.trunc(e?.offset??0)),s=e?.limit,i=s===void 0?Number.POSITIVE_INFINITY:Math.max(0,Math.trunc(s)),n=Number.isFinite(i)?t+i:Number.POSITIVE_INFINITY;return {offset:t,limit:i,take:n,hasWindow:t>0||Number.isFinite(i)}}shouldTrackPreviousResult(e){return !e.hasWindow}hasReachedWindowLimit(e,t){return Number.isFinite(e.limit)&&t>=e.limit}sliceItems(e,t){return t.hasWindow?e.slice(t.offset,t.take):e}collectItemsFromIndices(e,t){let s=t.hasWindow&&e.length>0?e.slice(t.offset,t.take):e,i=[];for(let n=0;n<s.length;n++){let r=this.dataset[s[n]];r&&i.push(r);}return {items:i,indices:s}}createLookup(e){let t=new Uint8Array(this.dataset.length);for(let s=0;s<e.length;s++)t[e[s]]=1;return t}getRestrictionLookup(e){let t=this.runtime.previousResultLookup;if(t!==null&&this.runtime.previousResultIndices===e&&t.length===this.dataset.length)return t;let s=this.createLookup(e);return this.runtime.previousResultLookup=s,s}getSearchSource(e){let{runtime:t}=this;return t.filterByPreviousResult?t.previousQuery!==null&&t.previousResultIndices!==null&&e.includes(t.previousQuery)?{indices:t.previousResultIndices,lookup:this.getRestrictionLookup(t.previousResultIndices)}:(t.previousResultIndices=null,t.previousResultLookup=null,t.previousQuery=null,{indices:null,lookup:null}):{indices:null,lookup:null}}saveSearchResult(e,t){let{runtime:s}=this;s.filterByPreviousResult&&(s.previousResultIndices=e,s.previousResultLookup=null,s.previousQuery=t);}persistSearchResult(e,t,s){s&&this.saveSearchResult(e.indices,t);}resetSearchState(){return this.clearPreviousSearchState(),this}getWarnings(){return [...this.warnings]}getStats(){let{totalQueries:e,indexedQueries:t,fallbackQueries:s,fallbackFields:i}=this.runtime.stats;return {totalQueries:e,indexedQueries:t,fallbackQueries:s,fallbackRate:e===0?0:s/e,fallbackFields:Object.fromEntries(i)}}resetStats(){let{stats:e}=this.runtime;return e.totalQueries=0,e.indexedQueries=0,e.fallbackQueries=0,e.fallbackFields.clear(),this}clearPreviousSearchState(){let{runtime:e}=this;e.previousResultIndices=null,e.previousResultLookup=null,e.previousQuery=null;}recordIndexedQuery(){let{stats:e}=this.runtime;e.totalQueries+=1,e.indexedQueries+=1;}recordFallbackQuery(e){let{stats:t}=this.runtime;t.totalQueries+=1,t.fallbackQueries+=1,t.fallbackFields.set(e,(t.fallbackFields.get(e)??0)+1);}shouldEmitFallbackWarning(e){return !this.silent&&e.length>=2}warnAboutFallback(e,t,s){if(!this.shouldEmitFallbackWarning(t))return;let i=`${e}\0${t}\0${s}`;if(this.emittedWarningKeys.has(i))return;this.emittedWarningKeys.add(i);let n=`[TextSearchEngine] warn: query "${t}" on ${e} used linear fallback. ${s}. Add the field(s) to the index schema to enable indexed search.`;this.warnings.push(n),typeof process<"u"&&process.env.NODE_ENV!=="production"&&process.env.NODE_ENV!=="test"&&console.warn(n);}getResolvedFlatIndex(e){let t=this.state.getMutationVersion(),s=this.flatIndexes.get(e);return s&&s.version!==t&&this.dataset.length>0&&(this.buildIndexFromData(this.dataset,e),s=this.flatIndexes.get(e)),s}getQueryGrams(e){let t=Q(e);return t.size>0?t:null}searchAllFields(e,t){let s=this.normalizeQuery(e),i=this.normalizeSearchWindow(t),n="all fields";if(i.limit===0)return [];if(!s||s.length<this.minQueryLength)return this.sliceItems(this.dataset,i);s.length>=2&&this.ensureConfiguredIndexesReady();let r=this.shouldTrackPreviousResult(i),{indices:l,lookup:a}=this.getSearchSource(s),o,u=()=>(o!==void 0||(o=s.length>=2?this.getQueryGrams(s):null),o);if(l!==null){let h=u();if(h!==null&&(this.flatIndexes.size>0||this.nestedCollection.hasIndexes())){this.recordIndexedQuery();let m=this.searchAllFieldsIndexed(s,h,i,a,l);return this.persistSearchResult(m,s,r),m.items}this.recordFallbackQuery(n);let g=this.searchLinearAllFields(this.dataset,s,l,i);return this.persistSearchResult(g,s,r),g.items}if(!this.flatIndexes.size&&!this.nestedCollection.hasIndexes()){this.warnAboutFallback(n,s,this.indexedFields.size>0?"configured indexes are not currently built":"no indexed fields are configured"),this.recordFallbackQuery(n);let h=this.searchLinearAllFields(this.dataset,s,null,i);return this.persistSearchResult(h,s,r),h.items}let c=u();if(c===null){this.recordFallbackQuery(n);let h=this.searchLinearAllFields(this.dataset,s,null,i);return this.persistSearchResult(h,s,r),h.items}this.recordIndexedQuery();let d=this.searchAllFieldsIndexed(s,c,i,null);return this.persistSearchResult(d,s,r),d.items}searchAllFieldsIndexed(e,t,s,i,n=null){if(n!==null&&!this.nestedCollection.hasRegisteredFields()&&this.flatIndexes.size>0)return this.searchAllFieldsIndexedInCandidates(e,t,s,i,n);let r=this.dataset,l=new Uint8Array(r.length),a=[],o=0;for(let u of this.flatIndexes.keys()){let c=this.searchFieldWithPreparedQueryIndices(u,e,t,i,n);for(let d=0;d<c.length;d++){let h=c[d];if(!l[h]){if(l[h]=1,o<s.offset){o+=1;continue}if(a.push(h),o+=1,this.hasReachedWindowLimit(s,a.length))return this.materializeIndicesResult(a)}}}for(let u of this.nestedCollection.searchAllIndexedFieldIndices(e,t,i,n))if(!l[u]){if(l[u]=1,o<s.offset){o+=1;continue}if(a.push(u),o+=1,this.hasReachedWindowLimit(s,a.length))return this.materializeIndicesResult(a)}return this.materializeIndicesResult(a)}searchAllFieldsIndexedInCandidates(e,t,s,i,n){let r=[];for(let o of this.flatIndexes.keys()){let u=this.getResolvedFlatIndex(o);if(!u)continue;let c=V(u.ngramMap,t,u.normalizedValues,e);c!==null&&r.push(c.matches);}if(r.length===0)return {items:[],indices:[]};let l=[],a=0;for(let o=0;o<n.length;o++){let u=n[o];if(i!==null&&!i[u])continue;let c=false;for(let d=0;d<r.length;d++)if(r[d](u)){c=true;break}if(c){if(a<s.offset){a+=1;continue}if(l.push(u),a+=1,this.hasReachedWindowLimit(s,l.length))break}}return this.materializeIndicesResult(l)}searchField(e,t,s){let i=this.normalizeQuery(t),n=this.normalizeSearchWindow(s),r=`field "${e}"`;if(n.limit===0)return [];if(!i||i.length<this.minQueryLength)return this.sliceItems(this.dataset,n);i.length>=2&&this.ensureConfiguredIndexesReady();let l=this.shouldTrackPreviousResult(n),{indices:a,lookup:o}=this.getSearchSource(i),u=this.nestedCollection.hasField(e),c,d=()=>(c!==void 0||(c=i.length>=2?this.getQueryGrams(i):null),c);if(a!==null){let m=d();if(u){if(m!==null&&this.nestedCollection.hasIndexes()){this.recordIndexedQuery();let x=this.nestedCollection.searchIndexedFieldIndices(e,i,m,o,a,n.take),y=this.collectItemsFromIndices(x,n);return this.persistSearchResult(y,i,l),y.items}this.recordFallbackQuery(r);let I=this.searchLinearSingleField(this.dataset,e,i,a,n);return this.persistSearchResult(I,i,l),I.items}if(m!==null&&this.flatIndexes.has(e)){this.recordIndexedQuery();let I=this.searchFieldWithPreparedQuery(e,i,m,n,o,a);return this.persistSearchResult(I,i,l),I.items}this.recordFallbackQuery(r);let p=this.searchLinearSingleField(this.dataset,e,i,a,n);return this.persistSearchResult(p,i,l),p.items}if(u){if(i.length>=2&&this.nestedCollection.hasIndexes()){let p=this.getQueryGrams(i);if(p===null)return [];this.recordIndexedQuery();let I=this.nestedCollection.searchIndexedFieldIndices(e,i,p,null,null,n.take),x=this.collectItemsFromIndices(I,n);return this.persistSearchResult(x,i,l),x.items}this.recordFallbackQuery(r);let m=this.searchLinearSingleField(this.dataset,e,i,null,n);return this.persistSearchResult(m,i,l),m.items}this.flatIndexes.size||this.warnAboutFallback(r,i,this.indexedFields.size>0?`field "${e}" is not backed by an active index`:"no indexed fields are configured");let h=d();if(this.flatIndexes.size>0&&h!==null){if(!this.flatIndexes.has(e))return [];this.recordIndexedQuery();let m=this.searchFieldWithPreparedQuery(e,i,h,n);return this.persistSearchResult(m,i,l),m.items}this.recordFallbackQuery(r);let g=this.searchLinearSingleField(this.dataset,e,i,null,n);return this.persistSearchResult(g,i,l),g.items}searchFieldWithPreparedQuery(e,t,s,i,n=null,r=null){let l=this.searchFieldWithPreparedQueryIndices(e,t,s,n,r,i.take);return this.collectItemsFromIndices(l,i)}searchFieldWithPreparedQueryIndices(e,t,s,i=null,n=null,r=Number.POSITIVE_INFINITY){let l=this.getResolvedFlatIndex(e);if(!l)return [];let{ngramMap:a,normalizedValues:o}=l;return n!==null?T(a,s,o,t,{candidateIndices:n,restrictionLookup:i,take:r}):R(a,s,o,t,{restrictionLookup:i,take:r})}getIndexedFieldsList(){return this.cachedIndexedFieldsList===null&&(this.cachedIndexedFieldsList=Array.from(this.indexedFields)),this.cachedIndexedFieldsList}materializeIndicesResult(e){let t=[];for(let s=0;s<e.length;s++){let i=this.dataset[e[s]];i&&t.push(i);}return {items:t,indices:e}}getLinearSearchFields(e){let t=this.getIndexedFieldsList();return t.length>0?t:this.cachedLinearSearchFieldsList!==null?this.cachedLinearSearchFieldsList:(this.cachedLinearSearchFieldsList=e.length?Object.keys(e[0]).filter(s=>typeof e[0][s]=="string"):[],this.cachedLinearSearchFieldsList)}buildNormalizedValuesOnly(e,t){let s=new Array(e.length);for(let i=0,n=e.length;i<n;i++){let r=e[i][t];typeof r=="string"&&(s[i]=r.toLowerCase());}return this.normalizedValuesCache.set(t,s),s}buildCombinedNormalizedValues(e,t){let s=new Array(e.length);for(let i=0;i<e.length;i++)s[i]=this.buildCombinedNormalizedValue(e[i],t);return this.combinedNormalizedValuesCache={fieldsKey:t.join("\0"),values:s},s}buildCombinedNormalizedValue(e,t){let s="";for(let i=0;i<t.length;i++){let n=e[t[i]];typeof n=="string"&&(s&&(s+=`
5
- `),s+=n.toLowerCase());}return s}getCombinedNormalizedValues(e,t){if(e!==this.dataset)return null;let s=t.join("\0");return this.combinedNormalizedValuesCache?.fieldsKey===s?this.combinedNormalizedValuesCache.values:this.buildCombinedNormalizedValues(e,t)}invalidateNormalizedValuesCacheEntry(e,t){for(let[s,i]of this.normalizedValuesCache){let n=t[s];i[e]=typeof n=="string"?n.toLowerCase():"";}if(this.combinedNormalizedValuesCache!==null){let s=this.combinedNormalizedValuesCache.fieldsKey.split("\0");this.combinedNormalizedValuesCache.values[e]=this.buildCombinedNormalizedValue(t,s);}}searchLinearAllFields(e,t,s,i){if(!e.length)return {items:[],indices:[]};let n=this.getLinearSearchFields(e),r=e===this.dataset,l=n.length,a=new Array(l);for(let g=0;g<l;g++){let m=n[g],p=this.flatIndexes.get(m);if(p)a[g]=p.normalizedValues;else {let I=this.normalizedValuesCache.get(m);I?a[g]=I:r?a[g]=this.buildNormalizedValuesOnly(e,m):a[g]=null;}}let o=this.nestedCollection.hasRegisteredFields(),u=o?null:this.getCombinedNormalizedValues(e,n),c=!o&&a.every(g=>g!==null),d=[],h=0;if(s!==null){let g=this.dataset;if(u!==null){for(let m=0;m<s.length;m++){let p=s[m];if(u[p]?.includes(t)){if(h<i.offset){h+=1;continue}if(d.push(p),h+=1,this.hasReachedWindowLimit(i,d.length))break}}return this.materializeIndicesResult(d)}if(c){for(let m=0;m<s.length;m++){let p=s[m],I=false;for(let x=0;x<l;x++){let y=a[x][p];if(y&&y.includes(t)){I=true;break}}if(I){if(h<i.offset){h+=1;continue}if(d.push(p),h+=1,this.hasReachedWindowLimit(i,d.length))break}}return this.materializeIndicesResult(d)}for(let m=0;m<s.length;m++){let p=s[m],I=g[p],x=false;for(let y=0;y<l;y++){let C=a[y];if(C){let F=C[p];if(F&&F.includes(t)){x=true;break}}else {let F=I[n[y]];if(typeof F=="string"&&F.toLowerCase().includes(t)){x=true;break}}}if(!x&&o&&(x=this.nestedCollection.matchesAnyField(I,t)),!!x){if(h<i.offset){h+=1;continue}if(d.push(p),h+=1,this.hasReachedWindowLimit(i,d.length))break}}return this.materializeIndicesResult(d)}if(u!==null){for(let g=0;g<e.length;g++)if(u[g]?.includes(t)){if(h<i.offset){h+=1;continue}if(d.push(g),h+=1,this.hasReachedWindowLimit(i,d.length))break}return this.materializeIndicesResult(d)}if(c){for(let g=0;g<e.length;g++){let m=false;for(let p=0;p<l;p++){let I=a[p][g];if(I&&I.includes(t)){m=true;break}}if(m){if(h<i.offset){h+=1;continue}if(d.push(g),h+=1,this.hasReachedWindowLimit(i,d.length))break}}return this.materializeIndicesResult(d)}for(let g=0;g<e.length;g++){let m=e[g],p=false;for(let I=0;I<l;I++){let x=a[I];if(x){let y=x[g];if(y&&y.includes(t)){p=true;break}}else {let y=m[n[I]];if(typeof y=="string"&&y.toLowerCase().includes(t)){p=true;break}}}if(!p&&o&&(p=this.nestedCollection.matchesAnyField(m,t)),!!p){if(h<i.offset){h+=1;continue}if(d.push(g),h+=1,this.hasReachedWindowLimit(i,d.length))break}}return this.materializeIndicesResult(d)}searchLinearSingleField(e,t,s,i,n){if(!e.length)return {items:[],indices:[]};if(this.nestedCollection.hasField(t)){let u=this.nestedCollection.searchFieldLinearIndices(e,t,s,i??void 0);return this.collectItemsFromIndices(u,n)}let r=[],l=this.flatIndexes.get(t),a=l?l.normalizedValues:this.normalizedValuesCache.get(t)??(e===this.dataset?this.buildNormalizedValuesOnly(e,t):null),o=0;if(i!==null){let u=this.dataset;if(a){for(let c=0;c<i.length;c++){let d=i[c];if(a[d]?.includes(s)){if(o<n.offset){o+=1;continue}if(r.push(d),o+=1,this.hasReachedWindowLimit(n,r.length))break}}return this.materializeIndicesResult(r)}for(let c=0;c<i.length;c++){let d=i[c],h=u[d];if(typeof h[t]=="string"&&h[t].toLowerCase().includes(s)){if(o<n.offset){o+=1;continue}if(r.push(d),o+=1,this.hasReachedWindowLimit(n,r.length))break}}return this.materializeIndicesResult(r)}if(a){for(let u=0;u<e.length;u++)if(a[u]?.includes(s)){if(o<n.offset){o+=1;continue}if(r.push(u),o+=1,this.hasReachedWindowLimit(n,r.length))break}return this.materializeIndicesResult(r)}for(let u=0;u<e.length;u++)if(typeof e[u][t]=="string"&&e[u][t].toLowerCase().includes(s)){if(o<n.offset){o+=1;continue}if(r.push(u),o+=1,this.hasReachedWindowLimit(n,r.length))break}return this.materializeIndicesResult(r)}clearIndexes(){return this.flatIndexes.clear(),this.nestedCollection.clearIndexes(),this.normalizedValuesCache.clear(),this.combinedNormalizedValuesCache=null,this}getOriginData(){return this.state.getOriginData()}data(e){return this.state.data(e),this}add(e){return this.state.add(e),this}update(e){return this.state.update(e),this}applyAddedItems(e,t){if(e.length===0)return this;for(let s of this.indexedFields)this.addItemsToField(s,e,t);return this.nestedCollection.addItems(e,t),this}clearData(){return this.state.clearData(),this}handleStateMutation(e){if(this.shouldDeferMutationIndexUpdates()&&(e.type==="add"||e.type==="update")){this.markDeferredMutationState();return}switch(e.type){case "add":this.applyAddedItems(e.items,e.startIndex);for(let[t,s]of this.normalizedValuesCache)for(let i=0;i<e.items.length;i++){let n=e.items[i][t];s[e.startIndex+i]=typeof n=="string"?n.toLowerCase():"";}this.combinedNormalizedValuesCache=null,this.clearPreviousSearchState();return;case "update":this.applyUpdatedItem(e.index,e.previousItem,e.nextItem),this.invalidateNormalizedValuesCacheEntry(e.index,e.nextItem),this.clearPreviousSearchState();return;case "data":this.runtime.deferredMutationVersion=null,this.rebuildConfiguredIndexes(),this.combinedNormalizedValuesCache=null,this.clearPreviousSearchState();return;case "clearData":this.runtime.deferredMutationVersion=null,this.flatIndexes.clear(),this.nestedCollection.clearIndexes(),this.cachedLinearSearchFieldsList=null,this.normalizedValuesCache.clear(),this.combinedNormalizedValuesCache=null,this.clearPreviousSearchState();return;case "remove":this.applyRemovedItem(e.removedItem,e.removedIndex,e.movedItem,e.movedFromIndex),this.cachedLinearSearchFieldsList=null,this.normalizedValuesCache.clear(),this.combinedNormalizedValuesCache=null,this.clearPreviousSearchState();return;case "removeMany":for(let t=0;t<e.entries.length;t++){let s=e.entries[t];this.applyRemovedItem(s.removedItem,s.removedIndex,s.movedItem,s.movedFromIndex);}this.cachedLinearSearchFieldsList=null,this.normalizedValuesCache.clear(),this.combinedNormalizedValuesCache=null,this.clearPreviousSearchState();return}}addItemsToField(e,t,s){let i=this.flatIndexes.get(e);if(!i)return;let{ngramMap:n,normalizedValues:r}=i;for(let l=0;l<t.length;l++){let a=t[l][e];if(typeof a!="string")continue;let o=a.toLowerCase(),u=s+l;r[u]=o,b(n,o,u);}i.version=this.state.getMutationVersion();}applyUpdatedItem(e,t,s){for(let i of this.indexedFields)this.updateIndexedField(i,e,t,s);this.nestedCollection.updateItem(s,t,e);}applyRemovedItem(e,t,s,i){for(let n of this.indexedFields)this.removeIndexedFieldValue(n,e,t),s!==null&&i!==null&&this.moveIndexedFieldValue(n,s,i,t);this.nestedCollection.removeItem(e,t),s!==null&&i!==null&&this.nestedCollection.moveItem(s,i,t);}updateIndexedField(e,t,s,i){let n=this.flatIndexes.get(e);if(!n)return;if(s[e]===i[e]){n.version=this.state.getMutationVersion();return}let{ngramMap:r,normalizedValues:l}=n,a=this.getNormalizedFieldValue(s,e);a&&S(r,a,t);let o=this.getNormalizedFieldValue(i,e);if(!o){delete l[t],n.version=this.state.getMutationVersion();return}l[t]=o,b(r,o,t),n.version=this.state.getMutationVersion();}removeIndexedFieldValue(e,t,s){let i=this.flatIndexes.get(e);if(!i)return;let{ngramMap:n,normalizedValues:r}=i,l=this.getNormalizedFieldValue(t,e);l&&S(n,l,s),delete r[s],i.version=this.state.getMutationVersion();}moveIndexedFieldValue(e,t,s,i){if(s===i)return;let n=this.flatIndexes.get(e);if(!n)return;let{ngramMap:r,normalizedValues:l}=n,a=l[s]??this.getNormalizedFieldValue(t,e);if(!a){delete l[s],n.version=this.state.getMutationVersion();return}S(r,a,s),b(r,a,i),l[i]=a,delete l[s],n.version=this.state.getMutationVersion();}getNormalizedFieldValue(e,t){let s=e[t];return typeof s=="string"?s.toLowerCase():null}};export{L as TextSearchEngine};
1
+ import { a as e, i as t, n } from "../chunk-BJZg_1dI.mjs";
2
+ import { n as r } from "../chunk-C5Zpu_ol.mjs";
3
+ //#region src/search/ngram.ts
4
+ function i(e) {
5
+ for (let t = 0; t < e.length - 1; t++) {
6
+ let n = t;
7
+ for (let r = t + 1; r < e.length; r++) e[r].size < e[n].size && (n = r);
8
+ if (n !== t) {
9
+ let r = e[t];
10
+ e[t] = e[n], e[n] = r;
11
+ }
12
+ }
13
+ }
14
+ function a(e, t) {
15
+ let n = [];
16
+ for (let r of t) {
17
+ let t = e.get(r);
18
+ if (!t) return null;
19
+ n.push(t);
20
+ }
21
+ return i(n), n;
22
+ }
23
+ function o(e, t) {
24
+ for (let n = 0; n < t.length; n++) if (!t[n].has(e)) return !1;
25
+ return !0;
26
+ }
27
+ function s(e, t, n, r) {
28
+ let i = a(e, t);
29
+ if (i === null) return null;
30
+ let s = i[0];
31
+ return {
32
+ smallestPostingList: s,
33
+ matches: (e) => s.has(e) && o(e, i) && !!n[e]?.includes(r)
34
+ };
35
+ }
36
+ function c(e) {
37
+ let t = Math.min(3, Math.max(2, e.length)), n = e.length - t + 1, r = Array(n);
38
+ for (let i = 0; i < n; i++) r[i] = e.substring(i, i + t);
39
+ return r;
40
+ }
41
+ function l(e, t) {
42
+ let n = e.get(t);
43
+ if (n) return n;
44
+ let r = /* @__PURE__ */ new Set();
45
+ return e.set(t, r), r;
46
+ }
47
+ function u(e) {
48
+ let t = c(e);
49
+ if (t.length <= 12) return new Set(t);
50
+ let n = /* @__PURE__ */ new Set(), r = t.length - 1;
51
+ for (let e = 0; e <= 11; e++) {
52
+ let i = Math.round(e * r / 11);
53
+ n.add(t[i]);
54
+ }
55
+ return n;
56
+ }
57
+ function d(e, t, n) {
58
+ let r = Math.min(3, t.length);
59
+ for (let i = 2; i <= r; i++) {
60
+ let r = t.length - i;
61
+ for (let a = 0; a <= r; a++) l(e, t.substring(a, a + i)).add(n);
62
+ }
63
+ }
64
+ function f(e, t, n, r, i = {}) {
65
+ let { restrictionLookup: a = null, take: o = Infinity } = i, c = s(e, t, n, r);
66
+ if (c === null) return [];
67
+ let l = [];
68
+ for (let e of c.smallestPostingList) if (!(a !== null && !a[e]) && c.matches(e) && (l.push(e), l.length >= o)) break;
69
+ return l;
70
+ }
71
+ function p(e, t, n, r, i) {
72
+ let { candidateIndices: a, restrictionLookup: o = null, take: c = Infinity } = i;
73
+ if (a.length === 0) return [];
74
+ let l = s(e, t, n, r);
75
+ if (l === null) return [];
76
+ let u = [];
77
+ if (o === null || a.length <= l.smallestPostingList.size) {
78
+ for (let e = 0; e < a.length; e++) {
79
+ let t = a[e];
80
+ if (l.matches(t) && (u.push(t), u.length >= c)) break;
81
+ }
82
+ return u;
83
+ }
84
+ for (let e of l.smallestPostingList) if (o[e] && l.matches(e) && (u.push(e), u.length >= c)) break;
85
+ return u;
86
+ }
87
+ function m(e, t, n) {
88
+ let r = Math.min(3, t.length);
89
+ for (let i = 2; i <= r; i++) {
90
+ let r = t.length - i;
91
+ for (let a = 0; a <= r; a++) {
92
+ let r = t.substring(a, a + i), o = e.get(r);
93
+ o && (o.delete(n), o.size === 0 && e.delete(r));
94
+ }
95
+ }
96
+ }
97
+ //#endregion
98
+ //#region src/search/nested.ts
99
+ var h = class {
100
+ constructor(e = {
101
+ ngramIndexes: /* @__PURE__ */ new Map(),
102
+ normalizedFieldValues: /* @__PURE__ */ new Map()
103
+ }) {
104
+ this.storage = e, this.registeredFields = /* @__PURE__ */ new Set(), this.fieldDescriptors = /* @__PURE__ */ new Map();
105
+ }
106
+ registerFields(e) {
107
+ if (e?.length) for (let t of e) this.registerField(t);
108
+ }
109
+ hasRegisteredFields() {
110
+ return this.registeredFields.size > 0;
111
+ }
112
+ hasField(e) {
113
+ return this.registeredFields.has(e);
114
+ }
115
+ hasIndexes() {
116
+ return this.storage.ngramIndexes.size > 0;
117
+ }
118
+ clearIndexes() {
119
+ this.storage.ngramIndexes.clear(), this.storage.normalizedFieldValues.clear();
120
+ }
121
+ buildIndexes(e) {
122
+ this.clearIndexes();
123
+ for (let t of this.registeredFields) this.buildIndex(e, t);
124
+ }
125
+ addItems(e, t) {
126
+ if (!(e.length === 0 || this.storage.ngramIndexes.size === 0)) for (let n of this.storage.ngramIndexes.keys()) this.addItemsToField(n, e, t);
127
+ }
128
+ updateItem(e, t, n) {
129
+ if (this.storage.ngramIndexes.size !== 0) for (let r of this.storage.ngramIndexes.keys()) this.updateItemInField(r, e, t, n);
130
+ }
131
+ removeItem(e, t) {
132
+ if (this.storage.ngramIndexes.size !== 0) for (let n of this.storage.ngramIndexes.keys()) this.removeItemFromField(n, e, t);
133
+ }
134
+ moveItem(e, t, n) {
135
+ if (!(this.storage.ngramIndexes.size === 0 || t === n)) for (let r of this.storage.ngramIndexes.keys()) this.moveItemForField(r, e, t, n);
136
+ }
137
+ searchAllIndexedFieldIndices(e, t, n, r) {
138
+ let i = /* @__PURE__ */ new Set(), a = [];
139
+ for (let o of this.storage.ngramIndexes.keys()) for (let s of this.searchIndexedFieldIndices(o, e, t, n, r)) i.has(s) || (i.add(s), a.push(s));
140
+ return a;
141
+ }
142
+ searchIndexedField(e, t, n, r, i, a, o = Infinity) {
143
+ let s = this.searchIndexedFieldIndices(t, n, r, i, a, o), c = [];
144
+ for (let t = 0; t < s.length; t++) {
145
+ let n = e[s[t]];
146
+ n && c.push(n);
147
+ }
148
+ return c;
149
+ }
150
+ searchIndexedFieldIndices(e, t, n, r, i, a = Infinity) {
151
+ let o = this.storage.ngramIndexes.get(e);
152
+ if (!o) return [];
153
+ let s = this.storage.normalizedFieldValues.get(e) ?? [];
154
+ return i == null ? f(o, n, s, t, {
155
+ restrictionLookup: r,
156
+ take: a
157
+ }) : p(o, n, s, t, {
158
+ candidateIndices: i,
159
+ restrictionLookup: r,
160
+ take: a
161
+ });
162
+ }
163
+ searchFieldLinear(e, t, n) {
164
+ let r = this.searchFieldLinearIndices(e, t, n), i = [];
165
+ for (let t = 0; t < r.length; t++) {
166
+ let n = e[r[t]];
167
+ n && i.push(n);
168
+ }
169
+ return i;
170
+ }
171
+ searchFieldLinearIndices(e, t, n, r) {
172
+ let i = this.fieldDescriptors.get(t);
173
+ if (!i) return [];
174
+ let { collectionKey: a, nestedKey: o } = i, s = [];
175
+ if (r) {
176
+ for (let t = 0; t < r.length; t++) {
177
+ let i = r[t], c = e[i][a];
178
+ if (!Array.isArray(c)) continue;
179
+ let l = !1;
180
+ for (let e = 0; e < c.length; e++) {
181
+ let t = c[e][o];
182
+ if (typeof t == "string" && t.toLowerCase().includes(n)) {
183
+ l = !0;
184
+ break;
185
+ }
186
+ }
187
+ l && s.push(i);
188
+ }
189
+ return s;
190
+ }
191
+ for (let t = 0; t < e.length; t++) {
192
+ let r = e[t][a];
193
+ if (!Array.isArray(r)) continue;
194
+ let i = !1;
195
+ for (let e = 0; e < r.length; e++) {
196
+ let t = r[e][o];
197
+ if (typeof t == "string" && t.toLowerCase().includes(n)) {
198
+ i = !0;
199
+ break;
200
+ }
201
+ }
202
+ i && s.push(t);
203
+ }
204
+ return s;
205
+ }
206
+ matchesAnyField(e, t) {
207
+ for (let n of this.registeredFields) {
208
+ let r = this.fieldDescriptors.get(n);
209
+ if (!r) continue;
210
+ let { collectionKey: i, nestedKey: a } = r, o = e[i];
211
+ if (Array.isArray(o)) for (let e = 0; e < o.length; e++) {
212
+ let n = o[e][a];
213
+ if (typeof n == "string" && n.toLowerCase().includes(t)) return !0;
214
+ }
215
+ }
216
+ return !1;
217
+ }
218
+ registerField(e) {
219
+ let t = r(e);
220
+ t && (this.registeredFields.add(e), this.fieldDescriptors.set(e, t));
221
+ }
222
+ getNormalizedValues(e) {
223
+ let t = this.storage.normalizedFieldValues.get(e);
224
+ if (t) return t;
225
+ let n = [];
226
+ return this.storage.normalizedFieldValues.set(e, n), n;
227
+ }
228
+ buildIndex(e, t) {
229
+ let n = this.fieldDescriptors.get(t);
230
+ if (!n) return;
231
+ let { collectionKey: r, nestedKey: i } = n, a = /* @__PURE__ */ new Map(), o = Array(e.length);
232
+ for (let t = 0, n = e.length; t < n; t++) {
233
+ let n = e[t][r];
234
+ if (!Array.isArray(n)) continue;
235
+ let s = [];
236
+ for (let e = 0; e < n.length; e++) {
237
+ let t = n[e][i];
238
+ typeof t == "string" && s.push(t.toLowerCase());
239
+ }
240
+ if (s.length === 0) continue;
241
+ let c = s.join("\n");
242
+ o[t] = c, d(a, c, t);
243
+ }
244
+ this.storage.ngramIndexes.set(t, a), this.storage.normalizedFieldValues.set(t, o);
245
+ }
246
+ addItemsToField(e, t, n) {
247
+ let r = this.fieldDescriptors.get(e), i = this.storage.ngramIndexes.get(e);
248
+ if (!r || !i) return;
249
+ let a = this.getNormalizedValues(e), { collectionKey: o, nestedKey: s } = r;
250
+ for (let e = 0; e < t.length; e++) {
251
+ let r = t[e][o];
252
+ if (!Array.isArray(r)) continue;
253
+ let c = [], l = n + e;
254
+ for (let e = 0; e < r.length; e++) {
255
+ let t = r[e][s];
256
+ typeof t == "string" && c.push(t.toLowerCase());
257
+ }
258
+ if (c.length === 0) continue;
259
+ let u = c.join("\n");
260
+ a[l] = u, d(i, u, l);
261
+ }
262
+ }
263
+ updateItemInField(e, t, n, r) {
264
+ let i = this.storage.ngramIndexes.get(e);
265
+ if (!i) return;
266
+ let a = this.getNormalizedValues(e), o = this.getNormalizedItemValue(e, n), s = this.getNormalizedItemValue(e, t);
267
+ if (o !== s) {
268
+ if (o && m(i, o, r), !s) {
269
+ delete a[r];
270
+ return;
271
+ }
272
+ a[r] = s, d(i, s, r);
273
+ }
274
+ }
275
+ removeItemFromField(e, t, n) {
276
+ let r = this.storage.ngramIndexes.get(e);
277
+ if (!r) return;
278
+ let i = this.getNormalizedValues(e), a = i[n] ?? this.getNormalizedItemValue(e, t);
279
+ a && m(r, a, n), delete i[n];
280
+ }
281
+ moveItemForField(e, t, n, r) {
282
+ let i = this.storage.ngramIndexes.get(e);
283
+ if (!i) return;
284
+ let a = this.getNormalizedValues(e), o = a[n] ?? this.getNormalizedItemValue(e, t);
285
+ if (!o) {
286
+ delete a[n];
287
+ return;
288
+ }
289
+ m(i, o, n), d(i, o, r), a[r] = o, delete a[n];
290
+ }
291
+ getNormalizedItemValue(e, t) {
292
+ let n = this.fieldDescriptors.get(e);
293
+ if (!n) return null;
294
+ let { collectionKey: r, nestedKey: i } = n, a = t[r];
295
+ if (!Array.isArray(a)) return null;
296
+ let o = [];
297
+ for (let e = 0; e < a.length; e++) {
298
+ let t = a[e][i];
299
+ typeof t == "string" && o.push(t.toLowerCase());
300
+ }
301
+ return o.length === 0 ? null : o.join("\n");
302
+ }
303
+ }, g = () => ({
304
+ indexedFields: /* @__PURE__ */ new Set(),
305
+ flatIndexes: /* @__PURE__ */ new Map(),
306
+ nestedStorage: {
307
+ ngramIndexes: /* @__PURE__ */ new Map(),
308
+ normalizedFieldValues: /* @__PURE__ */ new Map()
309
+ },
310
+ deferredMutationVersion: null,
311
+ filterByPreviousResult: !1,
312
+ previousResultIndices: null,
313
+ previousResultLookup: null,
314
+ previousQuery: null,
315
+ stats: {
316
+ totalQueries: 0,
317
+ indexedQueries: 0,
318
+ fallbackQueries: 0,
319
+ fallbackFields: /* @__PURE__ */ new Map()
320
+ }
321
+ }), _ = class {
322
+ constructor(t = {}) {
323
+ this.cachedIndexedFieldsList = null, this.cachedLinearSearchFieldsList = null, this.emittedWarningKeys = /* @__PURE__ */ new Set(), this.warnings = [], this.normalizedValuesCache = /* @__PURE__ */ new Map(), this.combinedNormalizedValuesCache = null, this.minQueryLength = t.minQueryLength ?? 1, this.silent = t.silent ?? !1, this.state = t.state ?? new e(t.data ?? []), this.namespace = this.state.createNamespace("search"), this.nestedCollection = new h(this.runtime.nestedStorage), this.nestedCollection.registerFields(t.nestedFields), this.state.subscribe((e) => this.handleStateMutation(e)), t.filterByPreviousResult && (this.runtime.filterByPreviousResult = !0);
324
+ let n = t.fields?.length, r = this.nestedCollection.hasRegisteredFields();
325
+ if (n) for (let e of t.fields) this.indexedFields.add(e);
326
+ this.dataset.length > 0 && (n || r) && this.rebuildConfiguredIndexes();
327
+ }
328
+ get dataset() {
329
+ return this.state.getOriginData();
330
+ }
331
+ get runtime() {
332
+ return this.state.getOrCreateScopedValue(this.namespace, "runtime", g);
333
+ }
334
+ get flatIndexes() {
335
+ return this.runtime.flatIndexes;
336
+ }
337
+ get indexedFields() {
338
+ return this.runtime.indexedFields;
339
+ }
340
+ shouldDeferMutationIndexUpdates() {
341
+ return this.state.getScopedValue(t, n) === !0;
342
+ }
343
+ markDeferredMutationState() {
344
+ this.runtime.deferredMutationVersion = this.state.getMutationVersion(), this.flatIndexes.clear(), this.nestedCollection.clearIndexes(), this.cachedIndexedFieldsList = null, this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
345
+ }
346
+ ensureConfiguredIndexesReady() {
347
+ this.runtime.deferredMutationVersion !== null && (this.runtime.deferredMutationVersion = null, this.dataset.length > 0 && (this.indexedFields.size > 0 || this.nestedCollection.hasRegisteredFields()) && this.rebuildConfiguredIndexes());
348
+ }
349
+ rebuildConfiguredIndexes() {
350
+ this.flatIndexes.clear(), this.nestedCollection.clearIndexes(), this.cachedIndexedFieldsList = null, this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null;
351
+ for (let e of this.indexedFields) this.buildIndexFromData(this.dataset, e);
352
+ this.nestedCollection.hasRegisteredFields() && this.nestedCollection.buildIndexes(this.dataset);
353
+ }
354
+ buildIndexFromData(e, t) {
355
+ let n = this.state.getMutationVersion(), r = /* @__PURE__ */ new Map(), i = Array(e.length);
356
+ for (let n = 0, a = e.length; n < a; n++) {
357
+ let a = e[n][t];
358
+ if (typeof a != "string") continue;
359
+ let o = a.toLowerCase();
360
+ i[n] = o, d(r, o, n);
361
+ }
362
+ this.flatIndexes.set(t, {
363
+ ngramMap: r,
364
+ normalizedValues: i,
365
+ version: n
366
+ });
367
+ }
368
+ search(e, t, n) {
369
+ return typeof t == "string" ? this.searchField(e, t, n) : this.searchAll(e, t);
370
+ }
371
+ searchAll(e, t) {
372
+ return this.searchAllFields(e, t);
373
+ }
374
+ normalizeQuery(e) {
375
+ return e.trim().toLowerCase();
376
+ }
377
+ normalizeSearchWindow(e) {
378
+ let t = Math.max(0, Math.trunc(e?.offset ?? 0)), n = e?.limit, r = n === void 0 ? Infinity : Math.max(0, Math.trunc(n));
379
+ return {
380
+ offset: t,
381
+ limit: r,
382
+ take: Number.isFinite(r) ? t + r : Infinity,
383
+ hasWindow: t > 0 || Number.isFinite(r)
384
+ };
385
+ }
386
+ shouldTrackPreviousResult(e) {
387
+ return !e.hasWindow;
388
+ }
389
+ hasReachedWindowLimit(e, t) {
390
+ return Number.isFinite(e.limit) && t >= e.limit;
391
+ }
392
+ sliceItems(e, t) {
393
+ return t.hasWindow ? e.slice(t.offset, t.take) : e;
394
+ }
395
+ collectItemsFromIndices(e, t) {
396
+ let n = t.hasWindow && e.length > 0 ? e.slice(t.offset, t.take) : e, r = [];
397
+ for (let e = 0; e < n.length; e++) {
398
+ let t = this.dataset[n[e]];
399
+ t && r.push(t);
400
+ }
401
+ return {
402
+ items: r,
403
+ indices: n
404
+ };
405
+ }
406
+ createLookup(e) {
407
+ let t = new Uint8Array(this.dataset.length);
408
+ for (let n = 0; n < e.length; n++) t[e[n]] = 1;
409
+ return t;
410
+ }
411
+ getRestrictionLookup(e) {
412
+ let t = this.runtime.previousResultLookup;
413
+ if (t !== null && this.runtime.previousResultIndices === e && t.length === this.dataset.length) return t;
414
+ let n = this.createLookup(e);
415
+ return this.runtime.previousResultLookup = n, n;
416
+ }
417
+ getSearchSource(e) {
418
+ let { runtime: t } = this;
419
+ return t.filterByPreviousResult ? t.previousQuery !== null && t.previousResultIndices !== null && e.includes(t.previousQuery) ? {
420
+ indices: t.previousResultIndices,
421
+ lookup: this.getRestrictionLookup(t.previousResultIndices)
422
+ } : (t.previousResultIndices = null, t.previousResultLookup = null, t.previousQuery = null, {
423
+ indices: null,
424
+ lookup: null
425
+ }) : {
426
+ indices: null,
427
+ lookup: null
428
+ };
429
+ }
430
+ saveSearchResult(e, t) {
431
+ let { runtime: n } = this;
432
+ n.filterByPreviousResult && (n.previousResultIndices = e, n.previousResultLookup = null, n.previousQuery = t);
433
+ }
434
+ persistSearchResult(e, t, n) {
435
+ n && this.saveSearchResult(e.indices, t);
436
+ }
437
+ resetSearchState() {
438
+ return this.clearPreviousSearchState(), this;
439
+ }
440
+ getWarnings() {
441
+ return [...this.warnings];
442
+ }
443
+ getStats() {
444
+ let { totalQueries: e, indexedQueries: t, fallbackQueries: n, fallbackFields: r } = this.runtime.stats;
445
+ return {
446
+ totalQueries: e,
447
+ indexedQueries: t,
448
+ fallbackQueries: n,
449
+ fallbackRate: e === 0 ? 0 : n / e,
450
+ fallbackFields: Object.fromEntries(r)
451
+ };
452
+ }
453
+ resetStats() {
454
+ let { stats: e } = this.runtime;
455
+ return e.totalQueries = 0, e.indexedQueries = 0, e.fallbackQueries = 0, e.fallbackFields.clear(), this;
456
+ }
457
+ clearPreviousSearchState() {
458
+ let { runtime: e } = this;
459
+ e.previousResultIndices = null, e.previousResultLookup = null, e.previousQuery = null;
460
+ }
461
+ recordIndexedQuery() {
462
+ let { stats: e } = this.runtime;
463
+ e.totalQueries += 1, e.indexedQueries += 1;
464
+ }
465
+ recordFallbackQuery(e) {
466
+ let { stats: t } = this.runtime;
467
+ t.totalQueries += 1, t.fallbackQueries += 1, t.fallbackFields.set(e, (t.fallbackFields.get(e) ?? 0) + 1);
468
+ }
469
+ shouldEmitFallbackWarning(e) {
470
+ return !this.silent && e.length >= 2;
471
+ }
472
+ warnAboutFallback(e, t, n) {
473
+ if (!this.shouldEmitFallbackWarning(t)) return;
474
+ let r = `${e}\u0000${t}\u0000${n}`;
475
+ if (this.emittedWarningKeys.has(r)) return;
476
+ this.emittedWarningKeys.add(r);
477
+ let i = `[TextSearchEngine] warn: query "${t}" on ${e} used linear fallback. ${n}. Add the field(s) to the index schema to enable indexed search.`;
478
+ this.warnings.push(i), typeof process < "u" && process.env.NODE_ENV !== "production" && process.env.NODE_ENV !== "test" && console.warn(i);
479
+ }
480
+ getResolvedFlatIndex(e) {
481
+ let t = this.state.getMutationVersion(), n = this.flatIndexes.get(e);
482
+ return n && n.version !== t && this.dataset.length > 0 && (this.buildIndexFromData(this.dataset, e), n = this.flatIndexes.get(e)), n;
483
+ }
484
+ getQueryGrams(e) {
485
+ let t = u(e);
486
+ return t.size > 0 ? t : null;
487
+ }
488
+ searchAllFields(e, t) {
489
+ let n = this.normalizeQuery(e), r = this.normalizeSearchWindow(t), i = "all fields";
490
+ if (r.limit === 0) return [];
491
+ if (!n || n.length < this.minQueryLength) return this.sliceItems(this.dataset, r);
492
+ n.length >= 2 && this.ensureConfiguredIndexesReady();
493
+ let a = this.shouldTrackPreviousResult(r), { indices: o, lookup: s } = this.getSearchSource(n), c, l = () => (c === void 0 && (c = n.length >= 2 ? this.getQueryGrams(n) : null), c);
494
+ if (o !== null) {
495
+ let e = l();
496
+ if (e !== null && (this.flatIndexes.size > 0 || this.nestedCollection.hasIndexes())) {
497
+ this.recordIndexedQuery();
498
+ let t = this.searchAllFieldsIndexed(n, e, r, s, o);
499
+ return this.persistSearchResult(t, n, a), t.items;
500
+ }
501
+ this.recordFallbackQuery(i);
502
+ let t = this.searchLinearAllFields(this.dataset, n, o, r);
503
+ return this.persistSearchResult(t, n, a), t.items;
504
+ }
505
+ if (!this.flatIndexes.size && !this.nestedCollection.hasIndexes()) {
506
+ this.warnAboutFallback(i, n, this.indexedFields.size > 0 ? "configured indexes are not currently built" : "no indexed fields are configured"), this.recordFallbackQuery(i);
507
+ let e = this.searchLinearAllFields(this.dataset, n, null, r);
508
+ return this.persistSearchResult(e, n, a), e.items;
509
+ }
510
+ let u = l();
511
+ if (u === null) {
512
+ this.recordFallbackQuery(i);
513
+ let e = this.searchLinearAllFields(this.dataset, n, null, r);
514
+ return this.persistSearchResult(e, n, a), e.items;
515
+ }
516
+ this.recordIndexedQuery();
517
+ let d = this.searchAllFieldsIndexed(n, u, r, null);
518
+ return this.persistSearchResult(d, n, a), d.items;
519
+ }
520
+ searchAllFieldsIndexed(e, t, n, r, i = null) {
521
+ if (i !== null && !this.nestedCollection.hasRegisteredFields() && this.flatIndexes.size > 0) return this.searchAllFieldsIndexedInCandidates(e, t, n, r, i);
522
+ let a = this.dataset, o = new Uint8Array(a.length), s = [], c = 0;
523
+ for (let a of this.flatIndexes.keys()) {
524
+ let l = this.searchFieldWithPreparedQueryIndices(a, e, t, r, i);
525
+ for (let e = 0; e < l.length; e++) {
526
+ let t = l[e];
527
+ if (!o[t]) {
528
+ if (o[t] = 1, c < n.offset) {
529
+ c += 1;
530
+ continue;
531
+ }
532
+ if (s.push(t), c += 1, this.hasReachedWindowLimit(n, s.length)) return this.materializeIndicesResult(s);
533
+ }
534
+ }
535
+ }
536
+ for (let a of this.nestedCollection.searchAllIndexedFieldIndices(e, t, r, i)) if (!o[a]) {
537
+ if (o[a] = 1, c < n.offset) {
538
+ c += 1;
539
+ continue;
540
+ }
541
+ if (s.push(a), c += 1, this.hasReachedWindowLimit(n, s.length)) return this.materializeIndicesResult(s);
542
+ }
543
+ return this.materializeIndicesResult(s);
544
+ }
545
+ searchAllFieldsIndexedInCandidates(e, t, n, r, i) {
546
+ let a = [];
547
+ for (let n of this.flatIndexes.keys()) {
548
+ let r = this.getResolvedFlatIndex(n);
549
+ if (!r) continue;
550
+ let i = s(r.ngramMap, t, r.normalizedValues, e);
551
+ i !== null && a.push(i.matches);
552
+ }
553
+ if (a.length === 0) return {
554
+ items: [],
555
+ indices: []
556
+ };
557
+ let o = [], c = 0;
558
+ for (let e = 0; e < i.length; e++) {
559
+ let t = i[e];
560
+ if (r !== null && !r[t]) continue;
561
+ let s = !1;
562
+ for (let e = 0; e < a.length; e++) if (a[e](t)) {
563
+ s = !0;
564
+ break;
565
+ }
566
+ if (s) {
567
+ if (c < n.offset) {
568
+ c += 1;
569
+ continue;
570
+ }
571
+ if (o.push(t), c += 1, this.hasReachedWindowLimit(n, o.length)) break;
572
+ }
573
+ }
574
+ return this.materializeIndicesResult(o);
575
+ }
576
+ searchField(e, t, n) {
577
+ let r = this.normalizeQuery(t), i = this.normalizeSearchWindow(n), a = `field "${e}"`;
578
+ if (i.limit === 0) return [];
579
+ if (!r || r.length < this.minQueryLength) return this.sliceItems(this.dataset, i);
580
+ r.length >= 2 && this.ensureConfiguredIndexesReady();
581
+ let o = this.shouldTrackPreviousResult(i), { indices: s, lookup: c } = this.getSearchSource(r), l = this.nestedCollection.hasField(e), u, d = () => (u === void 0 && (u = r.length >= 2 ? this.getQueryGrams(r) : null), u);
582
+ if (s !== null) {
583
+ let t = d();
584
+ if (l) {
585
+ if (t !== null && this.nestedCollection.hasIndexes()) {
586
+ this.recordIndexedQuery();
587
+ let n = this.nestedCollection.searchIndexedFieldIndices(e, r, t, c, s, i.take), a = this.collectItemsFromIndices(n, i);
588
+ return this.persistSearchResult(a, r, o), a.items;
589
+ }
590
+ this.recordFallbackQuery(a);
591
+ let n = this.searchLinearSingleField(this.dataset, e, r, s, i);
592
+ return this.persistSearchResult(n, r, o), n.items;
593
+ }
594
+ if (t !== null && this.flatIndexes.has(e)) {
595
+ this.recordIndexedQuery();
596
+ let n = this.searchFieldWithPreparedQuery(e, r, t, i, c, s);
597
+ return this.persistSearchResult(n, r, o), n.items;
598
+ }
599
+ this.recordFallbackQuery(a);
600
+ let n = this.searchLinearSingleField(this.dataset, e, r, s, i);
601
+ return this.persistSearchResult(n, r, o), n.items;
602
+ }
603
+ if (l) {
604
+ if (r.length >= 2 && this.nestedCollection.hasIndexes()) {
605
+ let t = this.getQueryGrams(r);
606
+ if (t === null) return [];
607
+ this.recordIndexedQuery();
608
+ let n = this.nestedCollection.searchIndexedFieldIndices(e, r, t, null, null, i.take), a = this.collectItemsFromIndices(n, i);
609
+ return this.persistSearchResult(a, r, o), a.items;
610
+ }
611
+ this.recordFallbackQuery(a);
612
+ let t = this.searchLinearSingleField(this.dataset, e, r, null, i);
613
+ return this.persistSearchResult(t, r, o), t.items;
614
+ }
615
+ this.flatIndexes.size || this.warnAboutFallback(a, r, this.indexedFields.size > 0 ? `field "${e}" is not backed by an active index` : "no indexed fields are configured");
616
+ let f = d();
617
+ if (this.flatIndexes.size > 0 && f !== null) {
618
+ if (!this.flatIndexes.has(e)) return [];
619
+ this.recordIndexedQuery();
620
+ let t = this.searchFieldWithPreparedQuery(e, r, f, i);
621
+ return this.persistSearchResult(t, r, o), t.items;
622
+ }
623
+ this.recordFallbackQuery(a);
624
+ let p = this.searchLinearSingleField(this.dataset, e, r, null, i);
625
+ return this.persistSearchResult(p, r, o), p.items;
626
+ }
627
+ searchFieldWithPreparedQuery(e, t, n, r, i = null, a = null) {
628
+ let o = this.searchFieldWithPreparedQueryIndices(e, t, n, i, a, r.take);
629
+ return this.collectItemsFromIndices(o, r);
630
+ }
631
+ searchFieldWithPreparedQueryIndices(e, t, n, r = null, i = null, a = Infinity) {
632
+ let o = this.getResolvedFlatIndex(e);
633
+ if (!o) return [];
634
+ let { ngramMap: s, normalizedValues: c } = o;
635
+ return i === null ? f(s, n, c, t, {
636
+ restrictionLookup: r,
637
+ take: a
638
+ }) : p(s, n, c, t, {
639
+ candidateIndices: i,
640
+ restrictionLookup: r,
641
+ take: a
642
+ });
643
+ }
644
+ getIndexedFieldsList() {
645
+ return this.cachedIndexedFieldsList === null && (this.cachedIndexedFieldsList = Array.from(this.indexedFields)), this.cachedIndexedFieldsList;
646
+ }
647
+ materializeIndicesResult(e) {
648
+ let t = [];
649
+ for (let n = 0; n < e.length; n++) {
650
+ let r = this.dataset[e[n]];
651
+ r && t.push(r);
652
+ }
653
+ return {
654
+ items: t,
655
+ indices: e
656
+ };
657
+ }
658
+ getLinearSearchFields(e) {
659
+ let t = this.getIndexedFieldsList();
660
+ return t.length > 0 ? t : (this.cachedLinearSearchFieldsList === null && (this.cachedLinearSearchFieldsList = e.length ? Object.keys(e[0]).filter((t) => typeof e[0][t] == "string") : []), this.cachedLinearSearchFieldsList);
661
+ }
662
+ buildNormalizedValuesOnly(e, t) {
663
+ let n = Array(e.length);
664
+ for (let r = 0, i = e.length; r < i; r++) {
665
+ let i = e[r][t];
666
+ typeof i == "string" && (n[r] = i.toLowerCase());
667
+ }
668
+ return this.normalizedValuesCache.set(t, n), n;
669
+ }
670
+ buildCombinedNormalizedValues(e, t) {
671
+ let n = Array(e.length);
672
+ for (let r = 0; r < e.length; r++) n[r] = this.buildCombinedNormalizedValue(e[r], t);
673
+ return this.combinedNormalizedValuesCache = {
674
+ fieldsKey: t.join("\0"),
675
+ values: n
676
+ }, n;
677
+ }
678
+ buildCombinedNormalizedValue(e, t) {
679
+ let n = "";
680
+ for (let r = 0; r < t.length; r++) {
681
+ let i = e[t[r]];
682
+ typeof i == "string" && (n && (n += "\n"), n += i.toLowerCase());
683
+ }
684
+ return n;
685
+ }
686
+ getCombinedNormalizedValues(e, t) {
687
+ if (e !== this.dataset) return null;
688
+ let n = t.join("\0");
689
+ return this.combinedNormalizedValuesCache?.fieldsKey === n ? this.combinedNormalizedValuesCache.values : this.buildCombinedNormalizedValues(e, t);
690
+ }
691
+ invalidateNormalizedValuesCacheEntry(e, t) {
692
+ for (let [n, r] of this.normalizedValuesCache) {
693
+ let i = t[n];
694
+ r[e] = typeof i == "string" ? i.toLowerCase() : "";
695
+ }
696
+ if (this.combinedNormalizedValuesCache !== null) {
697
+ let n = this.combinedNormalizedValuesCache.fieldsKey.split("\0");
698
+ this.combinedNormalizedValuesCache.values[e] = this.buildCombinedNormalizedValue(t, n);
699
+ }
700
+ }
701
+ searchLinearAllFields(e, t, n, r) {
702
+ if (!e.length) return {
703
+ items: [],
704
+ indices: []
705
+ };
706
+ let i = this.getLinearSearchFields(e), a = e === this.dataset, o = i.length, s = Array(o);
707
+ for (let t = 0; t < o; t++) {
708
+ let n = i[t], r = this.flatIndexes.get(n);
709
+ if (r) s[t] = r.normalizedValues;
710
+ else {
711
+ let r = this.normalizedValuesCache.get(n);
712
+ r ? s[t] = r : a ? s[t] = this.buildNormalizedValuesOnly(e, n) : s[t] = null;
713
+ }
714
+ }
715
+ let c = this.nestedCollection.hasRegisteredFields(), l = c ? null : this.getCombinedNormalizedValues(e, i), u = !c && s.every((e) => e !== null), d = [], f = 0;
716
+ if (n !== null) {
717
+ let e = this.dataset;
718
+ if (l !== null) {
719
+ for (let e = 0; e < n.length; e++) {
720
+ let i = n[e];
721
+ if (l[i]?.includes(t)) {
722
+ if (f < r.offset) {
723
+ f += 1;
724
+ continue;
725
+ }
726
+ if (d.push(i), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
727
+ }
728
+ }
729
+ return this.materializeIndicesResult(d);
730
+ }
731
+ if (u) {
732
+ for (let e = 0; e < n.length; e++) {
733
+ let i = n[e], a = !1;
734
+ for (let e = 0; e < o; e++) {
735
+ let n = s[e][i];
736
+ if (n && n.includes(t)) {
737
+ a = !0;
738
+ break;
739
+ }
740
+ }
741
+ if (a) {
742
+ if (f < r.offset) {
743
+ f += 1;
744
+ continue;
745
+ }
746
+ if (d.push(i), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
747
+ }
748
+ }
749
+ return this.materializeIndicesResult(d);
750
+ }
751
+ for (let a = 0; a < n.length; a++) {
752
+ let l = n[a], u = e[l], p = !1;
753
+ for (let e = 0; e < o; e++) {
754
+ let n = s[e];
755
+ if (n) {
756
+ let e = n[l];
757
+ if (e && e.includes(t)) {
758
+ p = !0;
759
+ break;
760
+ }
761
+ } else {
762
+ let n = u[i[e]];
763
+ if (typeof n == "string" && n.toLowerCase().includes(t)) {
764
+ p = !0;
765
+ break;
766
+ }
767
+ }
768
+ }
769
+ if (!p && c && (p = this.nestedCollection.matchesAnyField(u, t)), p) {
770
+ if (f < r.offset) {
771
+ f += 1;
772
+ continue;
773
+ }
774
+ if (d.push(l), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
775
+ }
776
+ }
777
+ return this.materializeIndicesResult(d);
778
+ }
779
+ if (l !== null) {
780
+ for (let n = 0; n < e.length; n++) if (l[n]?.includes(t)) {
781
+ if (f < r.offset) {
782
+ f += 1;
783
+ continue;
784
+ }
785
+ if (d.push(n), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
786
+ }
787
+ return this.materializeIndicesResult(d);
788
+ }
789
+ if (u) {
790
+ for (let n = 0; n < e.length; n++) {
791
+ let e = !1;
792
+ for (let r = 0; r < o; r++) {
793
+ let i = s[r][n];
794
+ if (i && i.includes(t)) {
795
+ e = !0;
796
+ break;
797
+ }
798
+ }
799
+ if (e) {
800
+ if (f < r.offset) {
801
+ f += 1;
802
+ continue;
803
+ }
804
+ if (d.push(n), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
805
+ }
806
+ }
807
+ return this.materializeIndicesResult(d);
808
+ }
809
+ for (let n = 0; n < e.length; n++) {
810
+ let a = e[n], l = !1;
811
+ for (let e = 0; e < o; e++) {
812
+ let r = s[e];
813
+ if (r) {
814
+ let e = r[n];
815
+ if (e && e.includes(t)) {
816
+ l = !0;
817
+ break;
818
+ }
819
+ } else {
820
+ let n = a[i[e]];
821
+ if (typeof n == "string" && n.toLowerCase().includes(t)) {
822
+ l = !0;
823
+ break;
824
+ }
825
+ }
826
+ }
827
+ if (!l && c && (l = this.nestedCollection.matchesAnyField(a, t)), l) {
828
+ if (f < r.offset) {
829
+ f += 1;
830
+ continue;
831
+ }
832
+ if (d.push(n), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
833
+ }
834
+ }
835
+ return this.materializeIndicesResult(d);
836
+ }
837
+ searchLinearSingleField(e, t, n, r, i) {
838
+ if (!e.length) return {
839
+ items: [],
840
+ indices: []
841
+ };
842
+ if (this.nestedCollection.hasField(t)) {
843
+ let a = this.nestedCollection.searchFieldLinearIndices(e, t, n, r ?? void 0);
844
+ return this.collectItemsFromIndices(a, i);
845
+ }
846
+ let a = [], o = this.flatIndexes.get(t), s = o ? o.normalizedValues : this.normalizedValuesCache.get(t) ?? (e === this.dataset ? this.buildNormalizedValuesOnly(e, t) : null), c = 0;
847
+ if (r !== null) {
848
+ let e = this.dataset;
849
+ if (s) {
850
+ for (let e = 0; e < r.length; e++) {
851
+ let t = r[e];
852
+ if (s[t]?.includes(n)) {
853
+ if (c < i.offset) {
854
+ c += 1;
855
+ continue;
856
+ }
857
+ if (a.push(t), c += 1, this.hasReachedWindowLimit(i, a.length)) break;
858
+ }
859
+ }
860
+ return this.materializeIndicesResult(a);
861
+ }
862
+ for (let o = 0; o < r.length; o++) {
863
+ let s = r[o], l = e[s];
864
+ if (typeof l[t] == "string" && l[t].toLowerCase().includes(n)) {
865
+ if (c < i.offset) {
866
+ c += 1;
867
+ continue;
868
+ }
869
+ if (a.push(s), c += 1, this.hasReachedWindowLimit(i, a.length)) break;
870
+ }
871
+ }
872
+ return this.materializeIndicesResult(a);
873
+ }
874
+ if (s) {
875
+ for (let t = 0; t < e.length; t++) if (s[t]?.includes(n)) {
876
+ if (c < i.offset) {
877
+ c += 1;
878
+ continue;
879
+ }
880
+ if (a.push(t), c += 1, this.hasReachedWindowLimit(i, a.length)) break;
881
+ }
882
+ return this.materializeIndicesResult(a);
883
+ }
884
+ for (let r = 0; r < e.length; r++) if (typeof e[r][t] == "string" && e[r][t].toLowerCase().includes(n)) {
885
+ if (c < i.offset) {
886
+ c += 1;
887
+ continue;
888
+ }
889
+ if (a.push(r), c += 1, this.hasReachedWindowLimit(i, a.length)) break;
890
+ }
891
+ return this.materializeIndicesResult(a);
892
+ }
893
+ clearIndexes() {
894
+ return this.flatIndexes.clear(), this.nestedCollection.clearIndexes(), this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this;
895
+ }
896
+ getOriginData() {
897
+ return this.state.getOriginData();
898
+ }
899
+ data(e) {
900
+ return this.state.data(e), this;
901
+ }
902
+ add(e) {
903
+ return this.state.add(e), this;
904
+ }
905
+ update(e) {
906
+ return this.state.update(e), this;
907
+ }
908
+ applyAddedItems(e, t) {
909
+ if (e.length === 0) return this;
910
+ for (let n of this.indexedFields) this.addItemsToField(n, e, t);
911
+ return this.nestedCollection.addItems(e, t), this;
912
+ }
913
+ clearData() {
914
+ return this.state.clearData(), this;
915
+ }
916
+ handleStateMutation(e) {
917
+ if (this.shouldDeferMutationIndexUpdates() && (e.type === "add" || e.type === "update")) {
918
+ this.markDeferredMutationState();
919
+ return;
920
+ }
921
+ switch (e.type) {
922
+ case "add":
923
+ this.applyAddedItems(e.items, e.startIndex);
924
+ for (let [t, n] of this.normalizedValuesCache) for (let r = 0; r < e.items.length; r++) {
925
+ let i = e.items[r][t];
926
+ n[e.startIndex + r] = typeof i == "string" ? i.toLowerCase() : "";
927
+ }
928
+ this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
929
+ return;
930
+ case "update":
931
+ this.applyUpdatedItem(e.index, e.previousItem, e.nextItem), this.invalidateNormalizedValuesCacheEntry(e.index, e.nextItem), this.clearPreviousSearchState();
932
+ return;
933
+ case "data":
934
+ this.runtime.deferredMutationVersion = null, this.rebuildConfiguredIndexes(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
935
+ return;
936
+ case "clearData":
937
+ this.runtime.deferredMutationVersion = null, this.flatIndexes.clear(), this.nestedCollection.clearIndexes(), this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
938
+ return;
939
+ case "remove":
940
+ this.applyRemovedItem(e.removedItem, e.removedIndex, e.movedItem, e.movedFromIndex), this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
941
+ return;
942
+ case "removeMany":
943
+ for (let t = 0; t < e.entries.length; t++) {
944
+ let n = e.entries[t];
945
+ this.applyRemovedItem(n.removedItem, n.removedIndex, n.movedItem, n.movedFromIndex);
946
+ }
947
+ this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
948
+ return;
949
+ }
950
+ }
951
+ addItemsToField(e, t, n) {
952
+ let r = this.flatIndexes.get(e);
953
+ if (!r) return;
954
+ let { ngramMap: i, normalizedValues: a } = r;
955
+ for (let r = 0; r < t.length; r++) {
956
+ let o = t[r][e];
957
+ if (typeof o != "string") continue;
958
+ let s = o.toLowerCase(), c = n + r;
959
+ a[c] = s, d(i, s, c);
960
+ }
961
+ r.version = this.state.getMutationVersion();
962
+ }
963
+ applyUpdatedItem(e, t, n) {
964
+ for (let r of this.indexedFields) this.updateIndexedField(r, e, t, n);
965
+ this.nestedCollection.updateItem(n, t, e);
966
+ }
967
+ applyRemovedItem(e, t, n, r) {
968
+ for (let i of this.indexedFields) this.removeIndexedFieldValue(i, e, t), n !== null && r !== null && this.moveIndexedFieldValue(i, n, r, t);
969
+ this.nestedCollection.removeItem(e, t), n !== null && r !== null && this.nestedCollection.moveItem(n, r, t);
970
+ }
971
+ updateIndexedField(e, t, n, r) {
972
+ let i = this.flatIndexes.get(e);
973
+ if (!i) return;
974
+ if (n[e] === r[e]) {
975
+ i.version = this.state.getMutationVersion();
976
+ return;
977
+ }
978
+ let { ngramMap: a, normalizedValues: o } = i, s = this.getNormalizedFieldValue(n, e);
979
+ s && m(a, s, t);
980
+ let c = this.getNormalizedFieldValue(r, e);
981
+ if (!c) {
982
+ delete o[t], i.version = this.state.getMutationVersion();
983
+ return;
984
+ }
985
+ o[t] = c, d(a, c, t), i.version = this.state.getMutationVersion();
986
+ }
987
+ removeIndexedFieldValue(e, t, n) {
988
+ let r = this.flatIndexes.get(e);
989
+ if (!r) return;
990
+ let { ngramMap: i, normalizedValues: a } = r, o = this.getNormalizedFieldValue(t, e);
991
+ o && m(i, o, n), delete a[n], r.version = this.state.getMutationVersion();
992
+ }
993
+ moveIndexedFieldValue(e, t, n, r) {
994
+ if (n === r) return;
995
+ let i = this.flatIndexes.get(e);
996
+ if (!i) return;
997
+ let { ngramMap: a, normalizedValues: o } = i, s = o[n] ?? this.getNormalizedFieldValue(t, e);
998
+ if (!s) {
999
+ delete o[n], i.version = this.state.getMutationVersion();
1000
+ return;
1001
+ }
1002
+ m(a, s, n), d(a, s, r), o[r] = s, delete o[n], i.version = this.state.getMutationVersion();
1003
+ }
1004
+ getNormalizedFieldValue(e, t) {
1005
+ let n = e[t];
1006
+ return typeof n == "string" ? n.toLowerCase() : null;
1007
+ }
1008
+ };
1009
+ //#endregion
1010
+ export { _ as TextSearchEngine };