@nosto/search-js 1.4.0 → 1.5.0

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 (69) hide show
  1. package/README.md +3 -39
  2. package/dist/core/core.d.ts +2 -2
  3. package/dist/core/src/applyDecorators.d.ts +9 -9
  4. package/dist/core/src/search.d.ts +9 -9
  5. package/dist/core/src/searchWithRetries.d.ts +2 -2
  6. package/dist/core/src/types.d.ts +5 -6
  7. package/dist/currencies/currencies.d.ts +2 -2
  8. package/dist/currencies/src/getCurrencyFormatting.d.ts +2 -3
  9. package/dist/currencies/src/priceDecorator.d.ts +2 -2
  10. package/dist/preact/preact.cjs.js +1 -1
  11. package/dist/preact/preact.d.ts +24 -5
  12. package/dist/preact/preact.es.js +741 -100
  13. package/dist/preact/src/actions/newSearch.d.ts +4 -0
  14. package/dist/preact/src/actions/replaceFilter.d.ts +3 -0
  15. package/dist/preact/src/actions/toggleProductFilter.d.ts +2 -0
  16. package/dist/preact/src/actions/types.d.ts +6 -0
  17. package/dist/preact/src/actions/updateSearch.d.ts +4 -0
  18. package/dist/preact/src/api/types.d.ts +4 -0
  19. package/dist/preact/src/components/SearchInput.d.ts +7 -0
  20. package/dist/preact/src/components/SerpElement.d.ts +18 -0
  21. package/dist/preact/src/config/base/baseConfig.d.ts +41 -0
  22. package/dist/preact/src/config/config.d.ts +4 -0
  23. package/dist/preact/src/config/configContext.d.ts +6 -0
  24. package/dist/preact/src/config/pages/autocomplete/config.d.ts +15 -0
  25. package/dist/preact/src/config/pages/autocomplete/provider.d.ts +10 -0
  26. package/dist/preact/src/config/pages/category/config.d.ts +44 -0
  27. package/dist/preact/src/config/pages/category/provider.d.ts +10 -0
  28. package/dist/preact/src/config/pages/serp/config.d.ts +34 -0
  29. package/dist/preact/src/config/pages/serp/provider.d.ts +10 -0
  30. package/dist/preact/src/dom/renderHeadless.d.ts +18 -0
  31. package/dist/preact/src/dom/utils.d.ts +5 -0
  32. package/dist/preact/src/hooks/useActions.d.ts +32 -0
  33. package/dist/preact/src/hooks/useCheckClientScript.d.ts +1 -0
  34. package/dist/preact/src/hooks/useDecoratedSearchResults.d.ts +27 -0
  35. package/dist/preact/src/hooks/useFacet.d.ts +60 -0
  36. package/dist/preact/src/hooks/useFacets.d.ts +39 -0
  37. package/dist/preact/src/hooks/useNostoAppState.d.ts +3 -2
  38. package/dist/preact/src/hooks/usePersonalization.d.ts +1 -1
  39. package/dist/preact/src/hooks/useProductFilters/useProductFilters.d.ts +59 -0
  40. package/dist/preact/src/hooks/useProductFilters/useProductFiltersUtils.d.ts +48 -0
  41. package/dist/preact/src/hooks/useRange.d.ts +34 -0
  42. package/dist/preact/src/hooks/useRangeSelector.d.ts +98 -0
  43. package/dist/preact/src/hooks/useResponse.d.ts +82 -0
  44. package/dist/preact/src/hooks/useSelectedFiltersCount.d.ts +17 -0
  45. package/dist/preact/src/hooks/useSizeOptions.d.ts +50 -0
  46. package/dist/preact/src/hooks/useSort/useSort.d.ts +47 -0
  47. package/dist/preact/src/hooks/useSort/utils.d.ts +9 -0
  48. package/dist/preact/src/search/defaults.d.ts +45 -0
  49. package/dist/preact/src/search/resultCaching.d.ts +4 -0
  50. package/dist/preact/src/store.d.ts +9 -1387
  51. package/dist/preact/src/storeContext.d.ts +5 -1389
  52. package/dist/preact/src/storeExtensions.d.ts +17 -0
  53. package/dist/thumbnails/src/generateThumbnailUrl.d.ts +1 -1
  54. package/dist/thumbnails/src/nostoThumbnailDecorator.d.ts +2 -2
  55. package/dist/thumbnails/src/shopifyThumbnailDecorator.d.ts +2 -2
  56. package/dist/thumbnails/src/thumbnailDecorator.d.ts +2 -2
  57. package/dist/thumbnails/thumbnails.d.ts +5 -5
  58. package/dist/utils/src/deepFreeze.d.ts +1 -1
  59. package/dist/utils/src/deepMerge.d.ts +5 -3
  60. package/dist/utils/src/mergeArrays.d.ts +2 -1
  61. package/dist/utils/src/parseNumber.d.ts +1 -0
  62. package/dist/utils/src/performance.d.ts +1 -0
  63. package/dist/utils/src/savePageScroll.d.ts +2 -0
  64. package/dist/utils/src/simplify.d.ts +1 -1
  65. package/dist/utils/src/storage.d.ts +6 -0
  66. package/dist/utils/src/types.d.ts +3 -0
  67. package/dist/utils/src/unique.d.ts +1 -0
  68. package/package.json +22 -15
  69. package/dist/preact/test/mocks/mocks.d.ts +0 -1384
