@athoscommerce/snap-store-mobx 1.5.0 → 1.5.1-beta.110

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 (47) hide show
  1. package/dist/cjs/Autocomplete/Stores/AutocompleteFacetStore.js +2 -1
  2. package/dist/cjs/Chat/ChatStore.d.ts +86 -0
  3. package/dist/cjs/Chat/ChatStore.d.ts.map +1 -0
  4. package/dist/cjs/Chat/ChatStore.js +539 -0
  5. package/dist/cjs/Chat/Stores/ChatAttachmentStore.d.ts +98 -0
  6. package/dist/cjs/Chat/Stores/ChatAttachmentStore.d.ts.map +1 -0
  7. package/dist/cjs/Chat/Stores/ChatAttachmentStore.js +314 -0
  8. package/dist/cjs/Chat/Stores/ChatCompareStore.d.ts +16 -0
  9. package/dist/cjs/Chat/Stores/ChatCompareStore.d.ts.map +1 -0
  10. package/dist/cjs/Chat/Stores/ChatCompareStore.js +65 -0
  11. package/dist/cjs/Chat/Stores/ChatSessionStore.d.ts +107 -0
  12. package/dist/cjs/Chat/Stores/ChatSessionStore.d.ts.map +1 -0
  13. package/dist/cjs/Chat/Stores/ChatSessionStore.js +635 -0
  14. package/dist/cjs/Search/Stores/SearchFacetStore.d.ts.map +1 -1
  15. package/dist/cjs/Search/Stores/SearchFacetStore.js +13 -10
  16. package/dist/cjs/Search/Stores/index.d.ts +1 -1
  17. package/dist/cjs/Search/Stores/index.d.ts.map +1 -1
  18. package/dist/cjs/Search/Stores/index.js +2 -1
  19. package/dist/cjs/index.d.ts +4 -0
  20. package/dist/cjs/index.d.ts.map +1 -1
  21. package/dist/cjs/index.js +5 -1
  22. package/dist/cjs/types.d.ts +21 -2
  23. package/dist/cjs/types.d.ts.map +1 -1
  24. package/dist/esm/Autocomplete/Stores/AutocompleteFacetStore.js +1 -1
  25. package/dist/esm/Chat/ChatStore.d.ts +86 -0
  26. package/dist/esm/Chat/ChatStore.d.ts.map +1 -0
  27. package/dist/esm/Chat/ChatStore.js +478 -0
  28. package/dist/esm/Chat/Stores/ChatAttachmentStore.d.ts +98 -0
  29. package/dist/esm/Chat/Stores/ChatAttachmentStore.d.ts.map +1 -0
  30. package/dist/esm/Chat/Stores/ChatAttachmentStore.js +222 -0
  31. package/dist/esm/Chat/Stores/ChatCompareStore.d.ts +16 -0
  32. package/dist/esm/Chat/Stores/ChatCompareStore.d.ts.map +1 -0
  33. package/dist/esm/Chat/Stores/ChatCompareStore.js +51 -0
  34. package/dist/esm/Chat/Stores/ChatSessionStore.d.ts +107 -0
  35. package/dist/esm/Chat/Stores/ChatSessionStore.d.ts.map +1 -0
  36. package/dist/esm/Chat/Stores/ChatSessionStore.js +601 -0
  37. package/dist/esm/Search/Stores/SearchFacetStore.d.ts.map +1 -1
  38. package/dist/esm/Search/Stores/SearchFacetStore.js +13 -10
  39. package/dist/esm/Search/Stores/index.d.ts +1 -1
  40. package/dist/esm/Search/Stores/index.d.ts.map +1 -1
  41. package/dist/esm/Search/Stores/index.js +1 -1
  42. package/dist/esm/index.d.ts +4 -0
  43. package/dist/esm/index.d.ts.map +1 -1
  44. package/dist/esm/index.js +2 -0
  45. package/dist/esm/types.d.ts +21 -2
  46. package/dist/esm/types.d.ts.map +1 -1
  47. package/package.json +5 -5
