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