@@ -1,133 +1,774 @@
1
- import { options as m, createContext as b } from "preact";
2
- import { useContext as A, useState as S, useEffect as h } from "preact/hooks";
3
- import { s as k } from "../index.es-Bcd5IQh9.js";
4
- var q = 0;
5
- function x(t, e, n, r, o, i) {
6
- e || (e = {});
7
- var u, s, c = e;
8
- if ("ref" in c) for (s in c = {}, e) s == "ref" ? u = e[s] : c[s] = e[s];
9
- var p = { type: t, props: c, key: n, ref: u, __k: null, __: null, __b: 0, __e: null, __c: null, constructor: void 0, __v: --q, __i: -1, __u: 0, __source: o, __self: i };
10
- if (typeof t == "function" && (u = t.defaultProps)) for (s in u) c[s] === void 0 && (c[s] = u[s]);
11
- return m.vnode && m.vnode(p), p;
12
- }
13
- const d = (t) => String(t) === "[object Object]";
14
- function l(t) {
15
- if (!d(t))
1
+ import { toChildArray as X, createContext as L, options as B } from "preact";
2
+ import { s as O } from "../index.es-Bcd5IQh9.js";
3
+ import { useContext as q, useEffect as z, useMemo as N, useCallback as h, useState as R } from "preact/hooks";
4
+ import { search as Z } from "../core/core.es.js";
5
+ function ee(e) {
6
+ return e.type === "input";
7
+ }
8
+ function te(e) {
9
+ return !!e && typeof e == "object" && "type" in e && "props" in e;
10
+ }
11
+ function V(e, t = { depth: 0 }) {
12
+ const { children: r } = e;
13
+ return X(r).map((n) => {
14
+ if (!te(n))
15
+ return n;
16
+ const o = e.updateElement(n, t);
17
+ return o === null ? null : (o.props = {
18
+ ...o.props,
19
+ children: V({ ...e, children: o.props.children }, { depth: t.depth + 1 })
20
+ }, o);
21
+ });
22
+ }
23
+ function Ve({ children: e, onSearchInput: t }) {
24
+ return V({
25
+ children: e,
26
+ updateElement: (r) => (!ee(r) || r.props.type !== "search" || (r.props = {
27
+ ...r.props,
28
+ onInput: (n) => {
29
+ n.target instanceof HTMLInputElement && t(n.target);
30
+ }
31
+ }), r)
32
+ });
33
+ }
34
+ const M = L(null), G = () => {
35
+ const e = q(M);
36
+ if (!e)
37
+ throw new Error("useConfig must be used within a ConfigProvider");
38
+ return e;
39
+ }, re = "nosto:search:scrollPos";
40
+ function ne() {
41
+ window.sessionStorage.setItem(re, window.scrollY.toString());
42
+ }
43
+ function $e({ children: e, hit: t, onClick: r }) {
44
+ const { pageType: n } = G(), o = n === "autocomplete" ? void 0 : n;
45
+ return V({
46
+ children: e,
47
+ updateElement: (u, c) => (c.depth > 0 || (u.props = {
48
+ ...u.props,
49
+ onClick: (i) => {
50
+ t && o && O((l) => l.recordSearchClick(o, t)), ne(), typeof r == "function" && r(i);
51
+ }
52
+ }), u)
53
+ });
54
+ }
55
+ var oe = 0;
56
+ function w(e, t, r, n, o, u) {
57
+ t || (t = {});
58
+ var c, i, l = t;
59
+ if ("ref" in l) for (i in l = {}, t) i == "ref" ? c = t[i] : l[i] = t[i];
60
+ var s = { type: e, props: l, key: r, ref: c, __k: null, __: null, __b: 0, __e: null, __c: null, constructor: void 0, __v: --oe, __i: -1, __u: 0, __source: o, __self: u };
61
+ if (typeof e == "function" && (c = e.defaultProps)) for (i in c) l[i] === void 0 && (l[i] = c[i]);
62
+ return B.vnode && B.vnode(s), s;
63
+ }
64
+ function $() {
65
+ z(() => {
66
+ const e = window.setTimeout(() => {
67
+ console.error("Nosto client script has not loaded after 3 seconds.");
68
+ }, 3e3);
69
+ O(() => {
70
+ window.clearTimeout(e);
71
+ });
72
+ }, []);
73
+ }
74
+ const K = (e) => String(e) === "[object Object]";
75
+ function C(e) {
76
+ if (!K(e))
16
77
  return !1;
17
- const e = t.constructor;
18
- if (e === void 0)
78
+ const t = e.constructor;
79
+ if (t === void 0)
19
80
  return !0;
20
- const n = e.prototype;
21
- return !(!d(n) || !n.hasOwnProperty("isPrototypeOf"));
81
+ const r = t.prototype;
82
+ return !(!K(r) || !r.hasOwnProperty("isPrototypeOf"));
22
83
  }
23
- const j = (t) => {
24
- const e = Object.entries(t).reduce((n, [r, o]) => {
25
- const i = l(o) ? j(o) : Object.freeze(o);
26
- return {
27
- ...n,
28
- [r]: i
29
- };
30
- }, {});
31
- return Object.freeze(e);
32
- };
33
- function w(...t) {
34
- return t.reduce((e, n) => v(e, n));
84
+ function E(...e) {
85
+ return e.reduce((t, r) => j(t, r));
86
+ }
87
+ function Q(...e) {
88
+ return e.reduce((t, r) => j(t, r));
35
89
  }
36
- function v(t, e) {
37
- return l(t) && l(e) ? Object.entries(e).reduce(
38
- (n, [r, o]) => (n[r] = v(n[r], o), n),
39
- { ...t }
40
- ) : e;
90
+ function j(e, t) {
91
+ return C(e) && C(t) ? Object.entries(t).reduce(
92
+ (r, [n, o]) => (r[n] = j(r[n], o), r),
93
+ { ...e }
94
+ ) : C(e) ? e : t;
41
95
  }
42
- function _(t, e) {
43
- if (t === e)
96
+ function P(e, t) {
97
+ if (e === t)
44
98
  return !0;
45
- if (t instanceof Date && e instanceof Date)
46
- return t.getTime() === e.getTime();
47
- if (Array.isArray(t) && Array.isArray(e))
48
- return t.length !== e.length ? !1 : t.every((n, r) => _(n, e[r]));
49
- if (l(t) && l(e)) {
50
- const n = Object.entries(t);
51
- return n.length !== Object.keys(e).length ? !1 : n.every(([r, o]) => _(o, e[r]));
99
+ if (e instanceof Date && t instanceof Date)
100
+ return e.getTime() === t.getTime();
101
+ if (Array.isArray(e) && Array.isArray(t))
102
+ return e.length !== t.length ? !1 : e.every((r, n) => P(r, t[n]));
103
+ if (C(e) && C(t)) {
104
+ const r = Object.entries(e);
105
+ return r.length !== Object.keys(t).length ? !1 : r.every(([n, o]) => P(o, t[n]));
52
106
  }
53
107
  return !1;
54
108
  }
55
- const O = {
109
+ function se(e, t = {}) {
110
+ const r = /* @__PURE__ */ new Map();
111
+ let n = Q(e, t ?? {});
112
+ const o = Q(e, t);
113
+ function u(a) {
114
+ n = a(n);
115
+ for (const d of r.values())
116
+ d(n);
117
+ }
118
+ function c(a) {
119
+ u((d) => ({ ...d, ...a }));
120
+ }
121
+ function i() {
122
+ return n;
123
+ }
124
+ function l() {
125
+ return structuredClone(o);
126
+ }
127
+ function s(a, d) {
128
+ let m;
129
+ r.set(d, (g) => {
130
+ const y = a(g);
131
+ P(y, m) || (m = y, d(y));
132
+ });
133
+ }
134
+ function f(a) {
135
+ r.delete(a);
136
+ }
137
+ return {
138
+ updateState: c,
139
+ getState: i,
140
+ getInitialState: l,
141
+ onChange: s,
142
+ clearOnChange: f
143
+ };
144
+ }
145
+ const ie = {
56
146
  loading: !0,
57
- pageType: void 0,
58
147
  query: {
59
148
  query: ""
60
149
  },
61
150
  response: {
62
151
  query: ""
63
152
  },
64
- initialized: !1,
65
- customParams: {}
153
+ initialized: !1
66
154
  };
67
- function z(t) {
68
- const e = /* @__PURE__ */ new Map();
69
- let n = t ? w(O, t) : O;
70
- const r = j(n);
71
- function o(f) {
72
- n = f(n);
73
- for (const a of e.values())
74
- a(n);
75
- }
76
- function i(f) {
77
- o((a) => ({ ...a, ...f }));
78
- }
79
- function u() {
80
- return n;
155
+ function I(e = {}) {
156
+ return se(ie, e);
157
+ }
158
+ const F = L(I()), D = {
159
+ defaultCurrency: "EUR",
160
+ queryModifications: (e) => e
161
+ }, ce = {
162
+ ...D
163
+ };
164
+ function ae(e) {
165
+ return {
166
+ pageType: "autocomplete",
167
+ ...ce,
168
+ ...e
169
+ };
170
+ }
171
+ function je({ config: e, store: t, children: r }) {
172
+ const n = t ?? I();
173
+ return $(), /* @__PURE__ */ w(M, { value: ae(e), children: /* @__PURE__ */ w(F, { value: n, children: r }) });
174
+ }
175
+ const ue = {
176
+ ...D,
177
+ persistentSearchCache: !1,
178
+ preservePageScroll: !1
179
+ };
180
+ function le(e) {
181
+ return {
182
+ pageType: "category",
183
+ ...ue,
184
+ ...e
185
+ };
186
+ }
187
+ function De({ config: e, store: t, children: r }) {
188
+ const n = t ?? I();
189
+ return $(), /* @__PURE__ */ w(M, { value: le(e), children: /* @__PURE__ */ w(F, { value: n, children: r }) });
190
+ }
191
+ const fe = {
192
+ ...D,
193
+ persistentSearchCache: !1,
194
+ preservePageScroll: !1
195
+ };
196
+ function de(e = {}) {
197
+ return {
198
+ pageType: "serp",
199
+ ...fe,
200
+ ...e
201
+ };
202
+ }
203
+ function Ue({ config: e, store: t, children: r }) {
204
+ const n = t ?? I();
205
+ return $(), /* @__PURE__ */ w(M, { value: de(e), children: /* @__PURE__ */ w(F, { value: n, children: r }) });
206
+ }
207
+ function ge(e) {
208
+ return Array.from(new Set(e));
209
+ }
210
+ function pe(e, t) {
211
+ return {
212
+ ...t,
213
+ products: {
214
+ facets: e === "autocomplete" ? void 0 : ["*"],
215
+ fields: me,
216
+ ...t.products
217
+ },
218
+ ...t.keywords ? {
219
+ keywords: {
220
+ ...t.keywords,
221
+ fields: ge([...ye, ...t.keywords.fields ?? []]),
222
+ highlight: t.keywords.highlight ?? {
223
+ preTag: "<strong>",
224
+ postTag: "</strong>"
225
+ }
226
+ }
227
+ } : {}
228
+ };
229
+ }
230
+ const me = [
231
+ "productId",
232
+ "url",
233
+ "name",
234
+ "imageUrl",
235
+ "imageHash",
236
+ "thumbUrl",
237
+ "description",
238
+ "brand",
239
+ "variantId",
240
+ "availability",
241
+ "price",
242
+ "priceText",
243
+ "categoryIds",
244
+ "categories",
245
+ "customFields.key",
246
+ "customFields.value",
247
+ "priceCurrencyCode",
248
+ "datePublished",
249
+ "listPrice",
250
+ "unitPricingBaseMeasure",
251
+ "unitPricingUnit",
252
+ "unitPricingMeasure",
253
+ "googleCategory",
254
+ "gtin",
255
+ "ageGroup",
256
+ "gender",
257
+ "condition",
258
+ "alternateImageUrls",
259
+ "ratingValue",
260
+ "reviewCount",
261
+ "inventoryLevel",
262
+ "skus.id",
263
+ "skus.name",
264
+ "skus.price",
265
+ "skus.listPrice",
266
+ "skus.priceText",
267
+ "skus.url",
268
+ "skus.imageUrl",
269
+ "skus.inventoryLevel",
270
+ "skus.customFields.key",
271
+ "skus.customFields.value",
272
+ "skus.availability",
273
+ "pid",
274
+ "onDiscount",
275
+ "extra.key",
276
+ "extra.value",
277
+ "saleable",
278
+ "available",
279
+ "tags1",
280
+ "tags2",
281
+ "tags3"
282
+ ], ye = ["keyword", "_redirect"];
283
+ function he(...e) {
284
+ if (!e.every((t) => t == null))
285
+ return e.filter((t) => Array.isArray(t)).reduce((t, r) => t.concat(r), []);
286
+ }
287
+ const J = "nosto.search.";
288
+ function Se(e) {
289
+ var r;
290
+ const t = `${J}${e}`;
291
+ (r = window.performance) == null || r.mark(`${t}.start`);
292
+ }
293
+ function ve(e) {
294
+ var r, n;
295
+ const t = `${J}${e}`;
296
+ (r = window.performance) == null || r.mark(`${t}.end`), (n = window.performance) == null || n.measure(t, `${t}.start`, `${t}.end`);
297
+ }
298
+ function T(e) {
299
+ return Se(e), () => ve(e);
300
+ }
301
+ function Ce(e, t, r) {
302
+ const n = JSON.stringify(t);
303
+ try {
304
+ console.info("Setting storage", e, n), r.setItem(e, n);
305
+ } catch (o) {
306
+ console.warn(o);
81
307
  }
82
- function s() {
83
- return r;
308
+ }
309
+ function we(e, t) {
310
+ try {
311
+ const r = t.getItem(e);
312
+ if (r)
313
+ return JSON.parse(r);
314
+ } catch (r) {
315
+ console.warn(r);
84
316
  }
85
- function c(f, a) {
86
- let y;
87
- e.set(a, (P) => {
88
- const g = f(P);
89
- _(g, y) || (y = g, a(g));
317
+ }
318
+ function ke(e, t) {
319
+ Ce(e, t, sessionStorage);
320
+ }
321
+ function Pe(e) {
322
+ return we(e, sessionStorage);
323
+ }
324
+ const Y = "nosto:search:searchResult";
325
+ function Fe(e, t, r) {
326
+ if (!e)
327
+ return;
328
+ ke(Y, { query: t, result: r });
329
+ }
330
+ function Ae(e, t) {
331
+ if (!e)
332
+ return null;
333
+ const r = Pe(Y);
334
+ if (!r || !be(r))
335
+ return null;
336
+ const n = H(r.query);
337
+ return P(H(t), n) ? r.result : null;
338
+ }
339
+ function H(e) {
340
+ return {
341
+ accountId: e.accountId,
342
+ customRules: e.customRules,
343
+ explain: e.explain,
344
+ keywords: e.keywords,
345
+ products: e.products,
346
+ query: e.query,
347
+ redirect: e.redirect,
348
+ rules: e.rules,
349
+ segments: e.segments,
350
+ sessionParams: e.sessionParams
351
+ };
352
+ }
353
+ function be(e) {
354
+ return typeof e == "object" && e !== null && "query" in e && "result" in e;
355
+ }
356
+ async function W(e, t, r) {
357
+ var s, f, a;
358
+ const n = T("newSearch"), o = e.config.pageType, u = E(e.store.getInitialState().query, t), c = E(e.config.search, r, {
359
+ track: o,
360
+ redirect: o !== "autocomplete",
361
+ isKeyword: !!(r != null && r.isKeyword)
362
+ });
363
+ e.store.updateState({
364
+ query: u,
365
+ loading: !0,
366
+ initialized: !0
367
+ });
368
+ const i = o !== "autocomplete" && e.config.persistentSearchCache, l = e.config.queryModifications(
369
+ {
370
+ ...u,
371
+ products: {
372
+ ...u.products,
373
+ // Apply filter merging to avoid overwriting base filters
374
+ filter: he((f = (s = e.store.getInitialState().query) == null ? void 0 : s.products) == null ? void 0 : f.filter, (a = t.products) == null ? void 0 : a.filter)
375
+ }
376
+ },
377
+ o
378
+ );
379
+ try {
380
+ let d;
381
+ const m = Ae(i, l);
382
+ if (m)
383
+ d = m;
384
+ else {
385
+ const g = pe(o, l);
386
+ d = await Z(g, c), Fe(i, l, d);
387
+ }
388
+ e.store.updateState({
389
+ response: d,
390
+ loading: !1
90
391
  });
392
+ } catch (d) {
393
+ console.error("Search action failed", d);
91
394
  }
92
- function p(f) {
93
- e.delete(f);
94
- }
395
+ n();
396
+ }
397
+ async function U(e, t, r) {
398
+ const n = T("updateSearch"), o = E(e.store.getState().query, { products: { from: 0 } }, t);
399
+ await W(e, o, r), n();
400
+ }
401
+ async function Re(e, t, r) {
402
+ var c;
403
+ const n = T("replaceFilter"), o = (c = e.store.getState().query.products) == null ? void 0 : c.filter, u = r !== void 0 ? [{ field: t, [typeof r == "object" ? "range" : "value"]: [r] }] : [];
404
+ await U(e, {
405
+ products: {
406
+ filter: [...(o == null ? void 0 : o.filter((i) => i.field !== t)) ?? [], ...u]
407
+ }
408
+ }), n();
409
+ }
410
+ async function _e(e, t, r, n) {
411
+ var l, s;
412
+ const o = T("toggleProductFilter"), u = (l = e.store.getState().query.products) == null ? void 0 : l.filter, c = u == null ? void 0 : u.find((f) => f.value instanceof Array && f.field === t), i = c != null && c.value ? {
413
+ ...c,
414
+ value: [...c.value.filter((f) => !P(f, r)), ...n ? [r] : []]
415
+ } : n ? {
416
+ field: t,
417
+ value: [r]
418
+ } : void 0;
419
+ await U(e, {
420
+ products: {
421
+ filter: [...(u == null ? void 0 : u.filter((f) => f !== c)) ?? [], ...(s = i == null ? void 0 : i.value) != null && s.length ? [i] : []]
422
+ }
423
+ }), o();
424
+ }
425
+ function k() {
426
+ const e = G(), t = q(F), r = N(
427
+ () => ({
428
+ config: e,
429
+ store: t
430
+ }),
431
+ [e, t]
432
+ ), n = h(
433
+ (i, l) => W(r, i, l),
434
+ [r]
435
+ ), o = h(
436
+ (i, l) => U(r, i, l),
437
+ [r]
438
+ ), u = h(
439
+ (i, l, s) => _e(r, i, l, s),
440
+ [r]
441
+ ), c = h(
442
+ (i, l) => Re(r, i, l),
443
+ [r]
444
+ );
95
445
  return {
96
- updateState: i,
97
- getState: u,
98
- getInitialState: s,
99
- onChange: c,
100
- clearOnChange: p
446
+ newSearch: n,
447
+ updateSearch: o,
448
+ toggleProductFilter: u,
449
+ replaceFilter: c
101
450
  };
102
451
  }
103
- const C = b(z({}));
104
- function M({ store: t, children: e }) {
105
- return /* @__PURE__ */ x(C, { value: t, children: e });
452
+ function v(e = Ne) {
453
+ const t = q(F), [r, n] = R(e(t.getState()));
454
+ return t.onChange(e, n), z(() => () => t.clearOnChange(n), [t]), r;
455
+ }
456
+ const Ne = (e) => e;
457
+ function Be(e) {
458
+ const t = v((r) => r.response);
459
+ return e && Array.isArray(e) || e && C(e) ? e : t;
460
+ }
461
+ function Ke(e) {
462
+ var c;
463
+ const t = ((c = e.data) == null ? void 0 : c.filter((i) => i.selected).length) ?? 0, [r, n] = R(t > 0), { toggleProductFilter: o } = k();
464
+ return {
465
+ /** Active value */
466
+ active: r,
467
+ /** Selected filters count */
468
+ selectedFiltersCount: t,
469
+ /** Toggle active function */
470
+ toggleActive: () => {
471
+ n(!r);
472
+ },
473
+ /** Toggle product filter function */
474
+ toggleProductFilter: o
475
+ };
106
476
  }
107
- function V(t = T) {
108
- const e = A(C), [n, r] = S(t(e.getState()));
109
- return e.onChange(t, r), h(() => () => e.clearOnChange(r), [e]), n;
477
+ function Qe() {
478
+ const { loading: e, facets: t } = v((r) => {
479
+ var n, o;
480
+ return {
481
+ loading: r.loading,
482
+ facets: ((o = (n = r.response) == null ? void 0 : n.products) == null ? void 0 : o.facets) ?? []
483
+ };
484
+ });
485
+ return {
486
+ /** Loading state */
487
+ loading: e,
488
+ /** Array of facets*/
489
+ facets: t
490
+ };
110
491
  }
111
- const T = (t) => t;
112
- function F() {
113
- const [t, e] = S([]), [n, r] = S([]);
114
- return h(() => {
115
- k(async (o) => {
116
- const { products: i, segments: u } = await o.getSearchSessionParams();
117
- e(u ?? []), r((i == null ? void 0 : i.personalizationBoost) ?? []);
492
+ function He() {
493
+ const [e, t] = R([]), [r, n] = R([]);
494
+ return z(() => {
495
+ O(async (o) => {
496
+ const { products: u, segments: c } = await o.getSearchSessionParams();
497
+ t(c ?? []), n((u == null ? void 0 : u.personalizationBoost) ?? []);
118
498
  });
119
499
  }, []), {
120
- segments: t,
121
- boost: n
500
+ segments: e,
501
+ boost: r
502
+ };
503
+ }
504
+ function Me() {
505
+ const { facets: e } = v((s) => {
506
+ var f;
507
+ return {
508
+ facets: ((f = s.response.products) == null ? void 0 : f.facets) ?? []
509
+ };
510
+ }), { replaceFilter: t, toggleProductFilter: r } = k(), n = h(
511
+ (s) => {
512
+ const f = e == null ? void 0 : e.find((a) => a.type === "stats" && a.field === s);
513
+ if (f && "min" in f && "max" in f)
514
+ return f;
515
+ },
516
+ [e]
517
+ ), o = h(
518
+ (s) => {
519
+ var f;
520
+ return ((f = e == null ? void 0 : e.find((a) => a.field === s)) == null ? void 0 : f.name) ?? s;
521
+ },
522
+ [e]
523
+ ), u = h((s) => "field" in s && (s.value instanceof Array || s.range instanceof Array), []), c = h((s) => {
524
+ var f;
525
+ return {
526
+ ...s,
527
+ range: (f = s.range) == null ? void 0 : f.map((a) => ({
528
+ gt: a.gt ? Number(a.gt) : a.gt,
529
+ gte: a.gte ? Number(a.gte) : a.gte,
530
+ lt: a.lt ? Number(a.lt) : a.lt,
531
+ lte: a.lte ? Number(a.lte) : a.lte
532
+ }))
533
+ };
534
+ }, []), i = h(
535
+ (s) => (s.value ?? []).map((a) => ({
536
+ value: a,
537
+ field: s.field,
538
+ name: o(s.field),
539
+ filter: c(s),
540
+ remove: () => {
541
+ r(s.field, a, !1);
542
+ }
543
+ })),
544
+ [c, o, r]
545
+ ), l = h(
546
+ (s) => (s.range ?? []).map((a) => {
547
+ var g, y;
548
+ const d = a.gte ?? a.gt ?? ((g = n(s.field)) == null ? void 0 : g.min), m = a.lte ?? a.lt ?? ((y = n(s.field)) == null ? void 0 : y.max);
549
+ if (d !== void 0 && m !== void 0)
550
+ return {
551
+ value: `${d} - ${m}`,
552
+ field: s.field,
553
+ name: o(s.field),
554
+ filter: c(s),
555
+ remove: () => {
556
+ t(s.field, void 0);
557
+ }
558
+ };
559
+ }).filter(Boolean),
560
+ [c, n, o, t]
561
+ );
562
+ return {
563
+ selectFilters: u,
564
+ toValueFilter: i,
565
+ toRangeFilter: l
566
+ };
567
+ }
568
+ function Ie() {
569
+ const { filter: e } = v((i) => {
570
+ var l;
571
+ return {
572
+ filter: ((l = i.query.products) == null ? void 0 : l.filter) ?? []
573
+ };
574
+ }), { updateSearch: t } = k(), { selectFilters: r, toValueFilter: n, toRangeFilter: o } = Me(), u = N(() => e ? e.filter(r).flatMap((i) => "value" in i ? n(i) : "range" in i ? o(i) : []).filter(Boolean) : [], [e, r, o, n]), c = h(() => {
575
+ t({
576
+ products: {
577
+ filter: []
578
+ }
579
+ });
580
+ }, [t]);
581
+ return {
582
+ /** Selected filters array. */
583
+ filters: u,
584
+ /** Should remove all selected filters. */
585
+ removeAll: c
586
+ };
587
+ }
588
+ function _(e) {
589
+ if (typeof e == "number")
590
+ return e;
591
+ if (typeof e != "string")
592
+ return;
593
+ const t = Number(e);
594
+ return isNaN(t) ? void 0 : t;
595
+ }
596
+ function Te(e) {
597
+ var a, d, m;
598
+ const { replaceFilter: t } = k(), { query: r, products: n } = v((g) => ({
599
+ query: g.query,
600
+ products: g.response.products
601
+ })), o = (a = n == null ? void 0 : n.facets) == null ? void 0 : a.find((g) => g.id === e);
602
+ if (!o)
603
+ return {
604
+ min: 0,
605
+ max: 0,
606
+ range: [0, 0],
607
+ updateRange: () => {
608
+ }
609
+ };
610
+ const u = (m = (d = r.products) == null ? void 0 : d.filter) == null ? void 0 : m.find((g) => g.field === o.field), c = u != null && u.range ? u.range[0] : void 0, i = typeof c == "object" && ("gte" in c || "lte" in c) ? [_(c.gte), _(c.lte)] : [void 0, void 0], l = "min" in o ? Math.floor(o.min) : 0, s = "max" in o ? Math.ceil(o.max) : 0, f = ([g, y]) => {
611
+ const p = g !== void 0 ? Math.floor(g) : void 0, S = y !== void 0 ? Math.ceil(y) : void 0, A = p !== void 0, b = S !== void 0;
612
+ (l === p || !A) && (s === S || !b) ? t(o.field, void 0) : (l === p || !A) && b ? t(o.field, {
613
+ lte: S.toString()
614
+ }) : (s === S || !b) && A ? t(o.field, {
615
+ gte: p.toString()
616
+ }) : A && b && t(o.field, {
617
+ gte: p.toString(),
618
+ lte: S.toString()
619
+ });
620
+ };
621
+ return {
622
+ /** Min value */
623
+ min: l,
624
+ /** Max value */
625
+ max: s,
626
+ /** Range value */
627
+ range: [i[0] ?? l, i[1] ?? s],
628
+ /** Update range function */
629
+ updateRange: f
630
+ };
631
+ }
632
+ function Le(e, t) {
633
+ const { min: r, max: n, range: o, updateRange: u } = Te(e), { filters: c } = Ie(), i = N(() => {
634
+ var y;
635
+ const a = c.find((p) => {
636
+ var S;
637
+ return (S = p == null ? void 0 : p.filter) == null ? void 0 : S.range;
638
+ });
639
+ let d = null;
640
+ if (a) {
641
+ const p = (y = a.filter.range) == null ? void 0 : y[0];
642
+ d = [_(p == null ? void 0 : p.gte), _(p == null ? void 0 : p.lte)];
643
+ }
644
+ const m = [];
645
+ let g = Math.floor(r / t) * t;
646
+ for (; g < n; ) {
647
+ const p = g + t, S = d && d[0] === g && d[1] === p;
648
+ m.push({
649
+ min: g,
650
+ max: p,
651
+ selected: S
652
+ }), g = p;
653
+ }
654
+ return m;
655
+ }, [c, r, n, t]), l = (a) => {
656
+ u([a, o[1]]);
657
+ }, s = (a) => {
658
+ u([o[0], a]);
659
+ }, f = r !== o[0] || n !== o[1];
660
+ return {
661
+ /** Minimum value */
662
+ min: r,
663
+ /** Maximum value */
664
+ max: n,
665
+ /** Range value */
666
+ range: o,
667
+ /** Update range function */
668
+ updateRange: u,
669
+ /** Ranges */
670
+ ranges: i,
671
+ /** Handle min change */
672
+ handleMinChange: l,
673
+ /** Handle max change */
674
+ handleMaxChange: s,
675
+ /** Is selected */
676
+ isSelected: f
677
+ };
678
+ }
679
+ function Ge() {
680
+ const { products: e, keywords: t } = v((r) => r.response);
681
+ return {
682
+ /** Array of products */
683
+ products: e ?? { hits: [], total: 0 },
684
+ /** Array of keywords */
685
+ keywords: t ?? { hits: [], total: 0 }
686
+ };
687
+ }
688
+ function Je() {
689
+ const e = v((r) => {
690
+ var n;
691
+ return (n = r.query.products) == null ? void 0 : n.filter;
692
+ });
693
+ return N(() => e ? e.reduce((r, n) => r + (Array.isArray(n.value) ? n.value.length : 1), 0) : 0, [e]);
694
+ }
695
+ function x(e) {
696
+ return e && !Number.isNaN(e) ? e : 0;
697
+ }
698
+ function Ye(e, t) {
699
+ const { from: r, size: n, total: o } = v((s) => {
700
+ var f, a, d, m, g;
701
+ return {
702
+ from: x(((f = s.query.products) == null ? void 0 : f.from) ?? 0),
703
+ size: x(((d = (a = s.response) == null ? void 0 : a.products) == null ? void 0 : d.size) ?? 0),
704
+ total: x(((g = (m = s.response) == null ? void 0 : m.products) == null ? void 0 : g.total) ?? 0)
705
+ };
706
+ }), { updateSearch: u } = k(), c = r + n, i = [...e].reverse().filter((s) => s + t < o);
707
+ return {
708
+ /** from value */
709
+ from: r,
710
+ /** to value */
711
+ to: c,
712
+ /** total value */
713
+ total: o,
714
+ /** size value */
715
+ size: n,
716
+ /** Array of size options */
717
+ sizeOptions: i,
718
+ /** Should be called when size is changed */
719
+ handleSizeChange: (s) => {
720
+ u({
721
+ products: {
722
+ size: s
723
+ }
724
+ });
725
+ }
122
726
  };
123
727
  }
124
- function I(t) {
125
- return z(t);
728
+ function xe(e, t) {
729
+ return e.length !== t.length ? !1 : e.every((r) => t.find((n) => r.field === n.field && r.order === n.order));
730
+ }
731
+ function We(e) {
732
+ var u, c;
733
+ const t = v((i) => i.query), { updateSearch: r } = k();
734
+ return {
735
+ /** Active sort */
736
+ activeSort: ((u = e.find((i) => {
737
+ var l;
738
+ return xe(i.value.sort, ((l = t.products) == null ? void 0 : l.sort) || []);
739
+ })) == null ? void 0 : u.id) ?? ((c = e[0]) == null ? void 0 : c.id),
740
+ /** Set sort function */
741
+ setSort: (i) => {
742
+ const l = e.find((s) => s.id === i);
743
+ l && r({
744
+ products: {
745
+ sort: l.value.sort
746
+ }
747
+ });
748
+ }
749
+ };
126
750
  }
127
751
  export {
128
- C as StoreContext,
129
- M as StoreProvider,
130
- I as mockStore,
131
- V as useNostoAppState,
132
- F as usePersonalization
752
+ je as AutocompletePageProvider,
753
+ De as CategoryPageProvider,
754
+ Ve as SearchInput,
755
+ Ue as SearchPageProvider,
756
+ $e as SerpElement,
757
+ F as StoreContext,
758
+ se as createExtendableStore,
759
+ I as createStore,
760
+ ie as defaultState,
761
+ k as useActions,
762
+ Be as useDecoratedSearchResults,
763
+ Ke as useFacet,
764
+ Qe as useFacets,
765
+ v as useNostoAppState,
766
+ He as usePersonalization,
767
+ Ie as useProductFilters,
768
+ Te as useRange,
769
+ Le as useRangeSelector,
770
+ Ge as useResponse,
771
+ Je as useSelectedFiltersCount,
772
+ Ye as useSizeOptions,
773
+ We as useSort
133
774
  };