@@ -0,0 +1,635 @@
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.ChatSessionStore = void 0;
15
+ var mobx_1 = require("mobx");
16
+ var uuid_1 = require("uuid");
17
+ var ChatAttachmentStore_1 = require("../Stores/ChatAttachmentStore");
18
+ var ChatCompareStore_1 = require("./ChatCompareStore");
19
+ var SearchResultStore_1 = require("../../Search/Stores/SearchResultStore");
20
+ function createChatResultStore(results, meta) {
21
+ return new SearchResultStore_1.SearchResultStore({
22
+ config: {},
23
+ state: { loaded: true },
24
+ data: {
25
+ search: { results: results },
26
+ meta: meta,
27
+ },
28
+ });
29
+ }
30
+ function createChatProduct(result, meta) {
31
+ return new SearchResultStore_1.Product({
32
+ config: {},
33
+ data: { result: result, meta: meta },
34
+ position: 0,
35
+ responseId: '',
36
+ });
37
+ }
38
+ /** Extract raw serializable data from a Product instance for storage. */
39
+ function serializeProduct(product) {
40
+ var _a, _b;
41
+ if (!(product instanceof SearchResultStore_1.Product))
42
+ return product;
43
+ var raw = {
44
+ id: product.id,
45
+ responseId: product.responseId,
46
+ mappings: product.mappings,
47
+ attributes: product.attributes,
48
+ badges: ((_b = (_a = product.badges) === null || _a === void 0 ? void 0 : _a.all) === null || _b === void 0 ? void 0 : _b.map(function (b) { return ({ tag: b.tag }); })) || [],
49
+ };
50
+ if (product.variants) {
51
+ raw.variants = {
52
+ data: product.variants.data.map(function (v) { return ({
53
+ mappings: v.mappings,
54
+ attributes: v.attributes,
55
+ options: v.options,
56
+ badges: v.badges,
57
+ }); }),
58
+ optionConfig: product.variants.optionConfig,
59
+ };
60
+ }
61
+ return raw;
62
+ }
63
+ /** Serialize attachments for localStorage. Strips base64 from images (kept only at runtime). */
64
+ function serializeAttachmentsForStorage(items) {
65
+ return items.map(function (item) {
66
+ if (item.type === 'image') {
67
+ return {
68
+ type: 'image',
69
+ id: item.id,
70
+ fileName: item.fileName,
71
+ imageId: item.imageId,
72
+ imageUrl: item.imageUrl,
73
+ thumbnailUrl: item.thumbnailUrl,
74
+ state: item.state,
75
+ error: item.error,
76
+ };
77
+ }
78
+ if (item.type === 'product') {
79
+ return {
80
+ type: 'product',
81
+ id: item.id,
82
+ productId: item.productId,
83
+ thumbnailUrl: item.thumbnailUrl,
84
+ name: item.name,
85
+ requestType: item.requestType,
86
+ state: item.state,
87
+ error: item.error,
88
+ };
89
+ }
90
+ return {
91
+ type: 'facet',
92
+ id: item.id,
93
+ key: item.key,
94
+ facetLabel: item.facetLabel,
95
+ value: item.value,
96
+ label: item.label,
97
+ count: item.count,
98
+ state: item.state,
99
+ error: item.error,
100
+ };
101
+ });
102
+ }
103
+ /** Convert a chat message array to a plain serializable form for localStorage. */
104
+ function serializeChatForStorage(chat) {
105
+ return chat.map(function (message) {
106
+ var _a, _b;
107
+ switch (message.messageType) {
108
+ case 'productSearchResult': {
109
+ var msg = message;
110
+ return __assign(__assign({}, msg), { results: Array.from(msg.results || []).map(serializeProduct) });
111
+ }
112
+ case 'inspirationResult': {
113
+ var msg = message;
114
+ return __assign(__assign({}, msg), { inspirationSections: (_a = msg.inspirationSections) === null || _a === void 0 ? void 0 : _a.map(function (section) { return (__assign(__assign({}, section), { products: Array.from(section.products || []).map(serializeProduct) })); }) });
115
+ }
116
+ case 'productAnswer': {
117
+ var msg = message;
118
+ return __assign(__assign({}, msg), { sourceProduct: serializeProduct(msg.sourceProduct) });
119
+ }
120
+ case 'productComparison': {
121
+ var msg = message;
122
+ return __assign(__assign({}, msg), { searchResults: Array.from(msg.searchResults || []).map(serializeProduct) });
123
+ }
124
+ case 'productRecommendation': {
125
+ var msg = message;
126
+ return __assign(__assign({}, msg), { recommendationResult: (_b = msg.recommendationResult) === null || _b === void 0 ? void 0 : _b.map(function (rec) { return (__assign(__assign({}, rec), { results: Array.from(rec.results || []).map(serializeProduct) })); }) });
127
+ }
128
+ case 'productQuery': {
129
+ var msg = message;
130
+ return __assign(__assign({}, msg), { sourceProduct: serializeProduct(msg.sourceProduct) });
131
+ }
132
+ default:
133
+ return message;
134
+ }
135
+ });
136
+ }
137
+ var ChatSessionStore = /** @class */ (function () {
138
+ function ChatSessionStore(params) {
139
+ this.chat = [];
140
+ this.actions = [];
141
+ this.attachments = new ChatAttachmentStore_1.ChatAttachmentStore();
142
+ this.comparisons = new ChatCompareStore_1.ChatCompareStore();
143
+ this.feedbacks = [];
144
+ this.sessionFeedback = null;
145
+ this.feedbackDismissed = false;
146
+ this.feedbackJustGiven = false;
147
+ this.createdAt = new Date();
148
+ this.requestType = '';
149
+ this.dismissedSideChatMessageId = null;
150
+ this.activeMessageId = null;
151
+ this.sessionLimitReached = false;
152
+ /** Whether raw stored results have been hydrated into Product/SearchResultStore instances. */
153
+ this.hydrated = true;
154
+ this.saveTimerId = null;
155
+ var _a = params.data || {}, id = _a.id, sessionId = _a.sessionId, chat = _a.chat, attachments = _a.attachments, actions = _a.actions, feedbacks = _a.feedbacks, sessionFeedback = _a.sessionFeedback, feedbackDismissed = _a.feedbackDismissed, createdAt = _a.createdAt, sessionEndTime = _a.sessionEndTime, committedComparisons = _a.committedComparisons;
156
+ var stores = params.stores;
157
+ this.id = id || (0, uuid_1.v4)();
158
+ this.sessionId = sessionId;
159
+ this.storage = stores.storage;
160
+ this.actions = actions || [];
161
+ this.createdAt = createdAt ? new Date(createdAt) : new Date();
162
+ this.sessionEndTime = sessionEndTime ? new Date(sessionEndTime) : undefined;
163
+ this.feedbacks = feedbacks || [];
164
+ this.sessionFeedback = sessionFeedback || null;
165
+ this.feedbackDismissed = feedbackDismissed || false;
166
+ // if chat and attachments are passed, load them
167
+ if (chat && chat.length > 0) {
168
+ // productQuery messages only exist to drive the side-chat panel for an
169
+ // in-flight productQuery click; they must not be rehydrated on reload
170
+ // or the side chat would re-open without the matching primary-chat state
171
+ this.chat = chat.filter(function (message) { return message.messageType !== 'productQuery'; });
172
+ }
173
+ if (attachments && attachments.length > 0) {
174
+ this.attachments.hydrate(attachments);
175
+ // Any attachment already referenced by a sent user message is no longer
176
+ // pending — transition it from 'active' to 'saved' so it stops appearing
177
+ // in the attachment context bar while remaining available via get(id) for
178
+ // rendering inside historical user messages.
179
+ var usedAttachmentIds_1 = new Set();
180
+ this.chat.forEach(function (msg) {
181
+ if (msg.messageType === 'user' && msg.attachments) {
182
+ msg.attachments.forEach(function (id) { return usedAttachmentIds_1.add(id); });
183
+ }
184
+ });
185
+ this.attachments.items.forEach(function (item) {
186
+ if ((item.state === 'active' || item.state === 'attached') && usedAttachmentIds_1.has(item.id)) {
187
+ item.save();
188
+ }
189
+ });
190
+ }
191
+ // restore committed comparisons only if the thread is still anchored
192
+ // to a product comparison — either the last response was a
193
+ // productComparison or the user sent a follow-up before a response
194
+ // arrived (pending state)
195
+ if (committedComparisons && committedComparisons.length > 0) {
196
+ var EXCLUDED_MESSAGE_TYPES_1 = ['topicDrift'];
197
+ var visibleMessages = this.chat.filter(function (message) { return !EXCLUDED_MESSAGE_TYPES_1.includes(message.messageType); });
198
+ var lastMessage = visibleMessages[visibleMessages.length - 1];
199
+ if ((lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.messageType) === 'productComparison' || (lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.messageType) === 'user') {
200
+ this.comparisons.committedItems = committedComparisons;
201
+ }
202
+ }
203
+ (0, mobx_1.makeObservable)(this, {
204
+ chat: mobx_1.observable,
205
+ requestType: mobx_1.observable,
206
+ actions: mobx_1.observable,
207
+ attachments: mobx_1.observable,
208
+ feedbacks: mobx_1.observable,
209
+ sessionFeedback: mobx_1.observable,
210
+ feedbackDismissed: mobx_1.observable,
211
+ feedbackJustGiven: mobx_1.observable,
212
+ dismissedSideChatMessageId: mobx_1.observable,
213
+ activeMessageId: mobx_1.observable,
214
+ sessionLimitReached: mobx_1.observable,
215
+ activeMessage: mobx_1.computed,
216
+ });
217
+ }
218
+ ChatSessionStore.prototype.dismissSideChat = function () {
219
+ // clear the override first so the fallback (last eligible message) is what we
220
+ // dismiss — otherwise closing while viewing an older message would leave the
221
+ // last message undismissed and the side chat would auto-reopen on it
222
+ this.activeMessageId = null;
223
+ var fallback = this.activeMessage;
224
+ if (fallback) {
225
+ this.dismissedSideChatMessageId = fallback.id;
226
+ }
227
+ };
228
+ ChatSessionStore.prototype.setActiveMessage = function (id) {
229
+ this.activeMessageId = id;
230
+ this.dismissedSideChatMessageId = null;
231
+ };
232
+ ChatSessionStore.prototype.pushProductQueryMessage = function (result) {
233
+ var _a, _b;
234
+ // capture the side-chat message that was active at click time so a back action
235
+ // can restore it even when it's not the last message in the chat
236
+ var sourceMessageId = (_a = this.activeMessage) === null || _a === void 0 ? void 0 : _a.id;
237
+ // drop any trailing productQuery so a fresh productQuery click replaces
238
+ // the side-chat target rather than stacking up
239
+ while (this.chat.length > 0 && ((_b = this.chat[this.chat.length - 1]) === null || _b === void 0 ? void 0 : _b.messageType) === 'productQuery') {
240
+ this.chat.pop();
241
+ }
242
+ this.chat.push({
243
+ id: (0, uuid_1.v4)(),
244
+ messageType: 'productQuery',
245
+ sourceProduct: result,
246
+ sourceMessageId: sourceMessageId,
247
+ });
248
+ // re-show the side chat in case the user previously dismissed it
249
+ this.dismissedSideChatMessageId = null;
250
+ this.activeMessageId = null;
251
+ this.save();
252
+ };
253
+ ChatSessionStore.prototype.popProductQueryMessage = function (restoreActiveMessageId) {
254
+ var _a;
255
+ while (this.chat.length > 0 && ((_a = this.chat[this.chat.length - 1]) === null || _a === void 0 ? void 0 : _a.messageType) === 'productQuery') {
256
+ this.chat.pop();
257
+ }
258
+ this.activeMessageId = restoreActiveMessageId || null;
259
+ this.save();
260
+ };
261
+ Object.defineProperty(ChatSessionStore.prototype, "isExpired", {
262
+ get: function () {
263
+ // Prefer the server-provided end time (returned by chatInit); fall back to a
264
+ // 24-hour window from createdAt for sessions persisted before that field existed.
265
+ if (this.sessionEndTime) {
266
+ return Date.now() > this.sessionEndTime.getTime();
267
+ }
268
+ var ONE_DAY = 24 * 60 * 60 * 1000;
269
+ return Date.now() - this.createdAt.getTime() > ONE_DAY;
270
+ },
271
+ enumerable: false,
272
+ configurable: true
273
+ });
274
+ Object.defineProperty(ChatSessionStore.prototype, "topicDrift", {
275
+ get: function () {
276
+ var lastMessage = this.chat[this.chat.length - 1];
277
+ return (lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.messageType) === 'topicDrift' ? lastMessage : null;
278
+ },
279
+ enumerable: false,
280
+ configurable: true
281
+ });
282
+ Object.defineProperty(ChatSessionStore.prototype, "activeMessage", {
283
+ get: function () {
284
+ var EXCLUDED_MESSAGE_TYPES = ['topicDrift', 'productAnswer'];
285
+ if (this.activeMessageId) {
286
+ // Walk backward — the override is usually near the end
287
+ for (var i = this.chat.length - 1; i >= 0; i--) {
288
+ var m = this.chat[i];
289
+ if (m.id === this.activeMessageId && !EXCLUDED_MESSAGE_TYPES.includes(m.messageType)) {
290
+ return m;
291
+ }
292
+ }
293
+ }
294
+ // Find the last eligible message by iterating backwards
295
+ var lastMessage = null;
296
+ for (var i = this.chat.length - 1; i >= 0; i--) {
297
+ if (!EXCLUDED_MESSAGE_TYPES.includes(this.chat[i].messageType)) {
298
+ lastMessage = this.chat[i];
299
+ break;
300
+ }
301
+ }
302
+ // When the user sends a follow-up while in a productQuery flow (e.g. "discuss product"),
303
+ // the last visible message becomes a 'user' message which would close the secondary panel.
304
+ // Instead, keep the productQuery message as the active side-chat target so the product
305
+ // information panel stays open during and after the request.
306
+ if ((lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.messageType) === 'user' && this.requestType === 'productQuery') {
307
+ for (var i = this.chat.length - 1; i >= 0; i--) {
308
+ var m = this.chat[i];
309
+ if (m.messageType === 'productQuery' && !EXCLUDED_MESSAGE_TYPES.includes(m.messageType)) {
310
+ return m;
311
+ }
312
+ }
313
+ }
314
+ return lastMessage || null;
315
+ },
316
+ enumerable: false,
317
+ configurable: true
318
+ });
319
+ ChatSessionStore.prototype.dismissTopicDrift = function () {
320
+ this.chat = this.chat.filter(function (m) { return m.messageType !== 'topicDrift'; });
321
+ this.save();
322
+ };
323
+ ChatSessionStore.prototype.handleTopicDrift = function () {
324
+ var lastUserMessage;
325
+ for (var i = this.chat.length - 1; i >= 0; i--) {
326
+ if (this.chat[i].messageType === 'user') {
327
+ lastUserMessage = this.chat[i];
328
+ break;
329
+ }
330
+ }
331
+ var messageText = lastUserMessage === null || lastUserMessage === void 0 ? void 0 : lastUserMessage.text;
332
+ // remove all topicDrift messages and the last user message that triggered the drift
333
+ if (lastUserMessage) {
334
+ var lastUserIndex = this.chat.lastIndexOf(lastUserMessage);
335
+ this.chat = this.chat.slice(0, lastUserIndex);
336
+ }
337
+ else {
338
+ this.chat = this.chat.filter(function (m) { return m.messageType !== 'topicDrift'; });
339
+ }
340
+ this.save();
341
+ return messageText;
342
+ };
343
+ ChatSessionStore.prototype.reset = function () {
344
+ this.attachments.reset();
345
+ this.chat = [];
346
+ this.actions = [];
347
+ this.feedbacks = [];
348
+ this.sessionFeedback = null;
349
+ };
350
+ /** Persist the session to storage immediately (synchronous). */
351
+ ChatSessionStore.prototype.saveImmediate = function () {
352
+ if (this.saveTimerId !== null) {
353
+ clearTimeout(this.saveTimerId);
354
+ this.saveTimerId = null;
355
+ }
356
+ this.storage.set("chats.".concat(this.id), {
357
+ sessionId: this.sessionId,
358
+ chat: serializeChatForStorage(this.chat),
359
+ attachments: serializeAttachmentsForStorage(this.attachments.items),
360
+ actions: this.actions,
361
+ feedbacks: this.feedbacks,
362
+ sessionFeedback: this.sessionFeedback,
363
+ feedbackDismissed: this.feedbackDismissed,
364
+ createdAt: this.createdAt,
365
+ sessionEndTime: this.sessionEndTime,
366
+ committedComparisons: this.comparisons.committedItems,
367
+ });
368
+ };
369
+ /**
370
+ * Schedule a save — multiple calls within the debounce window are coalesced
371
+ * into a single localStorage write.
372
+ */
373
+ ChatSessionStore.prototype.save = function () {
374
+ var _this = this;
375
+ if (this.saveTimerId !== null) {
376
+ clearTimeout(this.saveTimerId);
377
+ }
378
+ this.saveTimerId = setTimeout(function () {
379
+ _this.saveTimerId = null;
380
+ _this.saveImmediate();
381
+ }, 0);
382
+ };
383
+ /** Remove oldest stored sessions when exceeding the limit. */
384
+ ChatSessionStore.pruneStoredSessions = function (storage, maxSessions) {
385
+ if (maxSessions === void 0) { maxSessions = 10; }
386
+ var storedChats = storage.get('chats');
387
+ if (storedChats) {
388
+ var chatIds = Object.keys(storedChats);
389
+ if (chatIds.length > maxSessions) {
390
+ chatIds
391
+ .sort(function (a, b) {
392
+ var _a, _b;
393
+ var aTime = new Date(((_a = storedChats[a]) === null || _a === void 0 ? void 0 : _a.createdAt) || 0).getTime();
394
+ var bTime = new Date(((_b = storedChats[b]) === null || _b === void 0 ? void 0 : _b.createdAt) || 0).getTime();
395
+ return aTime - bTime;
396
+ })
397
+ .slice(0, chatIds.length - maxSessions)
398
+ .forEach(function (id) {
399
+ storage.set("chats.".concat(id), null);
400
+ });
401
+ }
402
+ }
403
+ };
404
+ /** Re-wrap raw stored results as Product / SearchResultStore instances. */
405
+ ChatSessionStore.prototype.hydrateResults = function (meta) {
406
+ this.chat.forEach(function (message) {
407
+ var _a, _b, _c, _d;
408
+ if (message.messageType === 'productSearchResult') {
409
+ var msg = message;
410
+ if (((_a = msg.results) === null || _a === void 0 ? void 0 : _a.length) && !(msg.results[0] instanceof SearchResultStore_1.Product)) {
411
+ msg.results = createChatResultStore(msg.results, meta);
412
+ }
413
+ }
414
+ else if (message.messageType === 'inspirationResult') {
415
+ var msg = message;
416
+ (_b = msg.inspirationSections) === null || _b === void 0 ? void 0 : _b.forEach(function (section) {
417
+ var _a;
418
+ if (((_a = section.products) === null || _a === void 0 ? void 0 : _a.length) && !(section.products[0] instanceof SearchResultStore_1.Product)) {
419
+ section.products = createChatResultStore(section.products, meta);
420
+ }
421
+ });
422
+ }
423
+ else if (message.messageType === 'productAnswer') {
424
+ var msg = message;
425
+ if (msg.sourceProduct && !(msg.sourceProduct instanceof SearchResultStore_1.Product)) {
426
+ msg.sourceProduct = createChatProduct(msg.sourceProduct, meta);
427
+ }
428
+ }
429
+ else if (message.messageType === 'productComparison') {
430
+ var msg = message;
431
+ if (((_c = msg.searchResults) === null || _c === void 0 ? void 0 : _c.length) && !(msg.searchResults[0] instanceof SearchResultStore_1.Product)) {
432
+ msg.searchResults = createChatResultStore(msg.searchResults, meta);
433
+ }
434
+ }
435
+ else if (message.messageType === 'productRecommendation') {
436
+ var msg = message;
437
+ (_d = msg.recommendationResult) === null || _d === void 0 ? void 0 : _d.forEach(function (rec) {
438
+ var _a;
439
+ if (((_a = rec.results) === null || _a === void 0 ? void 0 : _a.length) && !(rec.results[0] instanceof SearchResultStore_1.Product)) {
440
+ rec.results = createChatResultStore(rec.results, meta);
441
+ }
442
+ });
443
+ }
444
+ });
445
+ };
446
+ ChatSessionStore.prototype.request = function (request, filterLabels) {
447
+ var _this = this;
448
+ var _a, _b;
449
+ // clear the questions on new request
450
+ this.actions = [];
451
+ this.requestType = request.data.requestType;
452
+ this.activeMessageId = null;
453
+ // remove any attachments that failed to upload
454
+ var errorAttachments = this.attachments.items.filter(function (item) { return item.state === 'error'; });
455
+ errorAttachments.forEach(function (item) { return _this.attachments.items.splice(_this.attachments.items.indexOf(item), 1); });
456
+ var attachments = [];
457
+ if (request.data.requestType === 'productSearch') {
458
+ var searchFilters = request.data.searchFilters;
459
+ if (searchFilters && searchFilters.length > 0) {
460
+ var filterTextArray_1 = [];
461
+ searchFilters.forEach(function (filter) {
462
+ var _a;
463
+ var labelEntry = filterLabels === null || filterLabels === void 0 ? void 0 : filterLabels[filter.key];
464
+ (_a = filter.options) === null || _a === void 0 ? void 0 : _a.forEach(function (option) {
465
+ var _a, _b;
466
+ var facetLabel = (labelEntry === null || labelEntry === void 0 ? void 0 : labelEntry.facetLabel) || filter.key;
467
+ if ('low' in option || 'high' in option) {
468
+ var low = (_a = option.low) !== null && _a !== void 0 ? _a : '*';
469
+ var high = (_b = option.high) !== null && _b !== void 0 ? _b : '*';
470
+ filterTextArray_1.push("".concat(facetLabel, " ").concat(low, "-").concat(high));
471
+ }
472
+ else {
473
+ var key = option.key;
474
+ var optionLabel = (labelEntry === null || labelEntry === void 0 ? void 0 : labelEntry.values[key]) || key;
475
+ filterTextArray_1.push("".concat(facetLabel, " ").concat(optionLabel));
476
+ }
477
+ });
478
+ });
479
+ this.chat.push({
480
+ id: (0, uuid_1.v4)(),
481
+ messageType: 'user',
482
+ attachments: attachments.length > 0 ? attachments : undefined,
483
+ text: "Filter by ".concat(filterTextArray_1.join(' and ')),
484
+ requestType: request.data.requestType,
485
+ request: request.data, // request is added here to conditionally display different text in MessageUser
486
+ });
487
+ }
488
+ else if (request.data.searchTerm) {
489
+ // for when a query is clicked from ChatInspirationResultMessage
490
+ this.chat.push({
491
+ id: (0, uuid_1.v4)(),
492
+ messageType: 'user',
493
+ text: request.data.searchTerm,
494
+ requestType: request.data.requestType,
495
+ request: request.data, // request is added here to conditionally display different text in MessageUser
496
+ });
497
+ }
498
+ }
499
+ else if ('message' in request.data && request.data.message) {
500
+ if (request.data.requestType === 'imageSearch') {
501
+ var imageId_1 = request.data.attachedImageId;
502
+ var attachedImage = this.attachments.attached.find(function (item) { return item.type == 'image' && item.imageId == imageId_1; });
503
+ if (attachedImage) {
504
+ attachments.push(attachedImage.id);
505
+ attachedImage.activate();
506
+ }
507
+ }
508
+ else if (request.data.requestType === 'productQuery') {
509
+ var productId_1 = request.data.productId;
510
+ var attachedProduct = this.attachments.attached.find(function (item) { return item.type == 'product' && item.productId == productId_1; });
511
+ if (attachedProduct) {
512
+ attachments.push(attachedProduct.id);
513
+ attachedProduct.activate();
514
+ }
515
+ }
516
+ else if (request.data.requestType === 'productComparison') {
517
+ this.comparisons.compared.forEach(function (item) {
518
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
519
+ var d = ((_a = item.result) === null || _a === void 0 ? void 0 : _a.display) || item.result;
520
+ var attachment = _this.attachments.add({
521
+ type: 'product',
522
+ requestType: 'productComparison',
523
+ productId: item.result.id,
524
+ name: (_c = (_b = d.mappings) === null || _b === void 0 ? void 0 : _b.core) === null || _c === void 0 ? void 0 : _c.name,
525
+ thumbnailUrl: ((_e = (_d = d.mappings) === null || _d === void 0 ? void 0 : _d.core) === null || _e === void 0 ? void 0 : _e.thumbnailImageUrl) || ((_g = (_f = d.mappings) === null || _f === void 0 ? void 0 : _f.core) === null || _g === void 0 ? void 0 : _g.imageUrl) || ((_j = (_h = d.mappings) === null || _h === void 0 ? void 0 : _h.core) === null || _j === void 0 ? void 0 : _j.parentImageUrl),
526
+ });
527
+ if (attachment) {
528
+ attachments.push(attachment.id);
529
+ attachment.activate();
530
+ }
531
+ });
532
+ }
533
+ this.chat.push({
534
+ id: (0, uuid_1.v4)(),
535
+ messageType: 'user',
536
+ attachments: attachments.length > 0 ? attachments : undefined,
537
+ text: request.data.message,
538
+ requestType: request.data.requestType,
539
+ });
540
+ }
541
+ else if (((_a = request.data) === null || _a === void 0 ? void 0 : _a.requestType) === 'productSimilar') {
542
+ var attachedSimilarProduct = this.attachments.attached.find(function (item) { return item.type == 'product' && item.requestType == 'productSimilar'; });
543
+ if (attachedSimilarProduct) {
544
+ attachments.push(attachedSimilarProduct.id);
545
+ attachedSimilarProduct.activate();
546
+ this.chat.push({
547
+ id: (0, uuid_1.v4)(),
548
+ messageType: 'user',
549
+ attachments: attachments.length > 0 ? attachments : undefined,
550
+ text: "Show similar products to \"".concat(attachedSimilarProduct.name || attachedSimilarProduct.productId, "\""),
551
+ requestType: request.data.requestType,
552
+ });
553
+ }
554
+ }
555
+ else if (((_b = request.data) === null || _b === void 0 ? void 0 : _b.requestType) === 'productComparison') {
556
+ var productNames_1 = [];
557
+ this.comparisons.compared.forEach(function (item) {
558
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
559
+ var d = ((_a = item.result) === null || _a === void 0 ? void 0 : _a.display) || item.result;
560
+ var attachment = _this.attachments.add({
561
+ type: 'product',
562
+ requestType: 'productComparison',
563
+ productId: item.result.id,
564
+ name: (_c = (_b = d.mappings) === null || _b === void 0 ? void 0 : _b.core) === null || _c === void 0 ? void 0 : _c.name,
565
+ thumbnailUrl: ((_e = (_d = d.mappings) === null || _d === void 0 ? void 0 : _d.core) === null || _e === void 0 ? void 0 : _e.thumbnailImageUrl) || ((_g = (_f = d.mappings) === null || _f === void 0 ? void 0 : _f.core) === null || _g === void 0 ? void 0 : _g.imageUrl) || ((_j = (_h = d.mappings) === null || _h === void 0 ? void 0 : _h.core) === null || _j === void 0 ? void 0 : _j.parentImageUrl),
566
+ });
567
+ if (attachment) {
568
+ attachments.push(attachment.id);
569
+ attachment.activate();
570
+ productNames_1.push(attachment.name || attachment.productId);
571
+ }
572
+ });
573
+ if (attachments.length > 0) {
574
+ this.chat.push({
575
+ id: (0, uuid_1.v4)(),
576
+ messageType: 'user',
577
+ attachments: attachments,
578
+ text: "Compare ".concat(productNames_1.map(function (name) { return "\"".concat(name, "\""); }).join(' and ')),
579
+ requestType: request.data.requestType,
580
+ });
581
+ }
582
+ }
583
+ // snapshot the comparison list into the committed list so the
584
+ // header section can clear and the footer can display them
585
+ if (request.data.requestType === 'productComparison') {
586
+ this.comparisons.commit();
587
+ }
588
+ this.save();
589
+ };
590
+ ChatSessionStore.prototype.update = function (data) {
591
+ var _this = this;
592
+ this.sessionId = data.chat.context.sessionId;
593
+ var meta = data.meta;
594
+ data.chat.data.forEach(function (messageData) {
595
+ var _a, _b, _c, _d;
596
+ // check if the data has questions?
597
+ if (messageData.messageType === 'actions') {
598
+ _this.actions.push({
599
+ type: 'actions',
600
+ data: messageData.actions,
601
+ });
602
+ return;
603
+ }
604
+ // convert raw results to Product instances (via SearchResultStore) so
605
+ // display components can use result.display for mask-aware rendering
606
+ if (messageData.messageType === 'productSearchResult' && ((_a = messageData.results) === null || _a === void 0 ? void 0 : _a.length)) {
607
+ messageData.results = createChatResultStore(messageData.results, meta);
608
+ }
609
+ else if (messageData.messageType === 'inspirationResult' && ((_b = messageData.inspirationSections) === null || _b === void 0 ? void 0 : _b.length)) {
610
+ messageData.inspirationSections = messageData.inspirationSections.map(function (section) {
611
+ var _a;
612
+ return (__assign(__assign({}, section), { products: ((_a = section.products) === null || _a === void 0 ? void 0 : _a.length)
613
+ ? createChatResultStore(section.products, meta)
614
+ : section.products }));
615
+ });
616
+ }
617
+ else if (messageData.messageType === 'productAnswer' && messageData.sourceProduct) {
618
+ messageData.sourceProduct = createChatProduct(messageData.sourceProduct, meta);
619
+ }
620
+ else if (messageData.messageType === 'productComparison' && ((_c = messageData.searchResults) === null || _c === void 0 ? void 0 : _c.length)) {
621
+ messageData.searchResults = createChatResultStore(messageData.searchResults, meta);
622
+ }
623
+ else if (messageData.messageType === 'productRecommendation' && ((_d = messageData.recommendationResult) === null || _d === void 0 ? void 0 : _d.length)) {
624
+ messageData.recommendationResult = messageData.recommendationResult.map(function (rec) {
625
+ var _a;
626
+ return (__assign(__assign({}, rec), { results: ((_a = rec.results) === null || _a === void 0 ? void 0 : _a.length) ? createChatResultStore(rec.results, meta) : rec.results }));
627
+ });
628
+ }
629
+ _this.chat.push(messageData);
630
+ });
631
+ this.save();
632
+ };
633
+ return ChatSessionStore;
634
+ }());
635
+ exports.ChatSessionStore = ChatSessionStore;
@@ -1 +1 @@
1
- {"version":3,"file":"SearchFacetStore.d.ts","sourceRoot":"","sources":["../../../../src/Search/Stores/SearchFacetStore.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAClE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,KAAK,EAAE,uBAAuB,EAAE,iBAAiB,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/G,OAAO,KAAK,EACX,sBAAsB,EAEtB,4BAA4B,EAC5B,uCAAuC,EACvC,oCAAoC,EAEpC,6BAA6B,EAC7B,6BAA6B,EAE7B,wCAAwC,EACxC,uCAAuC,EACvC,mBAAmB,EACnB,iBAAiB,EACjB,MAAM,4BAA4B,CAAC;AAEpC,MAAM,MAAM,sBAAsB,GAAG;IACpC,MAAM,EAAE,iBAAiB,GAAG,uBAAuB,CAAC;IACpD,MAAM,EAAE;QACP,OAAO,EAAE,YAAY,CAAC;KACtB,CAAC;IACF,QAAQ,EAAE,aAAa,CAAC;IACxB,IAAI,EAAE;QACL,MAAM,CAAC,EAAE,mBAAmB,CAAC;QAC7B,IAAI,EAAE,iBAAiB,CAAC;K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