fast-simon-sdk 1.6.8 → 1.6.9

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 (165) hide show
  1. package/lib/config.d.ts +25 -25
  2. package/lib/config.js +30 -30
  3. package/lib/index.d.ts +3 -3
  4. package/lib/index.js +72 -72
  5. package/lib/index.js.map +1 -1
  6. package/lib/models/autocomplete.d.ts +73 -73
  7. package/lib/models/autocomplete.js +2 -2
  8. package/lib/models/common.d.ts +267 -267
  9. package/lib/models/common.js +65 -65
  10. package/lib/models/common.js.map +1 -1
  11. package/lib/models/content.d.ts +37 -37
  12. package/lib/models/content.js +2 -2
  13. package/lib/models/events.d.ts +113 -113
  14. package/lib/models/events.js +2 -2
  15. package/lib/models/exports.d.ts +6 -6
  16. package/lib/models/exports.js +5 -5
  17. package/lib/models/fullTextSearch.d.ts +130 -130
  18. package/lib/models/fullTextSearch.js +17 -17
  19. package/lib/models/fullTextSearch.js.map +1 -1
  20. package/lib/models/lookAlikeProductRecommendation.d.ts +8 -8
  21. package/lib/models/lookAlikeProductRecommendation.js +2 -2
  22. package/lib/models/productsInventory.d.ts +10 -10
  23. package/lib/models/productsInventory.js +2 -2
  24. package/lib/models/related.d.ts +35 -35
  25. package/lib/models/related.js +2 -2
  26. package/lib/models/smartCollections.d.ts +94 -94
  27. package/lib/models/smartCollections.js +2 -2
  28. package/lib/models/visualDiscovery.d.ts +129 -129
  29. package/lib/models/visualDiscovery.js +30 -30
  30. package/lib/models/visualDiscovery.js.map +1 -1
  31. package/lib/sections/contentSearch/index.d.ts +7 -7
  32. package/lib/sections/contentSearch/index.js +44 -44
  33. package/lib/sections/contentSearch/index.js.map +1 -1
  34. package/lib/sections/enums/index.d.ts +47 -47
  35. package/lib/sections/enums/index.js +50 -50
  36. package/lib/sections/eventReporter/index.d.ts +10 -10
  37. package/lib/sections/eventReporter/index.js +628 -628
  38. package/lib/sections/eventReporter/index.js.map +1 -1
  39. package/lib/sections/eventReporter/utils.d.ts +9 -9
  40. package/lib/sections/eventReporter/utils.js +31 -31
  41. package/lib/sections/eventReporter/utils.js.map +1 -1
  42. package/lib/sections/fullTextSearch/index.d.ts +6 -6
  43. package/lib/sections/fullTextSearch/index.js +178 -178
  44. package/lib/sections/fullTextSearch/index.js.map +1 -1
  45. package/lib/sections/fullTextSearch/utils.d.ts +17 -17
  46. package/lib/sections/fullTextSearch/utils.js +185 -181
  47. package/lib/sections/fullTextSearch/utils.js.map +1 -1
  48. package/lib/sections/index.d.ts +3 -3
  49. package/lib/sections/index.js +432 -434
  50. package/lib/sections/index.js.map +1 -1
  51. package/lib/sections/initialization/index.d.ts +8 -8
  52. package/lib/sections/initialization/index.js +150 -150
  53. package/lib/sections/initialization/index.js.map +1 -1
  54. package/lib/sections/initialization/utils.d.ts +11 -11
  55. package/lib/sections/initialization/utils.js +200 -200
  56. package/lib/sections/initialization/utils.js.map +1 -1
  57. package/lib/sections/instantSearch/index.d.ts +6 -6
  58. package/lib/sections/instantSearch/index.js +66 -66
  59. package/lib/sections/instantSearch/index.js.map +1 -1
  60. package/lib/sections/instantSearch/utils.d.ts +21 -21
  61. package/lib/sections/instantSearch/utils.js +232 -232
  62. package/lib/sections/instantSearch/utils.js.map +1 -1
  63. package/lib/sections/landingPage/index.d.ts +6 -6
  64. package/lib/sections/landingPage/index.js +95 -95
  65. package/lib/sections/landingPage/index.js.map +1 -1
  66. package/lib/sections/landingPage/utils.d.ts +14 -14
  67. package/lib/sections/landingPage/utils.js +96 -96
  68. package/lib/sections/landingPage/utils.js.map +1 -1
  69. package/lib/sections/lookAlikeProductRecommendation/index.d.ts +6 -6
  70. package/lib/sections/lookAlikeProductRecommendation/index.js +23 -23
  71. package/lib/sections/recommendations/index.d.ts +10 -10
  72. package/lib/sections/recommendations/index.js +95 -95
  73. package/lib/sections/recommendations/index.js.map +1 -1
  74. package/lib/sections/recommendations/utils.d.ts +6 -6
  75. package/lib/sections/recommendations/utils.js +82 -82
  76. package/lib/sections/recommendations/utils.js.map +1 -1
  77. package/lib/sections/smartCollections/index.d.ts +6 -6
  78. package/lib/sections/smartCollections/index.js +169 -169
  79. package/lib/sections/smartCollections/index.js.map +1 -1
  80. package/lib/sections/smartCollections/utils.d.ts +41 -41
  81. package/lib/sections/smartCollections/utils.js +286 -281
  82. package/lib/sections/smartCollections/utils.js.map +1 -1
  83. package/lib/sections/visualDiscovery/index.d.ts +22 -22
  84. package/lib/sections/visualDiscovery/index.js +261 -265
  85. package/lib/sections/visualDiscovery/index.js.map +1 -1
  86. package/lib/services/autocomplete.d.ts +24 -24
  87. package/lib/services/autocomplete.js +50 -50
  88. package/lib/services/common.d.ts +35 -35
  89. package/lib/services/common.js +94 -94
  90. package/lib/services/content.d.ts +4 -4
  91. package/lib/services/content.js +25 -25
  92. package/lib/services/fullTextSearch.d.ts +4 -4
  93. package/lib/services/fullTextSearch.js +87 -87
  94. package/lib/services/fullTextSearch.js.map +1 -1
  95. package/lib/services/landingPage.d.ts +21 -21
  96. package/lib/services/landingPage.js +44 -44
  97. package/lib/services/lookAlikeProductRecommendation.d.ts +13 -13
  98. package/lib/services/lookAlikeProductRecommendation.js +23 -23
  99. package/lib/services/related.d.ts +48 -48
  100. package/lib/services/related.js +114 -114
  101. package/lib/services/related.js.map +1 -1
  102. package/lib/services/reporting.d.ts +80 -80
  103. package/lib/services/reporting.js +258 -258
  104. package/lib/services/reporting.js.map +1 -1
  105. package/lib/services/smartCollections.d.ts +5 -5
  106. package/lib/services/smartCollections.js +93 -93
  107. package/lib/services/smartCollections.js.map +1 -1
  108. package/lib/services/utils.d.ts +5 -5
  109. package/lib/services/utils.js +25 -25
  110. package/lib/services/utils.js.map +1 -1
  111. package/lib/services/visualDiscovery.d.ts +9 -9
  112. package/lib/services/visualDiscovery.js +166 -170
  113. package/lib/services/visualDiscovery.js.map +1 -1
  114. package/lib/utils/ABTesting.d.ts +2 -2
  115. package/lib/utils/ABTesting.js +27 -28
  116. package/lib/utils/ABTesting.js.map +1 -1
  117. package/lib/utils/API.d.ts +19 -19
  118. package/lib/utils/API.js +81 -81
  119. package/lib/utils/API.js.map +1 -1
  120. package/lib/utils/ArrayUtils.d.ts +2 -2
  121. package/lib/utils/ArrayUtils.js +16 -17
  122. package/lib/utils/ArrayUtils.js.map +1 -1
  123. package/lib/utils/bindingBoxes.d.ts +23 -23
  124. package/lib/utils/bindingBoxes.js +88 -89
  125. package/lib/utils/bindingBoxes.js.map +1 -1
  126. package/lib/utils/cookies.d.ts +13 -13
  127. package/lib/utils/cookies.js +50 -50
  128. package/lib/utils/debug.d.ts +4 -4
  129. package/lib/utils/debug.js +31 -31
  130. package/lib/utils/debug.js.map +1 -1
  131. package/lib/utils/duplicationsHandler.d.ts +19 -0
  132. package/lib/utils/duplicationsHandler.js +70 -0
  133. package/lib/utils/duplicationsHandler.js.map +1 -0
  134. package/lib/utils/filtersPersonalization.d.ts +3 -3
  135. package/lib/utils/filtersPersonalization.js +39 -39
  136. package/lib/utils/filtersPersonalization.js.map +1 -1
  137. package/lib/utils/image.d.ts +6 -6
  138. package/lib/utils/image.js +153 -153
  139. package/lib/utils/image.js.map +1 -1
  140. package/lib/utils/klaviyo.js +1 -1
  141. package/lib/utils/location.d.ts +1 -1
  142. package/lib/utils/location.js +11 -11
  143. package/lib/utils/location.js.map +1 -1
  144. package/lib/utils/mappingUtil.d.ts +4 -4
  145. package/lib/utils/mappingUtil.js +16 -16
  146. package/lib/utils/options.d.ts +23 -23
  147. package/lib/utils/options.js +16 -16
  148. package/lib/utils/personalization.d.ts +2 -2
  149. package/lib/utils/personalization.js +20 -20
  150. package/lib/utils/personalization.js.map +1 -1
  151. package/lib/utils/segments.d.ts +2 -2
  152. package/lib/utils/segments.js +19 -20
  153. package/lib/utils/segments.js.map +1 -1
  154. package/lib/utils/shopifyMarkets.d.ts +3 -3
  155. package/lib/utils/shopifyMarkets.js +12 -12
  156. package/lib/utils/sortUtil.d.ts +1 -1
  157. package/lib/utils/sortUtil.js +24 -24
  158. package/lib/utils/sortUtil.js.map +1 -1
  159. package/lib/utils/updateRecentSearches.d.ts +2 -2
  160. package/lib/utils/updateRecentSearches.js +56 -57
  161. package/lib/utils/updateRecentSearches.js.map +1 -1
  162. package/lib/utils/validation.d.ts +5 -5
  163. package/lib/utils/validation.js +46 -46
  164. package/lib/utils/validation.js.map +1 -1
  165. package/package.json +3 -2
