@devisfuture/mega-collection 2.4.6 → 2.4.7

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.
Files changed (62) hide show
  1. package/dist/State.d.ts +42 -0
  2. package/dist/{chunk-BJZg_1dI.mjs → State.mjs} +2 -2
  3. package/dist/constants.d.ts +5 -0
  4. package/dist/constants.mjs +4 -0
  5. package/dist/filter/chain.d.ts +9 -0
  6. package/dist/filter/chain.mjs +22 -0
  7. package/dist/filter/constants.d.ts +2 -0
  8. package/dist/filter/criterion.d.ts +6 -0
  9. package/dist/filter/criterion.mjs +64 -0
  10. package/dist/filter/errors.d.ts +7 -0
  11. package/dist/filter/errors.mjs +17 -0
  12. package/dist/filter/filter.d.ts +89 -0
  13. package/dist/filter/filter.mjs +439 -0
  14. package/dist/filter/index.mjs +2 -891
  15. package/dist/filter/nested.d.ts +32 -0
  16. package/dist/filter/nested.mjs +246 -0
  17. package/dist/filter/types.d.ts +74 -0
  18. package/dist/filter/utils.d.ts +4 -0
  19. package/dist/filter/utils.mjs +29 -0
  20. package/dist/index.mjs +1 -1
  21. package/dist/indexer.d.ts +41 -0
  22. package/dist/indexer.mjs +101 -0
  23. package/dist/internal.d.ts +6 -0
  24. package/dist/{chunk-C5Zpu_ol.mjs → internal.mjs} +1 -1
  25. package/dist/merge/chain.d.ts +9 -0
  26. package/dist/merge/chain.mjs +23 -0
  27. package/dist/merge/constants.d.ts +6 -0
  28. package/dist/merge/constants.mjs +8 -0
  29. package/dist/merge/errors.d.ts +8 -0
  30. package/dist/merge/errors.mjs +19 -0
  31. package/dist/merge/index.mjs +1 -1
  32. package/dist/merge/merge-engines.d.ts +75 -0
  33. package/dist/{chunk-BURK5sks.mjs → merge/merge-engines.mjs} +35 -108
  34. package/dist/merge/module-adapters.d.ts +7 -0
  35. package/dist/merge/module-adapters.mjs +41 -0
  36. package/dist/merge/types.d.ts +125 -0
  37. package/dist/search/constants.d.ts +5 -0
  38. package/dist/search/index.mjs +2 -1010
  39. package/dist/search/nested.d.ts +41 -0
  40. package/dist/search/nested.mjs +210 -0
  41. package/dist/search/ngram.d.ts +23 -0
  42. package/dist/search/ngram.mjs +96 -0
  43. package/dist/search/text-search.d.ts +146 -0
  44. package/dist/search/text-search.mjs +696 -0
  45. package/dist/search/types.d.ts +93 -0
  46. package/dist/search/utils.d.ts +4 -0
  47. package/dist/search/utils.mjs +22 -0
  48. package/dist/sort/errors.d.ts +5 -0
  49. package/dist/sort/errors.mjs +11 -0
  50. package/dist/sort/index.mjs +2 -416
  51. package/dist/sort/sorter.d.ts +47 -0
  52. package/dist/sort/sorter.mjs +375 -0
  53. package/dist/sort/types.d.ts +18 -0
  54. package/dist/sort/utils.d.ts +17 -0
  55. package/dist/sort/utils.mjs +38 -0
  56. package/dist/types.d.ts +73 -0
  57. package/package.json +11 -34
  58. /package/dist/filter/{index.d.mts → index.d.ts} +0 -0
  59. /package/dist/{index.d.mts → index.d.ts} +0 -0
  60. /package/dist/merge/{index.d.mts → index.d.ts} +0 -0
  61. /package/dist/search/{index.d.mts → index.d.ts} +0 -0
  62. /package/dist/sort/{index.d.mts → index.d.ts} +0 -0
