@devisfuture/mega-collection 2.4.2 → 2.4.4

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