@@ -1,282 +1,287 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.hasProductFacetsCallbacks = exports.handleProductFacetsCallbacks = exports.isRecommendedProduct = exports.facetNeeded = exports.savePersonalizedProductsFromCollectionResponse = exports.updateRecentCollectionProducts = exports.getRecentCollectionProducts = exports.getCollectionName = exports.getPersonalizedProducts = exports.handleSmartCollection = exports.getData = exports.setInfiniteScroll = exports.emptyResults = void 0;
15
- var ArrayUtils_1 = require("../../utils/ArrayUtils");
16
- var related_1 = require("../../services/related");
17
- var config_1 = require("../../config");
18
- var utils_1 = require("../fullTextSearch/utils");
19
- var utilities_1 = require("@fast-simon/utilities");
20
- var utils_2 = require("../initialization/utils");
21
- var emptyResults = function (self, categoryID) {
22
- self.temp.smartCollections = {
23
- ancestors: [],
24
- totalResults: 0,
25
- products: [],
26
- categoryID: categoryID,
27
- name: ""
28
- };
29
- };
30
- exports.emptyResults = emptyResults;
31
- var setInfiniteScroll = function (self, infiniteCount, productsPerPage) {
32
- if (infiniteCount && productsPerPage) {
33
- self.temp.smartCollections.pageCount = Math.ceil(self.temp.smartCollections.totalResults / (productsPerPage / infiniteCount));
34
- }
35
- };
36
- exports.setInfiniteScroll = setInfiniteScroll;
37
- var getData = function (self, response) {
38
- var _a, _b, _c, _d, _e, _f;
39
- var result = {};
40
- result.products = response.items;
41
- result.totalResults = response.total_results;
42
- if (response.ancestor_categories) {
43
- result.ancestors = response.ancestor_categories;
44
- }
45
- if (response.narrow) {
46
- result.narrow = (0, utils_1.reformatNarrow)(response.narrow);
47
- }
48
- if (response.sort_by) {
49
- result.sortBy = response.sort_by;
50
- }
51
- if (response.pop_products_no_results) {
52
- result.isNoResults = true;
53
- }
54
- if (response.total_p) {
55
- result.pageCount = response.total_p;
56
- }
57
- if (response.avoid_tag_prefixes) {
58
- result.avoidPrefixList = response.avoid_tag_prefixes.split(",");
59
- }
60
- if (response.merch) {
61
- result.merch = response.merch;
62
- }
63
- if (response.banner_html) {
64
- result.bannerHTML = response.banner_html;
65
- }
66
- if (response.personalized_products) {
67
- self.data.personalizedProducts = (0, exports.savePersonalizedProductsFromCollectionResponse)(response.personalized_products);
68
- }
69
- if (response.related_categories) {
70
- result.relatedCategories = (_b = (_a = response.related_categories) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.call(_a, function (category) {
71
- var _a, _b;
72
- return ({
73
- id: category === null || category === void 0 ? void 0 : category.id,
74
- title: category === null || category === void 0 ? void 0 : category.l,
75
- image: ((_b = (_a = category.t) === null || _a === void 0 ? void 0 : _a.includes) === null || _b === void 0 ? void 0 : _b.call(_a, '/missing.')) ? '' : category.t,
76
- url: category === null || category === void 0 ? void 0 : category.u
77
- });
78
- });
79
- }
80
- else if ((_c = self === null || self === void 0 ? void 0 : self.data) === null || _c === void 0 ? void 0 : _c.categories) {
81
- try {
82
- var platform = (0, utils_2.getPlatform)();
83
- var localStoragePopCategories = localStorage === null || localStorage === void 0 ? void 0 : localStorage.getItem(utilities_1.FSKeys.FS_POP_CATEGORIES);
84
- var relatedCategories = utilities_1.RelatedCategories === null || utilities_1.RelatedCategories === void 0 ? void 0 : utilities_1.RelatedCategories.getRelated(((_e = (_d = self === null || self === void 0 ? void 0 : self.data) === null || _d === void 0 ? void 0 : _d.categories) !== null && _e !== void 0 ? _e : (localStoragePopCategories ? JSON.parse(localStoragePopCategories) : [])), response.category_id, undefined, (platform ? platform : undefined));
85
- result.relatedCategories = (_f = relatedCategories === null || relatedCategories === void 0 ? void 0 : relatedCategories.map) === null || _f === void 0 ? void 0 : _f.call(relatedCategories, function (category) {
86
- var _a, _b;
87
- return ({
88
- id: category.id,
89
- title: category.l,
90
- image: ((_b = (_a = category.t) === null || _a === void 0 ? void 0 : _a.includes) === null || _b === void 0 ? void 0 : _b.call(_a, '/missing.')) ? '' : category.t,
91
- url: category.u
92
- });
93
- });
94
- }
95
- catch (e) {
96
- }
97
- }
98
- result.page = response.p || 1;
99
- result.categoryID = response.category_id;
100
- result.name = response.category_name;
101
- return result;
102
- };
103
- exports.getData = getData;
104
- var handleSmartCollection = function (_a) {
105
- var event = _a.event, callback = _a.callback, self = _a.self, personalized = _a.personalized, time = _a.time, isNoResults = _a.isNoResults, page = _a.page, sortBy = _a.sortBy, narrow = _a.narrow, result = _a.result, inventoryCallback = _a.inventoryCallback, access_token = _a.access_token, shop_url = _a.shop_url, onProductsLoaded = _a.onProductsLoaded, onFacetsLoaded = _a.onFacetsLoaded, callbackType = _a.callbackType;
106
- if (personalized && !isNoResults && page === 1 && (!narrow || narrow.length === 0) && (!sortBy || sortBy === "relevency")) {
107
- handlePersonalisedProducts(self, result, callback, event, time !== null && time !== void 0 ? time : 356, onProductsLoaded, onFacetsLoaded, callbackType);
108
- }
109
- else {
110
- if ((0, exports.hasProductFacetsCallbacks)(onProductsLoaded, onFacetsLoaded)) {
111
- if (callbackType === "products" || callbackType === "facets and products") {
112
- (0, exports.handleProductFacetsCallbacks)(result, "products", onProductsLoaded, onFacetsLoaded);
113
- }
114
- if (callbackType === "facets" || callbackType === "facets and products") {
115
- (0, exports.handleProductFacetsCallbacks)(result, "facets", onProductsLoaded, onFacetsLoaded);
116
- }
117
- }
118
- else {
119
- callback({
120
- action: event,
121
- payload: result
122
- });
123
- }
124
- }
125
- if (access_token && inventoryCallback) {
126
- (0, utilities_1.getProductsInventory)(result === null || result === void 0 ? void 0 : result.products, access_token, inventoryCallback, shop_url);
127
- }
128
- };
129
- exports.handleSmartCollection = handleSmartCollection;
130
- var handlePersonalisedProducts = function (self, payload, callback, event, time, onProductsLoaded, onFacetsLoaded, callbackType) {
131
- var _a;
132
- var _b = self.data, storeID = _b.storeID, uuid = _b.uuid, userToken = _b.userToken;
133
- var now = Math.round(Date.now() / 1000);
134
- var categoryID = payload.categoryID;
135
- var recent = (0, exports.getRecentCollectionProducts)().filter(function (r) { return r.time + time * 360 > now; });
136
- localStorage.setItem("".concat(config_1.RECENT_COLLECTION_PRODUCTS_KEY), JSON.stringify(recent));
137
- var isPersonalizedFiltersActive = ((_a = self.data.dashboard) === null || _a === void 0 ? void 0 : _a.is_personalized_filter) || false;
138
- var recentProducts = recent.map(function (r) { return r.id; });
139
- if (recent.length > 0) {
140
- related_1.Related.getRecentlyViewed({
141
- uuid: uuid,
142
- storeID: storeID,
143
- recent: recentProducts,
144
- categoryID: categoryID,
145
- cartToken: self.data.cartToken,
146
- isPersonalizedFiltersActive: isPersonalizedFiltersActive,
147
- userToken: userToken
148
- }).then(function (response) {
149
- var result = __assign(__assign({}, payload), { products: (0, exports.getPersonalizedProducts)(self, response.widget_responses[0].products, payload.products) });
150
- if ((0, exports.hasProductFacetsCallbacks)(onProductsLoaded, onFacetsLoaded)) {
151
- if (callbackType === "products" || callbackType === "facets and products") {
152
- (0, exports.handleProductFacetsCallbacks)(result, "products", onProductsLoaded, onFacetsLoaded);
153
- }
154
- if (callbackType === "facets" || callbackType === "facets and products") {
155
- (0, exports.handleProductFacetsCallbacks)(result, "facets", onProductsLoaded, onFacetsLoaded);
156
- }
157
- }
158
- else {
159
- callback({
160
- action: event,
161
- payload: result
162
- });
163
- }
164
- });
165
- }
166
- else {
167
- if ((0, exports.hasProductFacetsCallbacks)(onProductsLoaded, onFacetsLoaded)) {
168
- if (callbackType === "products" || callbackType === "facets and products") {
169
- (0, exports.handleProductFacetsCallbacks)(payload, "products", onProductsLoaded, onFacetsLoaded);
170
- }
171
- if (callbackType === "facets" || callbackType === "facets and products") {
172
- (0, exports.handleProductFacetsCallbacks)(payload, "facets", onProductsLoaded, onFacetsLoaded);
173
- }
174
- }
175
- else {
176
- callback({
177
- action: event,
178
- payload: payload
179
- });
180
- }
181
- }
182
- };
183
- var getPersonalizedProducts = function (self, recent, products) {
184
- var recentProducts = recent.filter(function (r, i) {
185
- var newPosition = i * 2;
186
- var oldPosition = products.findIndex(function (p) { return p.id === r.id; });
187
- return oldPosition === -1 || (newPosition < oldPosition);
188
- });
189
- var ids = new Set(recentProducts.map(function (p) { return p.id; }));
190
- var newProducts = products.filter(function (p) { return !ids.has(p.id); });
191
- for (var i = 0, pos = 1; i < recentProducts.length; i++, pos += 2) {
192
- (0, ArrayUtils_1.insert)(newProducts, pos, __assign({ rec: true }, recentProducts[i]));
193
- }
194
- return newProducts;
195
- };
196
- exports.getPersonalizedProducts = getPersonalizedProducts;
197
- var getCollectionName = function (self, id) {
198
- id = String(id);
199
- var category = self.data.categories.find(function (category) { return category.id === id; });
200
- if (!category) {
201
- console.warn("can't find the collection / category name for id: ".concat(id));
202
- return id;
203
- }
204
- return category.l;
205
- };
206
- exports.getCollectionName = getCollectionName;
207
- var getRecentCollectionProducts = function () {
208
- if (config_1.RECENT_COLLECTION_PRODUCTS_KEY in localStorage) {
209
- return JSON.parse(localStorage.getItem(config_1.RECENT_COLLECTION_PRODUCTS_KEY));
210
- }
211
- return [];
212
- };
213
- exports.getRecentCollectionProducts = getRecentCollectionProducts;
214
- var updateRecentCollectionProducts = function (productID, categoryID) {
215
- var categories = (0, exports.getRecentCollectionProducts)();
216
- var now = Math.round(Date.now() / 1000);
217
- var index = categories.findIndex(function (c) { return c.id === productID; });
218
- if (index !== -1) {
219
- moveAndUpdate(categories, index);
220
- }
221
- else {
222
- categories.push({
223
- id: productID,
224
- time: now
225
- });
226
- }
227
- // Filter out products older than RECENT_PRODUCTS_EXPIRY_DAYS
228
- var updatedCategoriesProducts = categories.filter(function (c) { return (now - c.time <= (config_1.RECENT_PRODUCTS_EXPIRY_DAYS * 24 * 60 * 60)); });
229
- var sortedCategoriesProducts = updatedCategoriesProducts.sort(function (a, b) { return b.time - a.time; });
230
- localStorage.setItem(config_1.RECENT_COLLECTION_PRODUCTS_KEY, JSON.stringify(sortedCategoriesProducts.splice(0, config_1.MAX_RECENT_COLLECTION_PRODUCTS)));
231
- };
232
- exports.updateRecentCollectionProducts = updateRecentCollectionProducts;
233
- var savePersonalizedProductsFromCollectionResponse = function (data) {
234
- localStorage.setItem(config_1.RECENT_RESPONSE_COLLECTION_PRODUCTS_KEY, JSON.stringify(data.map(function (id) { return String(id); })));
235
- return data === null || data === void 0 ? void 0 : data.join(',');
236
- };
237
- exports.savePersonalizedProductsFromCollectionResponse = savePersonalizedProductsFromCollectionResponse;
238
- var moveAndUpdate = function (categories, index) {
239
- (0, ArrayUtils_1.arrayMove)(categories, index, categories.length - 1);
240
- categories[categories.length - 1].time = Date.now();
241
- };
242
- var facetNeeded = function (_a) {
243
- var self = _a.self, newNarrow = _a.newNarrow, newPage = _a.newPage, newCategoryID = _a.newCategoryID;
244
- var _b = self.temp.smartCollections, narrow = _b.narrow, page = _b.page, categoryID = _b.categoryID;
245
- return !(JSON.stringify(narrow) === JSON.stringify(newNarrow) && newPage === page && categoryID === newCategoryID);
246
- };
247
- exports.facetNeeded = facetNeeded;
248
- var isRecommendedProduct = function (productId) {
249
- var recent = (0, exports.getRecentCollectionProducts)();
250
- return Boolean(recent.find(function (r) { return r.id === productId; }));
251
- };
252
- exports.isRecommendedProduct = isRecommendedProduct;
253
- var handleProductFacetsCallbacks = function (result, action, onProductsLoaded, onFacetsLoaded) {
254
- if (action === "products") {
255
- // Create a shallow copy of the result object to safely remove facets
256
- var resultWithoutFacets = __assign({}, result);
257
- // If the facets field exists on the result copy, remove it
258
- if (resultWithoutFacets === null || resultWithoutFacets === void 0 ? void 0 : resultWithoutFacets.facets) {
259
- delete resultWithoutFacets.facets;
260
- }
261
- // Call the onProductsLoaded function with the result copy that has facets removed
262
- onProductsLoaded ? onProductsLoaded(resultWithoutFacets) : null;
263
- }
264
- if (action === "facets") {
265
- // Create another shallow copy of the result object to safely remove products
266
- var resultWithoutProducts = __assign({}, result);
267
- // If the products field exists on the result copy, remove it
268
- if (resultWithoutProducts === null || resultWithoutProducts === void 0 ? void 0 : resultWithoutProducts.products) {
269
- delete resultWithoutProducts.products;
270
- }
271
- // Call the onFacetsLoaded function with the result copy that has products removed
272
- onFacetsLoaded ? onFacetsLoaded(resultWithoutProducts) : null;
273
- }
274
- };
275
- exports.handleProductFacetsCallbacks = handleProductFacetsCallbacks;
276
- // Dummy implementation of hasProductFacetsCallbacks
277
- var hasProductFacetsCallbacks = function (onProductsLoaded, onFacetsLoaded) {
278
- // Logic to check if props contain product facets callbacks
279
- return !!onProductsLoaded && !!onFacetsLoaded;
280
- };
281
- exports.hasProductFacetsCallbacks = hasProductFacetsCallbacks;
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.hasProductFacetsCallbacks = exports.handleProductFacetsCallbacks = exports.isRecommendedProduct = exports.facetNeeded = exports.savePersonalizedProductsFromCollectionResponse = exports.updateRecentCollectionProducts = exports.getRecentCollectionProducts = exports.getCollectionName = exports.getPersonalizedProducts = exports.handleSmartCollection = exports.getData = exports.setInfiniteScroll = exports.emptyResults = void 0;
15
+ var ArrayUtils_1 = require("../../utils/ArrayUtils");
16
+ var related_1 = require("../../services/related");
17
+ var config_1 = require("../../config");
18
+ var utils_1 = require("../fullTextSearch/utils");
19
+ var utilities_1 = require("@fast-simon/utilities");
20
+ var utils_2 = require("../initialization/utils");
21
+ var duplicationsHandler_1 = require("../../utils/duplicationsHandler");
22
+ var emptyResults = function (self, categoryID) {
23
+ self.temp.smartCollections = {
24
+ ancestors: [],
25
+ totalResults: 0,
26
+ products: [],
27
+ categoryID: categoryID,
28
+ name: ""
29
+ };
30
+ };
31
+ exports.emptyResults = emptyResults;
32
+ var setInfiniteScroll = function (self, infiniteCount, productsPerPage) {
33
+ if (infiniteCount && productsPerPage) {
34
+ self.temp.smartCollections.pageCount = Math.ceil(self.temp.smartCollections.totalResults / (productsPerPage / infiniteCount));
35
+ }
36
+ };
37
+ exports.setInfiniteScroll = setInfiniteScroll;
38
+ var getData = function (self, response) {
39
+ var _a, _b, _c, _d, _e, _f;
40
+ var result = {};
41
+ result.products = response.items;
42
+ result.totalResults = response.total_results;
43
+ if (response.ancestor_categories) {
44
+ result.ancestors = response.ancestor_categories;
45
+ }
46
+ if (response.narrow) {
47
+ result.narrow = (0, utils_1.reformatNarrow)(response.narrow);
48
+ }
49
+ if (response.sort_by) {
50
+ result.sortBy = response.sort_by;
51
+ }
52
+ if (response.pop_products_no_results) {
53
+ result.isNoResults = true;
54
+ }
55
+ if (response.total_p) {
56
+ result.pageCount = response.total_p;
57
+ }
58
+ if (response.avoid_tag_prefixes) {
59
+ result.avoidPrefixList = response.avoid_tag_prefixes.split(",");
60
+ }
61
+ if (response.merch) {
62
+ result.merch = response.merch;
63
+ }
64
+ if (response.banner_html) {
65
+ result.bannerHTML = response.banner_html;
66
+ }
67
+ if (response.personalized_products) {
68
+ self.data.personalizedProducts = (0, exports.savePersonalizedProductsFromCollectionResponse)(response.personalized_products);
69
+ }
70
+ if (response.related_categories) {
71
+ result.relatedCategories = (_b = (_a = response.related_categories) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.call(_a, function (category) {
72
+ var _a, _b;
73
+ return ({
74
+ id: category === null || category === void 0 ? void 0 : category.id,
75
+ title: category === null || category === void 0 ? void 0 : category.l,
76
+ image: ((_b = (_a = category.t) === null || _a === void 0 ? void 0 : _a.includes) === null || _b === void 0 ? void 0 : _b.call(_a, '/missing.')) ? '' : category.t,
77
+ url: category === null || category === void 0 ? void 0 : category.u
78
+ });
79
+ });
80
+ }
81
+ else if ((_c = self === null || self === void 0 ? void 0 : self.data) === null || _c === void 0 ? void 0 : _c.categories) {
82
+ try {
83
+ var platform = (0, utils_2.getPlatform)();
84
+ var localStoragePopCategories = localStorage === null || localStorage === void 0 ? void 0 : localStorage.getItem(utilities_1.FSKeys.FS_POP_CATEGORIES);
85
+ var relatedCategories = utilities_1.RelatedCategories === null || utilities_1.RelatedCategories === void 0 ? void 0 : utilities_1.RelatedCategories.getRelated(((_e = (_d = self === null || self === void 0 ? void 0 : self.data) === null || _d === void 0 ? void 0 : _d.categories) !== null && _e !== void 0 ? _e : (localStoragePopCategories ? JSON.parse(localStoragePopCategories) : [])), response.category_id, undefined, (platform ? platform : undefined));
86
+ result.relatedCategories = (_f = relatedCategories === null || relatedCategories === void 0 ? void 0 : relatedCategories.map) === null || _f === void 0 ? void 0 : _f.call(relatedCategories, function (category) {
87
+ var _a, _b;
88
+ return ({
89
+ id: category.id,
90
+ title: category.l,
91
+ image: ((_b = (_a = category.t) === null || _a === void 0 ? void 0 : _a.includes) === null || _b === void 0 ? void 0 : _b.call(_a, '/missing.')) ? '' : category.t,
92
+ url: category.u
93
+ });
94
+ });
95
+ }
96
+ catch (e) {
97
+ }
98
+ }
99
+ result.page = response.p || 1;
100
+ result.categoryID = response.category_id;
101
+ result.name = response.category_name;
102
+ return result;
103
+ };
104
+ exports.getData = getData;
105
+ var handleSmartCollection = function (_a) {
106
+ var _b, _c;
107
+ var event = _a.event, callback = _a.callback, self = _a.self, personalized = _a.personalized, time = _a.time, isNoResults = _a.isNoResults, page = _a.page, sortBy = _a.sortBy, narrow = _a.narrow, result = _a.result, inventoryCallback = _a.inventoryCallback, access_token = _a.access_token, shop_url = _a.shop_url, onProductsLoaded = _a.onProductsLoaded, onFacetsLoaded = _a.onFacetsLoaded, callbackType = _a.callbackType;
108
+ if (personalized && !isNoResults && page === 1 && (!narrow || narrow.length === 0) && (!sortBy || sortBy === "relevency")) {
109
+ handlePersonalisedProducts(self, result, callback, event, time !== null && time !== void 0 ? time : 356, onProductsLoaded, onFacetsLoaded, callbackType);
110
+ }
111
+ else {
112
+ if ((result === null || result === void 0 ? void 0 : result.products) && ((_b = result === null || result === void 0 ? void 0 : result.products) === null || _b === void 0 ? void 0 : _b.length) && ((_c = self.data) === null || _c === void 0 ? void 0 : _c.uuid) === 'a93cd17e-84be-4302-b068-8b2f37c1c415') {
113
+ result.products = duplicationsHandler_1.duplicationsHandler.getFilteredProducts(result);
114
+ }
115
+ if ((0, exports.hasProductFacetsCallbacks)(onProductsLoaded, onFacetsLoaded)) {
116
+ if (callbackType === "products" || callbackType === "facets and products") {
117
+ (0, exports.handleProductFacetsCallbacks)(result, "products", onProductsLoaded, onFacetsLoaded);
118
+ }
119
+ if (callbackType === "facets" || callbackType === "facets and products") {
120
+ (0, exports.handleProductFacetsCallbacks)(result, "facets", onProductsLoaded, onFacetsLoaded);
121
+ }
122
+ }
123
+ else {
124
+ callback({
125
+ action: event,
126
+ payload: result
127
+ });
128
+ }
129
+ }
130
+ if (access_token && inventoryCallback) {
131
+ (0, utilities_1.getProductsInventory)(result === null || result === void 0 ? void 0 : result.products, access_token, inventoryCallback, shop_url);
132
+ }
133
+ };
134
+ exports.handleSmartCollection = handleSmartCollection;
135
+ var handlePersonalisedProducts = function (self, payload, callback, event, time, onProductsLoaded, onFacetsLoaded, callbackType) {
136
+ var _a;
137
+ var _b = self.data, storeID = _b.storeID, uuid = _b.uuid, userToken = _b.userToken;
138
+ var now = Math.round(Date.now() / 1000);
139
+ var categoryID = payload.categoryID;
140
+ var recent = (0, exports.getRecentCollectionProducts)().filter(function (r) { return r.time + time * 360 > now; });
141
+ localStorage.setItem("".concat(config_1.RECENT_COLLECTION_PRODUCTS_KEY), JSON.stringify(recent));
142
+ var isPersonalizedFiltersActive = ((_a = self.data.dashboard) === null || _a === void 0 ? void 0 : _a.is_personalized_filter) || false;
143
+ var recentProducts = recent.map(function (r) { return r.id; });
144
+ if (recent.length > 0) {
145
+ related_1.Related.getRecentlyViewed({
146
+ uuid: uuid,
147
+ storeID: storeID,
148
+ recent: recentProducts,
149
+ categoryID: categoryID,
150
+ cartToken: self.data.cartToken,
151
+ isPersonalizedFiltersActive: isPersonalizedFiltersActive,
152
+ userToken: userToken
153
+ }).then(function (response) {
154
+ var result = __assign(__assign({}, payload), { products: (0, exports.getPersonalizedProducts)(self, response.widget_responses[0].products, payload.products) });
155
+ if ((0, exports.hasProductFacetsCallbacks)(onProductsLoaded, onFacetsLoaded)) {
156
+ if (callbackType === "products" || callbackType === "facets and products") {
157
+ (0, exports.handleProductFacetsCallbacks)(result, "products", onProductsLoaded, onFacetsLoaded);
158
+ }
159
+ if (callbackType === "facets" || callbackType === "facets and products") {
160
+ (0, exports.handleProductFacetsCallbacks)(result, "facets", onProductsLoaded, onFacetsLoaded);
161
+ }
162
+ }
163
+ else {
164
+ callback({
165
+ action: event,
166
+ payload: result
167
+ });
168
+ }
169
+ });
170
+ }
171
+ else {
172
+ if ((0, exports.hasProductFacetsCallbacks)(onProductsLoaded, onFacetsLoaded)) {
173
+ if (callbackType === "products" || callbackType === "facets and products") {
174
+ (0, exports.handleProductFacetsCallbacks)(payload, "products", onProductsLoaded, onFacetsLoaded);
175
+ }
176
+ if (callbackType === "facets" || callbackType === "facets and products") {
177
+ (0, exports.handleProductFacetsCallbacks)(payload, "facets", onProductsLoaded, onFacetsLoaded);
178
+ }
179
+ }
180
+ else {
181
+ callback({
182
+ action: event,
183
+ payload: payload
184
+ });
185
+ }
186
+ }
187
+ };
188
+ var getPersonalizedProducts = function (self, recent, products) {
189
+ var recentProducts = recent.filter(function (r, i) {
190
+ var newPosition = i * 2;
191
+ var oldPosition = products.findIndex(function (p) { return p.id === r.id; });
192
+ return oldPosition === -1 || (newPosition < oldPosition);
193
+ });
194
+ var ids = new Set(recentProducts.map(function (p) { return p.id; }));
195
+ var newProducts = products.filter(function (p) { return !ids.has(p.id); });
196
+ for (var i = 0, pos = 1; i < recentProducts.length; i++, pos += 2) {
197
+ (0, ArrayUtils_1.insert)(newProducts, pos, __assign({ rec: true }, recentProducts[i]));
198
+ }
199
+ return newProducts;
200
+ };
201
+ exports.getPersonalizedProducts = getPersonalizedProducts;
202
+ var getCollectionName = function (self, id) {
203
+ id = String(id);
204
+ var category = self.data.categories.find(function (category) { return category.id === id; });
205
+ if (!category) {
206
+ console.warn("can't find the collection / category name for id: ".concat(id));
207
+ return id;
208
+ }
209
+ return category.l;
210
+ };
211
+ exports.getCollectionName = getCollectionName;
212
+ var getRecentCollectionProducts = function () {
213
+ if (config_1.RECENT_COLLECTION_PRODUCTS_KEY in localStorage) {
214
+ return JSON.parse(localStorage.getItem(config_1.RECENT_COLLECTION_PRODUCTS_KEY));
215
+ }
216
+ return [];
217
+ };
218
+ exports.getRecentCollectionProducts = getRecentCollectionProducts;
219
+ var updateRecentCollectionProducts = function (productID, categoryID) {
220
+ var categories = (0, exports.getRecentCollectionProducts)();
221
+ var now = Math.round(Date.now() / 1000);
222
+ var index = categories.findIndex(function (c) { return c.id === productID; });
223
+ if (index !== -1) {
224
+ moveAndUpdate(categories, index);
225
+ }
226
+ else {
227
+ categories.push({
228
+ id: productID,
229
+ time: now
230
+ });
231
+ }
232
+ // Filter out products older than RECENT_PRODUCTS_EXPIRY_DAYS
233
+ var updatedCategoriesProducts = categories.filter(function (c) { return (now - c.time <= (config_1.RECENT_PRODUCTS_EXPIRY_DAYS * 24 * 60 * 60)); });
234
+ var sortedCategoriesProducts = updatedCategoriesProducts.sort(function (a, b) { return b.time - a.time; });
235
+ localStorage.setItem(config_1.RECENT_COLLECTION_PRODUCTS_KEY, JSON.stringify(sortedCategoriesProducts.splice(0, config_1.MAX_RECENT_COLLECTION_PRODUCTS)));
236
+ };
237
+ exports.updateRecentCollectionProducts = updateRecentCollectionProducts;
238
+ var savePersonalizedProductsFromCollectionResponse = function (data) {
239
+ localStorage.setItem(config_1.RECENT_RESPONSE_COLLECTION_PRODUCTS_KEY, JSON.stringify(data.map(function (id) { return String(id); })));
240
+ return data === null || data === void 0 ? void 0 : data.join(',');
241
+ };
242
+ exports.savePersonalizedProductsFromCollectionResponse = savePersonalizedProductsFromCollectionResponse;
243
+ var moveAndUpdate = function (categories, index) {
244
+ (0, ArrayUtils_1.arrayMove)(categories, index, categories.length - 1);
245
+ categories[categories.length - 1].time = Date.now();
246
+ };
247
+ var facetNeeded = function (_a) {
248
+ var self = _a.self, newNarrow = _a.newNarrow, newPage = _a.newPage, newCategoryID = _a.newCategoryID;
249
+ var _b = self.temp.smartCollections, narrow = _b.narrow, page = _b.page, categoryID = _b.categoryID;
250
+ return !(JSON.stringify(narrow) === JSON.stringify(newNarrow) && newPage === page && categoryID === newCategoryID);
251
+ };
252
+ exports.facetNeeded = facetNeeded;
253
+ var isRecommendedProduct = function (productId) {
254
+ var recent = (0, exports.getRecentCollectionProducts)();
255
+ return Boolean(recent.find(function (r) { return r.id === productId; }));
256
+ };
257
+ exports.isRecommendedProduct = isRecommendedProduct;
258
+ var handleProductFacetsCallbacks = function (result, action, onProductsLoaded, onFacetsLoaded) {
259
+ if (action === "products") {
260
+ // Create a shallow copy of the result object to safely remove facets
261
+ var resultWithoutFacets = __assign({}, result);
262
+ // If the facets field exists on the result copy, remove it
263
+ if (resultWithoutFacets === null || resultWithoutFacets === void 0 ? void 0 : resultWithoutFacets.facets) {
264
+ delete resultWithoutFacets.facets;
265
+ }
266
+ // Call the onProductsLoaded function with the result copy that has facets removed
267
+ onProductsLoaded ? onProductsLoaded(resultWithoutFacets) : null;
268
+ }
269
+ if (action === "facets") {
270
+ // Create another shallow copy of the result object to safely remove products
271
+ var resultWithoutProducts = __assign({}, result);
272
+ // If the products field exists on the result copy, remove it
273
+ if (resultWithoutProducts === null || resultWithoutProducts === void 0 ? void 0 : resultWithoutProducts.products) {
274
+ delete resultWithoutProducts.products;
275
+ }
276
+ // Call the onFacetsLoaded function with the result copy that has products removed
277
+ onFacetsLoaded ? onFacetsLoaded(resultWithoutProducts) : null;
278
+ }
279
+ };
280
+ exports.handleProductFacetsCallbacks = handleProductFacetsCallbacks;
281
+ // Dummy implementation of hasProductFacetsCallbacks
282
+ var hasProductFacetsCallbacks = function (onProductsLoaded, onFacetsLoaded) {
283
+ // Logic to check if props contain product facets callbacks
284
+ return !!onProductsLoaded && !!onFacetsLoaded;
285
+ };
286
+ exports.hasProductFacetsCallbacks = hasProductFacetsCallbacks;
282
287
  //# sourceMappingURL=utils.js.map