@@ -0,0 +1,696 @@
1
+ import { State as e } from "../State.mjs";
2
+ import { DEFER_SEARCH_MUTATION_INDEX_UPDATES_KEY as t, MERGE_SHARED_SCOPE as n } from "../constants.mjs";
3
+ import { buildIntersectionQueryGrams as r, createIntersectionPlan as i, indexLowerValue as a, intersectPostingLists as o, intersectPostingListsInCandidates as s, removeLowerValue as c } from "./ngram.mjs";
4
+ import { SearchNestedCollection as l } from "./nested.mjs";
5
+ import { createSearchRuntime as u } from "./utils.mjs";
6
+ //#region src/search/text-search.ts
7
+ var d = class {
8
+ constructor(t = {}) {
9
+ 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 l(this.runtime.nestedStorage), this.nestedCollection.registerFields(t.nestedFields), this.state.subscribe((e) => this.handleStateMutation(e)), t.filterByPreviousResult && (this.runtime.filterByPreviousResult = !0);
10
+ let n = t.fields?.length, r = this.nestedCollection.hasRegisteredFields();
11
+ if (n) for (let e of t.fields) this.indexedFields.add(e);
12
+ this.dataset.length > 0 && (n || r) && this.rebuildConfiguredIndexes();
13
+ }
14
+ get dataset() {
15
+ return this.state.getOriginData();
16
+ }
17
+ get runtime() {
18
+ return this.state.getOrCreateScopedValue(this.namespace, "runtime", u);
19
+ }
20
+ get flatIndexes() {
21
+ return this.runtime.flatIndexes;
22
+ }
23
+ get indexedFields() {
24
+ return this.runtime.indexedFields;
25
+ }
26
+ shouldDeferMutationIndexUpdates() {
27
+ return this.state.getScopedValue(n, t) === !0;
28
+ }
29
+ markDeferredMutationState() {
30
+ 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();
31
+ }
32
+ ensureConfiguredIndexesReady() {
33
+ this.runtime.deferredMutationVersion !== null && (this.runtime.deferredMutationVersion = null, this.dataset.length > 0 && (this.indexedFields.size > 0 || this.nestedCollection.hasRegisteredFields()) && this.rebuildConfiguredIndexes());
34
+ }
35
+ rebuildConfiguredIndexes() {
36
+ this.flatIndexes.clear(), this.nestedCollection.clearIndexes(), this.cachedIndexedFieldsList = null, this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null;
37
+ for (let e of this.indexedFields) this.buildIndexFromData(this.dataset, e);
38
+ this.nestedCollection.hasRegisteredFields() && this.nestedCollection.buildIndexes(this.dataset);
39
+ }
40
+ buildIndexFromData(e, t) {
41
+ let n = this.state.getMutationVersion(), r = /* @__PURE__ */ new Map(), i = Array(e.length);
42
+ for (let n = 0, o = e.length; n < o; n++) {
43
+ let o = e[n][t];
44
+ if (typeof o != "string") continue;
45
+ let s = o.toLowerCase();
46
+ i[n] = s, a(r, s, n);
47
+ }
48
+ this.flatIndexes.set(t, {
49
+ ngramMap: r,
50
+ normalizedValues: i,
51
+ version: n
52
+ });
53
+ }
54
+ search(e, t, n) {
55
+ return typeof t == "string" ? this.searchField(e, t, n) : this.searchAll(e, t);
56
+ }
57
+ searchAll(e, t) {
58
+ return this.searchAllFields(e, t);
59
+ }
60
+ normalizeQuery(e) {
61
+ return e.trim().toLowerCase();
62
+ }
63
+ normalizeSearchWindow(e) {
64
+ let t = Math.max(0, Math.trunc(e?.offset ?? 0)), n = e?.limit, r = n === void 0 ? Infinity : Math.max(0, Math.trunc(n));
65
+ return {
66
+ offset: t,
67
+ limit: r,
68
+ take: Number.isFinite(r) ? t + r : Infinity,
69
+ hasWindow: t > 0 || Number.isFinite(r)
70
+ };
71
+ }
72
+ shouldTrackPreviousResult(e) {
73
+ return !e.hasWindow;
74
+ }
75
+ hasReachedWindowLimit(e, t) {
76
+ return Number.isFinite(e.limit) && t >= e.limit;
77
+ }
78
+ sliceItems(e, t) {
79
+ return t.hasWindow ? e.slice(t.offset, t.take) : e;
80
+ }
81
+ collectItemsFromIndices(e, t) {
82
+ let n = t.hasWindow && e.length > 0 ? e.slice(t.offset, t.take) : e, r = [];
83
+ for (let e = 0; e < n.length; e++) {
84
+ let t = this.dataset[n[e]];
85
+ t && r.push(t);
86
+ }
87
+ return {
88
+ items: r,
89
+ indices: n
90
+ };
91
+ }
92
+ createLookup(e) {
93
+ let t = new Uint8Array(this.dataset.length);
94
+ for (let n = 0; n < e.length; n++) t[e[n]] = 1;
95
+ return t;
96
+ }
97
+ getRestrictionLookup(e) {
98
+ let t = this.runtime.previousResultLookup;
99
+ if (t !== null && this.runtime.previousResultIndices === e && t.length === this.dataset.length) return t;
100
+ let n = this.createLookup(e);
101
+ return this.runtime.previousResultLookup = n, n;
102
+ }
103
+ getSearchSource(e) {
104
+ let { runtime: t } = this;
105
+ return t.filterByPreviousResult ? t.previousQuery !== null && t.previousResultIndices !== null && e.includes(t.previousQuery) ? {
106
+ indices: t.previousResultIndices,
107
+ lookup: this.getRestrictionLookup(t.previousResultIndices)
108
+ } : (t.previousResultIndices = null, t.previousResultLookup = null, t.previousQuery = null, {
109
+ indices: null,
110
+ lookup: null
111
+ }) : {
112
+ indices: null,
113
+ lookup: null
114
+ };
115
+ }
116
+ saveSearchResult(e, t) {
117
+ let { runtime: n } = this;
118
+ n.filterByPreviousResult && (n.previousResultIndices = e, n.previousResultLookup = null, n.previousQuery = t);
119
+ }
120
+ persistSearchResult(e, t, n) {
121
+ n && this.saveSearchResult(e.indices, t);
122
+ }
123
+ resetSearchState() {
124
+ return this.clearPreviousSearchState(), this;
125
+ }
126
+ getWarnings() {
127
+ return [...this.warnings];
128
+ }
129
+ getStats() {
130
+ let { totalQueries: e, indexedQueries: t, fallbackQueries: n, fallbackFields: r } = this.runtime.stats;
131
+ return {
132
+ totalQueries: e,
133
+ indexedQueries: t,
134
+ fallbackQueries: n,
135
+ fallbackRate: e === 0 ? 0 : n / e,
136
+ fallbackFields: Object.fromEntries(r)
137
+ };
138
+ }
139
+ resetStats() {
140
+ let { stats: e } = this.runtime;
141
+ return e.totalQueries = 0, e.indexedQueries = 0, e.fallbackQueries = 0, e.fallbackFields.clear(), this;
142
+ }
143
+ clearPreviousSearchState() {
144
+ let { runtime: e } = this;
145
+ e.previousResultIndices = null, e.previousResultLookup = null, e.previousQuery = null;
146
+ }
147
+ recordIndexedQuery() {
148
+ let { stats: e } = this.runtime;
149
+ e.totalQueries += 1, e.indexedQueries += 1;
150
+ }
151
+ recordFallbackQuery(e) {
152
+ let { stats: t } = this.runtime;
153
+ t.totalQueries += 1, t.fallbackQueries += 1, t.fallbackFields.set(e, (t.fallbackFields.get(e) ?? 0) + 1);
154
+ }
155
+ shouldEmitFallbackWarning(e) {
156
+ return !this.silent && e.length >= 2;
157
+ }
158
+ warnAboutFallback(e, t, n) {
159
+ if (!this.shouldEmitFallbackWarning(t)) return;
160
+ let r = `${e}\u0000${t}\u0000${n}`;
161
+ if (this.emittedWarningKeys.has(r)) return;
162
+ this.emittedWarningKeys.add(r);
163
+ let i = `[TextSearchEngine] warn: query "${t}" on ${e} used linear fallback. ${n}. Add the field(s) to the index schema to enable indexed search.`;
164
+ this.warnings.push(i), typeof process < "u" && process.env.NODE_ENV !== "production" && process.env.NODE_ENV !== "test" && console.warn(i);
165
+ }
166
+ getResolvedFlatIndex(e) {
167
+ let t = this.state.getMutationVersion(), n = this.flatIndexes.get(e);
168
+ return n && n.version !== t && this.dataset.length > 0 && (this.buildIndexFromData(this.dataset, e), n = this.flatIndexes.get(e)), n;
169
+ }
170
+ getQueryGrams(e) {
171
+ let t = r(e);
172
+ return t.size > 0 ? t : null;
173
+ }
174
+ searchAllFields(e, t) {
175
+ let n = this.normalizeQuery(e), r = this.normalizeSearchWindow(t), i = "all fields";
176
+ if (r.limit === 0) return [];
177
+ if (!n || n.length < this.minQueryLength) return this.sliceItems(this.dataset, r);
178
+ n.length >= 2 && this.ensureConfiguredIndexesReady();
179
+ 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);
180
+ if (o !== null) {
181
+ let e = l();
182
+ if (e !== null && (this.flatIndexes.size > 0 || this.nestedCollection.hasIndexes())) {
183
+ this.recordIndexedQuery();
184
+ let t = this.searchAllFieldsIndexed(n, e, r, s, o);
185
+ return this.persistSearchResult(t, n, a), t.items;
186
+ }
187
+ this.recordFallbackQuery(i);
188
+ let t = this.searchLinearAllFields(this.dataset, n, o, r);
189
+ return this.persistSearchResult(t, n, a), t.items;
190
+ }
191
+ if (!this.flatIndexes.size && !this.nestedCollection.hasIndexes()) {
192
+ this.warnAboutFallback(i, n, this.indexedFields.size > 0 ? "configured indexes are not currently built" : "no indexed fields are configured"), this.recordFallbackQuery(i);
193
+ let e = this.searchLinearAllFields(this.dataset, n, null, r);
194
+ return this.persistSearchResult(e, n, a), e.items;
195
+ }
196
+ let u = l();
197
+ if (u === null) {
198
+ this.recordFallbackQuery(i);
199
+ let e = this.searchLinearAllFields(this.dataset, n, null, r);
200
+ return this.persistSearchResult(e, n, a), e.items;
201
+ }
202
+ this.recordIndexedQuery();
203
+ let d = this.searchAllFieldsIndexed(n, u, r, null);
204
+ return this.persistSearchResult(d, n, a), d.items;
205
+ }
206
+ searchAllFieldsIndexed(e, t, n, r, i = null) {
207
+ if (i !== null && !this.nestedCollection.hasRegisteredFields() && this.flatIndexes.size > 0) return this.searchAllFieldsIndexedInCandidates(e, t, n, r, i);
208
+ let a = this.dataset, o = new Uint8Array(a.length), s = [], c = 0;
209
+ for (let a of this.flatIndexes.keys()) {
210
+ let l = this.searchFieldWithPreparedQueryIndices(a, e, t, r, i);
211
+ for (let e = 0; e < l.length; e++) {
212
+ let t = l[e];
213
+ if (!o[t]) {
214
+ if (o[t] = 1, c < n.offset) {
215
+ c += 1;
216
+ continue;
217
+ }
218
+ if (s.push(t), c += 1, this.hasReachedWindowLimit(n, s.length)) return this.materializeIndicesResult(s);
219
+ }
220
+ }
221
+ }
222
+ for (let a of this.nestedCollection.searchAllIndexedFieldIndices(e, t, r, i)) if (!o[a]) {
223
+ if (o[a] = 1, c < n.offset) {
224
+ c += 1;
225
+ continue;
226
+ }
227
+ if (s.push(a), c += 1, this.hasReachedWindowLimit(n, s.length)) return this.materializeIndicesResult(s);
228
+ }
229
+ return this.materializeIndicesResult(s);
230
+ }
231
+ searchAllFieldsIndexedInCandidates(e, t, n, r, a) {
232
+ let o = [];
233
+ for (let n of this.flatIndexes.keys()) {
234
+ let r = this.getResolvedFlatIndex(n);
235
+ if (!r) continue;
236
+ let a = i(r.ngramMap, t, r.normalizedValues, e);
237
+ a !== null && o.push(a.matches);
238
+ }
239
+ if (o.length === 0) return {
240
+ items: [],
241
+ indices: []
242
+ };
243
+ let s = [], c = 0;
244
+ for (let e = 0; e < a.length; e++) {
245
+ let t = a[e];
246
+ if (r !== null && !r[t]) continue;
247
+ let i = !1;
248
+ for (let e = 0; e < o.length; e++) if (o[e](t)) {
249
+ i = !0;
250
+ break;
251
+ }
252
+ if (i) {
253
+ if (c < n.offset) {
254
+ c += 1;
255
+ continue;
256
+ }
257
+ if (s.push(t), c += 1, this.hasReachedWindowLimit(n, s.length)) break;
258
+ }
259
+ }
260
+ return this.materializeIndicesResult(s);
261
+ }
262
+ searchField(e, t, n) {
263
+ let r = this.normalizeQuery(t), i = this.normalizeSearchWindow(n), a = `field "${e}"`;
264
+ if (i.limit === 0) return [];
265
+ if (!r || r.length < this.minQueryLength) return this.sliceItems(this.dataset, i);
266
+ r.length >= 2 && this.ensureConfiguredIndexesReady();
267
+ 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);
268
+ if (s !== null) {
269
+ let t = d();
270
+ if (l) {
271
+ if (t !== null && this.nestedCollection.hasIndexes()) {
272
+ this.recordIndexedQuery();
273
+ let n = this.nestedCollection.searchIndexedFieldIndices(e, r, t, c, s, i.take), a = this.collectItemsFromIndices(n, i);
274
+ return this.persistSearchResult(a, r, o), a.items;
275
+ }
276
+ this.recordFallbackQuery(a);
277
+ let n = this.searchLinearSingleField(this.dataset, e, r, s, i);
278
+ return this.persistSearchResult(n, r, o), n.items;
279
+ }
280
+ if (t !== null && this.flatIndexes.has(e)) {
281
+ this.recordIndexedQuery();
282
+ let n = this.searchFieldWithPreparedQuery(e, r, t, i, c, s);
283
+ return this.persistSearchResult(n, r, o), n.items;
284
+ }
285
+ this.recordFallbackQuery(a);
286
+ let n = this.searchLinearSingleField(this.dataset, e, r, s, i);
287
+ return this.persistSearchResult(n, r, o), n.items;
288
+ }
289
+ if (l) {
290
+ if (r.length >= 2 && this.nestedCollection.hasIndexes()) {
291
+ let t = this.getQueryGrams(r);
292
+ if (t === null) return [];
293
+ this.recordIndexedQuery();
294
+ let n = this.nestedCollection.searchIndexedFieldIndices(e, r, t, null, null, i.take), a = this.collectItemsFromIndices(n, i);
295
+ return this.persistSearchResult(a, r, o), a.items;
296
+ }
297
+ this.recordFallbackQuery(a);
298
+ let t = this.searchLinearSingleField(this.dataset, e, r, null, i);
299
+ return this.persistSearchResult(t, r, o), t.items;
300
+ }
301
+ 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");
302
+ let f = d();
303
+ if (this.flatIndexes.size > 0 && f !== null) {
304
+ if (!this.flatIndexes.has(e)) return [];
305
+ this.recordIndexedQuery();
306
+ let t = this.searchFieldWithPreparedQuery(e, r, f, i);
307
+ return this.persistSearchResult(t, r, o), t.items;
308
+ }
309
+ this.recordFallbackQuery(a);
310
+ let p = this.searchLinearSingleField(this.dataset, e, r, null, i);
311
+ return this.persistSearchResult(p, r, o), p.items;
312
+ }
313
+ searchFieldWithPreparedQuery(e, t, n, r, i = null, a = null) {
314
+ let o = this.searchFieldWithPreparedQueryIndices(e, t, n, i, a, r.take);
315
+ return this.collectItemsFromIndices(o, r);
316
+ }
317
+ searchFieldWithPreparedQueryIndices(e, t, n, r = null, i = null, a = Infinity) {
318
+ let c = this.getResolvedFlatIndex(e);
319
+ if (!c) return [];
320
+ let { ngramMap: l, normalizedValues: u } = c;
321
+ return i === null ? o(l, n, u, t, {
322
+ restrictionLookup: r,
323
+ take: a
324
+ }) : s(l, n, u, t, {
325
+ candidateIndices: i,
326
+ restrictionLookup: r,
327
+ take: a
328
+ });
329
+ }
330
+ getIndexedFieldsList() {
331
+ return this.cachedIndexedFieldsList === null && (this.cachedIndexedFieldsList = Array.from(this.indexedFields)), this.cachedIndexedFieldsList;
332
+ }
333
+ materializeIndicesResult(e) {
334
+ let t = [];
335
+ for (let n = 0; n < e.length; n++) {
336
+ let r = this.dataset[e[n]];
337
+ r && t.push(r);
338
+ }
339
+ return {
340
+ items: t,
341
+ indices: e
342
+ };
343
+ }
344
+ getLinearSearchFields(e) {
345
+ let t = this.getIndexedFieldsList();
346
+ 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);
347
+ }
348
+ buildNormalizedValuesOnly(e, t) {
349
+ let n = Array(e.length);
350
+ for (let r = 0, i = e.length; r < i; r++) {
351
+ let i = e[r][t];
352
+ typeof i == "string" && (n[r] = i.toLowerCase());
353
+ }
354
+ return this.normalizedValuesCache.set(t, n), n;
355
+ }
356
+ buildCombinedNormalizedValues(e, t) {
357
+ let n = Array(e.length);
358
+ for (let r = 0; r < e.length; r++) n[r] = this.buildCombinedNormalizedValue(e[r], t);
359
+ return this.combinedNormalizedValuesCache = {
360
+ fieldsKey: t.join("\0"),
361
+ values: n
362
+ }, n;
363
+ }
364
+ buildCombinedNormalizedValue(e, t) {
365
+ let n = "";
366
+ for (let r = 0; r < t.length; r++) {
367
+ let i = e[t[r]];
368
+ typeof i == "string" && (n && (n += "\n"), n += i.toLowerCase());
369
+ }
370
+ return n;
371
+ }
372
+ getCombinedNormalizedValues(e, t) {
373
+ if (e !== this.dataset) return null;
374
+ let n = t.join("\0");
375
+ return this.combinedNormalizedValuesCache?.fieldsKey === n ? this.combinedNormalizedValuesCache.values : this.buildCombinedNormalizedValues(e, t);
376
+ }
377
+ invalidateNormalizedValuesCacheEntry(e, t) {
378
+ for (let [n, r] of this.normalizedValuesCache) {
379
+ let i = t[n];
380
+ r[e] = typeof i == "string" ? i.toLowerCase() : "";
381
+ }
382
+ if (this.combinedNormalizedValuesCache !== null) {
383
+ let n = this.combinedNormalizedValuesCache.fieldsKey.split("\0");
384
+ this.combinedNormalizedValuesCache.values[e] = this.buildCombinedNormalizedValue(t, n);
385
+ }
386
+ }
387
+ searchLinearAllFields(e, t, n, r) {
388
+ if (!e.length) return {
389
+ items: [],
390
+ indices: []
391
+ };
392
+ let i = this.getLinearSearchFields(e), a = e === this.dataset, o = i.length, s = Array(o);
393
+ for (let t = 0; t < o; t++) {
394
+ let n = i[t], r = this.flatIndexes.get(n);
395
+ if (r) s[t] = r.normalizedValues;
396
+ else {
397
+ let r = this.normalizedValuesCache.get(n);
398
+ r ? s[t] = r : a ? s[t] = this.buildNormalizedValuesOnly(e, n) : s[t] = null;
399
+ }
400
+ }
401
+ let c = this.nestedCollection.hasRegisteredFields(), l = c ? null : this.getCombinedNormalizedValues(e, i), u = !c && s.every((e) => e !== null), d = [], f = 0;
402
+ if (n !== null) {
403
+ let e = this.dataset;
404
+ if (l !== null) {
405
+ for (let e = 0; e < n.length; e++) {
406
+ let i = n[e];
407
+ if (l[i]?.includes(t)) {
408
+ if (f < r.offset) {
409
+ f += 1;
410
+ continue;
411
+ }
412
+ if (d.push(i), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
413
+ }
414
+ }
415
+ return this.materializeIndicesResult(d);
416
+ }
417
+ if (u) {
418
+ for (let e = 0; e < n.length; e++) {
419
+ let i = n[e], a = !1;
420
+ for (let e = 0; e < o; e++) {
421
+ let n = s[e][i];
422
+ if (n && n.includes(t)) {
423
+ a = !0;
424
+ break;
425
+ }
426
+ }
427
+ if (a) {
428
+ if (f < r.offset) {
429
+ f += 1;
430
+ continue;
431
+ }
432
+ if (d.push(i), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
433
+ }
434
+ }
435
+ return this.materializeIndicesResult(d);
436
+ }
437
+ for (let a = 0; a < n.length; a++) {
438
+ let l = n[a], u = e[l], p = !1;
439
+ for (let e = 0; e < o; e++) {
440
+ let n = s[e];
441
+ if (n) {
442
+ let e = n[l];
443
+ if (e && e.includes(t)) {
444
+ p = !0;
445
+ break;
446
+ }
447
+ } else {
448
+ let n = u[i[e]];
449
+ if (typeof n == "string" && n.toLowerCase().includes(t)) {
450
+ p = !0;
451
+ break;
452
+ }
453
+ }
454
+ }
455
+ if (!p && c && (p = this.nestedCollection.matchesAnyField(u, t)), p) {
456
+ if (f < r.offset) {
457
+ f += 1;
458
+ continue;
459
+ }
460
+ if (d.push(l), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
461
+ }
462
+ }
463
+ return this.materializeIndicesResult(d);
464
+ }
465
+ if (l !== null) {
466
+ for (let n = 0; n < e.length; n++) if (l[n]?.includes(t)) {
467
+ if (f < r.offset) {
468
+ f += 1;
469
+ continue;
470
+ }
471
+ if (d.push(n), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
472
+ }
473
+ return this.materializeIndicesResult(d);
474
+ }
475
+ if (u) {
476
+ for (let n = 0; n < e.length; n++) {
477
+ let e = !1;
478
+ for (let r = 0; r < o; r++) {
479
+ let i = s[r][n];
480
+ if (i && i.includes(t)) {
481
+ e = !0;
482
+ break;
483
+ }
484
+ }
485
+ if (e) {
486
+ if (f < r.offset) {
487
+ f += 1;
488
+ continue;
489
+ }
490
+ if (d.push(n), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
491
+ }
492
+ }
493
+ return this.materializeIndicesResult(d);
494
+ }
495
+ for (let n = 0; n < e.length; n++) {
496
+ let a = e[n], l = !1;
497
+ for (let e = 0; e < o; e++) {
498
+ let r = s[e];
499
+ if (r) {
500
+ let e = r[n];
501
+ if (e && e.includes(t)) {
502
+ l = !0;
503
+ break;
504
+ }
505
+ } else {
506
+ let n = a[i[e]];
507
+ if (typeof n == "string" && n.toLowerCase().includes(t)) {
508
+ l = !0;
509
+ break;
510
+ }
511
+ }
512
+ }
513
+ if (!l && c && (l = this.nestedCollection.matchesAnyField(a, t)), l) {
514
+ if (f < r.offset) {
515
+ f += 1;
516
+ continue;
517
+ }
518
+ if (d.push(n), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
519
+ }
520
+ }
521
+ return this.materializeIndicesResult(d);
522
+ }
523
+ searchLinearSingleField(e, t, n, r, i) {
524
+ if (!e.length) return {
525
+ items: [],
526
+ indices: []
527
+ };
528
+ if (this.nestedCollection.hasField(t)) {
529
+ let a = this.nestedCollection.searchFieldLinearIndices(e, t, n, r ?? void 0);
530
+ return this.collectItemsFromIndices(a, i);
531
+ }
532
+ 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;
533
+ if (r !== null) {
534
+ let e = this.dataset;
535
+ if (s) {
536
+ for (let e = 0; e < r.length; e++) {
537
+ let t = r[e];
538
+ if (s[t]?.includes(n)) {
539
+ if (c < i.offset) {
540
+ c += 1;
541
+ continue;
542
+ }
543
+ if (a.push(t), c += 1, this.hasReachedWindowLimit(i, a.length)) break;
544
+ }
545
+ }
546
+ return this.materializeIndicesResult(a);
547
+ }
548
+ for (let o = 0; o < r.length; o++) {
549
+ let s = r[o], l = e[s];
550
+ if (typeof l[t] == "string" && l[t].toLowerCase().includes(n)) {
551
+ if (c < i.offset) {
552
+ c += 1;
553
+ continue;
554
+ }
555
+ if (a.push(s), c += 1, this.hasReachedWindowLimit(i, a.length)) break;
556
+ }
557
+ }
558
+ return this.materializeIndicesResult(a);
559
+ }
560
+ if (s) {
561
+ for (let t = 0; t < e.length; t++) if (s[t]?.includes(n)) {
562
+ if (c < i.offset) {
563
+ c += 1;
564
+ continue;
565
+ }
566
+ if (a.push(t), c += 1, this.hasReachedWindowLimit(i, a.length)) break;
567
+ }
568
+ return this.materializeIndicesResult(a);
569
+ }
570
+ for (let r = 0; r < e.length; r++) if (typeof e[r][t] == "string" && e[r][t].toLowerCase().includes(n)) {
571
+ if (c < i.offset) {
572
+ c += 1;
573
+ continue;
574
+ }
575
+ if (a.push(r), c += 1, this.hasReachedWindowLimit(i, a.length)) break;
576
+ }
577
+ return this.materializeIndicesResult(a);
578
+ }
579
+ clearIndexes() {
580
+ return this.flatIndexes.clear(), this.nestedCollection.clearIndexes(), this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this;
581
+ }
582
+ getOriginData() {
583
+ return this.state.getOriginData();
584
+ }
585
+ data(e) {
586
+ return this.state.data(e), this;
587
+ }
588
+ add(e) {
589
+ return this.state.add(e), this;
590
+ }
591
+ update(e) {
592
+ return this.state.update(e), this;
593
+ }
594
+ applyAddedItems(e, t) {
595
+ if (e.length === 0) return this;
596
+ for (let n of this.indexedFields) this.addItemsToField(n, e, t);
597
+ return this.nestedCollection.addItems(e, t), this;
598
+ }
599
+ clearData() {
600
+ return this.state.clearData(), this;
601
+ }
602
+ handleStateMutation(e) {
603
+ if (this.shouldDeferMutationIndexUpdates() && (e.type === "add" || e.type === "update")) {
604
+ this.markDeferredMutationState();
605
+ return;
606
+ }
607
+ switch (e.type) {
608
+ case "add":
609
+ this.applyAddedItems(e.items, e.startIndex);
610
+ for (let [t, n] of this.normalizedValuesCache) for (let r = 0; r < e.items.length; r++) {
611
+ let i = e.items[r][t];
612
+ n[e.startIndex + r] = typeof i == "string" ? i.toLowerCase() : "";
613
+ }
614
+ this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
615
+ return;
616
+ case "update":
617
+ this.applyUpdatedItem(e.index, e.previousItem, e.nextItem), this.invalidateNormalizedValuesCacheEntry(e.index, e.nextItem), this.clearPreviousSearchState();
618
+ return;
619
+ case "data":
620
+ this.runtime.deferredMutationVersion = null, this.rebuildConfiguredIndexes(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
621
+ return;
622
+ case "clearData":
623
+ this.runtime.deferredMutationVersion = null, this.flatIndexes.clear(), this.nestedCollection.clearIndexes(), this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
624
+ return;
625
+ case "remove":
626
+ this.applyRemovedItem(e.removedItem, e.removedIndex, e.movedItem, e.movedFromIndex), this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
627
+ return;
628
+ case "removeMany":
629
+ for (let t = 0; t < e.entries.length; t++) {
630
+ let n = e.entries[t];
631
+ this.applyRemovedItem(n.removedItem, n.removedIndex, n.movedItem, n.movedFromIndex);
632
+ }
633
+ this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
634
+ return;
635
+ }
636
+ }
637
+ addItemsToField(e, t, n) {
638
+ let r = this.flatIndexes.get(e);
639
+ if (!r) return;
640
+ let { ngramMap: i, normalizedValues: o } = r;
641
+ for (let r = 0; r < t.length; r++) {
642
+ let s = t[r][e];
643
+ if (typeof s != "string") continue;
644
+ let c = s.toLowerCase(), l = n + r;
645
+ o[l] = c, a(i, c, l);
646
+ }
647
+ r.version = this.state.getMutationVersion();
648
+ }
649
+ applyUpdatedItem(e, t, n) {
650
+ for (let r of this.indexedFields) this.updateIndexedField(r, e, t, n);
651
+ this.nestedCollection.updateItem(n, t, e);
652
+ }
653
+ applyRemovedItem(e, t, n, r) {
654
+ for (let i of this.indexedFields) this.removeIndexedFieldValue(i, e, t), n !== null && r !== null && this.moveIndexedFieldValue(i, n, r, t);
655
+ this.nestedCollection.removeItem(e, t), n !== null && r !== null && this.nestedCollection.moveItem(n, r, t);
656
+ }
657
+ updateIndexedField(e, t, n, r) {
658
+ let i = this.flatIndexes.get(e);
659
+ if (!i) return;
660
+ if (n[e] === r[e]) {
661
+ i.version = this.state.getMutationVersion();
662
+ return;
663
+ }
664
+ let { ngramMap: o, normalizedValues: s } = i, l = this.getNormalizedFieldValue(n, e);
665
+ l && c(o, l, t);
666
+ let u = this.getNormalizedFieldValue(r, e);
667
+ if (!u) {
668
+ delete s[t], i.version = this.state.getMutationVersion();
669
+ return;
670
+ }
671
+ s[t] = u, a(o, u, t), i.version = this.state.getMutationVersion();
672
+ }
673
+ removeIndexedFieldValue(e, t, n) {
674
+ let r = this.flatIndexes.get(e);
675
+ if (!r) return;
676
+ let { ngramMap: i, normalizedValues: a } = r, o = this.getNormalizedFieldValue(t, e);
677
+ o && c(i, o, n), delete a[n], r.version = this.state.getMutationVersion();
678
+ }
679
+ moveIndexedFieldValue(e, t, n, r) {
680
+ if (n === r) return;
681
+ let i = this.flatIndexes.get(e);
682
+ if (!i) return;
683
+ let { ngramMap: o, normalizedValues: s } = i, l = s[n] ?? this.getNormalizedFieldValue(t, e);
684
+ if (!l) {
685
+ delete s[n], i.version = this.state.getMutationVersion();
686
+ return;
687
+ }
688
+ c(o, l, n), a(o, l, r), s[r] = l, delete s[n], i.version = this.state.getMutationVersion();
689
+ }
690
+ getNormalizedFieldValue(e, t) {
691
+ let n = e[t];
692
+ return typeof n == "string" ? n.toLowerCase() : null;
693
+ }
694
+ };
695
+ //#endregion
696
+ export { d as TextSearchEngine };