@gengage/assistant-fe 0.6.48 → 0.6.49

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.
@@ -1,1491 +0,0 @@
1
- import { i as X } from "./widget-base-CrsfKJMB.js";
2
- var Z = ({ element: t, renderElement: e }) => {
3
- if (!t.children || t.children.length === 0) return null;
4
- const r = document.createElement("div");
5
- for (const n of t.children) {
6
- const o = e(n);
7
- o && r.appendChild(o);
8
- }
9
- return r;
10
- };
11
- function _e(t) {
12
- const e = document.createElement("div");
13
- if (e.className = t.containerClassName, !t.spec.elements[t.spec.root]) return e;
14
- const r = t.unknownRenderer ?? Z, n = (s) => {
15
- const i = t.spec.elements[s];
16
- if (!i) return null;
17
- const c = t.registry[i.type];
18
- return c ? c({
19
- elementId: s,
20
- element: i,
21
- spec: t.spec,
22
- context: t.context,
23
- renderElement: n
24
- }) : r({
25
- elementId: s,
26
- element: i,
27
- spec: t.spec,
28
- context: t.context,
29
- renderElement: n
30
- });
31
- }, o = n(t.spec.root);
32
- return o && e.appendChild(o), e;
33
- }
34
- function he(t, e) {
35
- if (!e) return t;
36
- const r = { ...t };
37
- for (const [n, o] of Object.entries(e))
38
- o && (r[n] = o);
39
- return r;
40
- }
41
- function B(t) {
42
- return typeof t == "object" && t !== null && typeof t.type == "string";
43
- }
44
- function tt(t) {
45
- const e = [];
46
- let r = 0, n = !1, o = !1, s = 0;
47
- for (let i = 0; i < t.length; i++) {
48
- const c = t[i];
49
- if (o) {
50
- o = !1;
51
- continue;
52
- }
53
- if (c === "\\" && n) {
54
- o = !0;
55
- continue;
56
- }
57
- if (c === '"') {
58
- n = !n;
59
- continue;
60
- }
61
- n || (c === "{" && r++, c === "}" && (r--, r === 0 && (e.push(t.slice(s, i + 1)), s = i + 1)));
62
- }
63
- return e.length > 0 ? e : [t];
64
- }
65
- function M(t, e) {
66
- const r = t.trim();
67
- if (!r || r.startsWith(":")) return !1;
68
- const n = r.startsWith("data: ") ? r.slice(6) : r;
69
- if (n === "[DONE]")
70
- return e.onDone?.(), !0;
71
- try {
72
- const o = JSON.parse(n);
73
- return B(o) ? (X("stream", `event: ${o.type}`, o), e.onEvent(o), o.type === "done" ? (e.onDone?.(), !0) : !1) : !1;
74
- } catch {
75
- const o = tt(n);
76
- if (o.length > 1) {
77
- for (const s of o) try {
78
- const i = JSON.parse(s);
79
- if (!B(i)) continue;
80
- if (e.onEvent(i), i.type === "done")
81
- return e.onDone?.(), !0;
82
- } catch {
83
- }
84
- return !1;
85
- }
86
- return !1;
87
- }
88
- }
89
- async function et(t, e) {
90
- if (!t.ok) {
91
- e.onError?.(/* @__PURE__ */ new Error(`HTTP ${t.status}: ${t.statusText}`));
92
- return;
93
- }
94
- if (!t.body) {
95
- e.onError?.(/* @__PURE__ */ new Error("Response body is null — streaming not supported"));
96
- return;
97
- }
98
- const r = t.body.getReader(), n = new TextDecoder("utf-8");
99
- let o = "";
100
- const s = e.idleTimeoutMs ?? 6e4, i = /* @__PURE__ */ Symbol("idle-timeout");
101
- let c = !1;
102
- try {
103
- for (; ; ) {
104
- const a = r.read();
105
- let d;
106
- const y = await (s > 0 ? Promise.race([a.then((p) => (clearTimeout(d), p)), new Promise((p) => {
107
- d = setTimeout(() => p(i), s);
108
- })]) : a);
109
- if (y === i) {
110
- c = !0, await r.cancel();
111
- break;
112
- }
113
- const { done: u, value: m } = y;
114
- if (u) break;
115
- o += n.decode(m, { stream: !0 });
116
- const f = o.split(`
117
- `);
118
- o = f.pop() ?? "";
119
- for (const p of f) if (M(p, e)) return;
120
- }
121
- if (o.trim() && M(o, e))
122
- return;
123
- } catch (a) {
124
- if (a instanceof DOMException && a.name === "AbortError") return;
125
- e.onError?.(a instanceof Error ? a : new Error(String(a)));
126
- } finally {
127
- c || r.releaseLock();
128
- }
129
- e.onDone?.();
130
- }
131
- function be(t, e, r) {
132
- const n = new AbortController(), o = r.signal ? rt([r.signal, n.signal]) : n.signal;
133
- return fetch(t, {
134
- method: "POST",
135
- headers: {
136
- "Content-Type": "application/json",
137
- ...r.headers
138
- },
139
- body: JSON.stringify(e),
140
- signal: o
141
- }).then((s) => et(s, {
142
- ...r,
143
- signal: o
144
- })).catch((s) => {
145
- s instanceof DOMException && s.name === "AbortError" || r.onError?.(s instanceof Error ? s : new Error(String(s)));
146
- }), n;
147
- }
148
- function rt(t) {
149
- const e = new AbortController(), r = [];
150
- for (const n of t) {
151
- if (n.aborted)
152
- return e.abort(n.reason), e.signal;
153
- const o = () => e.abort(n.reason);
154
- n.addEventListener("abort", o, { once: !0 }), r.push(() => n.removeEventListener("abort", o));
155
- }
156
- return e.signal.addEventListener("abort", () => {
157
- for (const n of r) n();
158
- }, { once: !0 }), e.signal;
159
- }
160
- var U = 3, nt = (t, e) => {
161
- const r = t.replace(/\s+/g, " ").trim().replace(/^[,.;:!?•-]+|[,.;:!?•-]+$/g, "");
162
- if (!r) return "";
163
- const n = e?.stripLeadingStopWords ? r.replace(/^(?:daha|için)\s+/i, "").trim() : r;
164
- return n ? n.split(/\s+/).slice(0, 3).join(" ").trim() : "";
165
- }, N = (t, e) => t ? t.split(/[•,;:/()]|(?:\sve\s)|(?:\sand\s)|(?:\sile\s)|(?:\sfor\s)|(?:\swith\s)/i).map((r) => nt(r, e)).filter(Boolean) : [];
166
- function ot(t) {
167
- const e = (t.display_keywords ?? []).flatMap((o) => N(o, { stripLeadingStopWords: !0 })), r = e.filter((o, s) => e.indexOf(o) === s);
168
- if (r.length > 0) return r.slice(0, U);
169
- const n = [...N(t.chosen_attribute, { stripLeadingStopWords: !0 }), ...N(t.short_name)];
170
- return n.filter((o, s) => n.indexOf(o) === s).slice(0, U);
171
- }
172
- function st(t) {
173
- return ot(t).join(" • ");
174
- }
175
- function it(t) {
176
- return t === "beauty_consulting" || t === "account_expert";
177
- }
178
- function E(...t) {
179
- for (const e of t) {
180
- if (typeof e != "string") continue;
181
- const r = e.trim();
182
- if (r.length > 0) return r;
183
- }
184
- }
185
- function T(t) {
186
- return Array.isArray(t) ? t.filter((e) => typeof e == "string" && e.trim().length > 0).map((e) => e.trim()) : [];
187
- }
188
- function I(t) {
189
- const e = { ...t }, r = E(e.sku), n = T(e.group_skus);
190
- if (r ? e.sku = r : delete e.sku, n.length > 0 ? e.group_skus = n : delete e.group_skus, r || n.length > 0) delete e.image_url;
191
- else {
192
- const o = E(e.image_url);
193
- o ? e.image_url = o : delete e.image_url;
194
- }
195
- return e;
196
- }
197
- function ke(t) {
198
- if (typeof t.type != "string") return null;
199
- if (at(t)) return t;
200
- const e = t;
201
- switch (e.type) {
202
- case "outputText":
203
- return ct(e);
204
- case "suggestedActions":
205
- return ut(e);
206
- case "productList":
207
- return dt(e);
208
- case "productDetails":
209
- return lt(e);
210
- case "productDetailsSimilars":
211
- return pt(e);
212
- case "comparisonTable":
213
- return ft(e);
214
- case "context":
215
- return _t(e);
216
- case "chatStreamEnd":
217
- return ht();
218
- case "loading":
219
- return bt(e);
220
- case "panelLoading":
221
- return kt(e);
222
- case "similarLoading":
223
- return St(e);
224
- case "redirect":
225
- return At(e);
226
- case "error":
227
- return wt(e);
228
- case "dummy":
229
- return xt(e);
230
- case "launcherAction":
231
- return Pt(e);
232
- case "text":
233
- return v(e);
234
- case "productItem":
235
- return Et(e);
236
- case "text_image":
237
- return F(e);
238
- case "quick_qna":
239
- return K(e);
240
- case "reviewHighlights":
241
- return Tt(e);
242
- case "aiProductSuggestions":
243
- return Dt(e);
244
- case "aiProductGroupings":
245
- return Nt(e);
246
- case "aiSuggestedSearches":
247
- return Ot(e);
248
- case "prosAndCons":
249
- return Ct(e);
250
- case "getGroundingReview":
251
- return vt(e);
252
- case "voice":
253
- return jt(e);
254
- case "visitorDataResponse":
255
- return It(e);
256
- case "productListPreview":
257
- return qt();
258
- case "groupList":
259
- return Bt(e);
260
- case "formGetInfo":
261
- case "formTestDrive":
262
- case "formServiceRequest":
263
- case "launchFormPage":
264
- return Mt(e);
265
- case "launcherContent":
266
- return zt(e);
267
- case "handoff":
268
- return Ut(e);
269
- case "uiSpec":
270
- return Jt(e);
271
- default:
272
- return null;
273
- }
274
- }
275
- function at(t) {
276
- const e = t.type;
277
- if (typeof e != "string") return !1;
278
- switch (e) {
279
- case "metadata":
280
- return typeof t.sessionId == "string" && typeof t.model == "string";
281
- case "text_chunk":
282
- return typeof t.content == "string";
283
- case "ui_spec": {
284
- const r = t.widget;
285
- if (r !== "chat" && r !== "qna" && r !== "simrel") return !1;
286
- const n = b(t.spec);
287
- return n ? typeof n.root == "string" && b(n.elements) !== null : !1;
288
- }
289
- case "action": {
290
- const r = b(t.action);
291
- return r !== null && typeof r.kind == "string";
292
- }
293
- case "error":
294
- return typeof t.code == "string" && typeof t.message == "string";
295
- case "done":
296
- return !0;
297
- default:
298
- return !1;
299
- }
300
- }
301
- function ct(t) {
302
- const e = l(t.payload.text, t.payload.plain_text) ?? "", r = l(t.payload.plain_text, t.payload.text) ?? e;
303
- if (t.payload.is_error) return {
304
- type: "error",
305
- code: "BACKEND_ERROR",
306
- message: r || "Backend returned an error"
307
- };
308
- const n = {
309
- type: "text_chunk",
310
- content: e,
311
- final: !0
312
- }, o = t.payload.sku_to_product_item;
313
- o && typeof o == "object" && !Array.isArray(o) && (n.skuToProductItem = o);
314
- const s = t.payload.product_mentions;
315
- Array.isArray(s) && s.length > 0 && (n.productMentions = s.map((a) => {
316
- if (typeof a != "object" || a === null || typeof a.sku != "string") return null;
317
- const d = typeof a.short_name == "string" ? a.short_name : "";
318
- if (!d) return null;
319
- const y = (typeof a.url == "string" && a.url.trim() ? a.url.trim() : void 0) ?? (() => {
320
- const u = n.skuToProductItem?.[a.sku];
321
- return typeof u?.url == "string" && u.url.trim() ? u.url.trim() : void 0;
322
- })();
323
- return y ? {
324
- sku: a.sku,
325
- short_name: d,
326
- url: y
327
- } : {
328
- sku: a.sku,
329
- short_name: d
330
- };
331
- }).filter((a) => a !== null));
332
- const i = t.payload.conversation_mode;
333
- typeof i == "string" && i && (n.conversationMode = i);
334
- const c = t.payload.render_hint;
335
- return typeof c == "string" && c && (n.renderHint = c), t.payload.kvkk === !0 && (n.kvkk = !0), n;
336
- }
337
- function ut(t) {
338
- return V((t.payload.actions ?? []).map((e) => {
339
- const r = l(e.title) ?? "", n = k(e.requestDetails, r), o = n ? {
340
- label: r,
341
- action: n
342
- } : null;
343
- return o ? (typeof e.icon == "string" && (o.icon = e.icon), typeof e.image == "string" && (o.image = e.image), o) : null;
344
- }).filter(C), "chat");
345
- }
346
- function dt(t) {
347
- const e = Array.isArray(t.payload.grouped_assignment) ? t.payload.grouped_assignment : [], r = (Array.isArray(t.payload.style_variations) ? t.payload.style_variations : []).map((c) => {
348
- const a = l(c.style_label);
349
- if (!a) return null;
350
- const d = l(c.style_mood), y = l(c.image_url ?? void 0), u = (Array.isArray(c.product_list) ? c.product_list : []).map((f) => {
351
- const p = b(f);
352
- return p ? j(b(p.product_detail) ?? b(p.product) ?? p) : null;
353
- }).filter(C), m = Array.isArray(c.recommendation_groups) ? c.recommendation_groups.map((f) => ({
354
- label: l(f.label) ?? "",
355
- reason: l(f.reason) ?? "",
356
- skus: Array.isArray(f.skus) ? f.skus.filter((p) => typeof p == "string" && p.trim().length > 0) : []
357
- })) : [];
358
- return {
359
- style_label: a,
360
- style_mood: d ?? "",
361
- ...y ? { image_url: y } : {},
362
- ...typeof c.status == "string" ? { status: c.status } : {},
363
- product_list: u,
364
- recommendation_groups: m
365
- };
366
- }).filter(C), n = t.payload.product_list ?? [], o = it(t.payload.source) && r.length > 0 ? r[0].product_list : null, s = o ? Gt(o, "chat") : e.length > 0 ? Ht(e, n, "chat") ?? R(n, "chat") : R(n, "chat");
367
- s.panelHint = "panel";
368
- const i = s.spec.elements[s.spec.root];
369
- if (i) {
370
- const c = {};
371
- typeof t.payload.offset == "number" && (c.offset = t.payload.offset), typeof t.payload.end_of_list == "boolean" && (c.endOfList = t.payload.end_of_list), typeof t.payload.title == "string" && (c.panelTitle = t.payload.title), typeof t.payload.source == "string" && (c.source = t.payload.source), r.length > 0 && (c.styleVariations = r), t.payload.replace_panel === !0 && (c.replacePanel = !0), (t.payload.ranking_state === "pending" || t.payload.ranking_state === "final") && (c.rankingState = t.payload.ranking_state);
372
- const a = t.payload.sequence_id ?? t.payload.search_id;
373
- typeof a == "string" && a.length > 0 && (c.sequenceId = a), Object.keys(c).length > 0 && (i.props = {
374
- ...i.props,
375
- ...c
376
- });
377
- }
378
- return s;
379
- }
380
- function lt(t) {
381
- const e = t.payload.productDetails;
382
- if (!e) return Ft("chat");
383
- const r = w(e);
384
- return {
385
- type: "ui_spec",
386
- widget: "chat",
387
- spec: {
388
- root: "root",
389
- elements: { root: {
390
- type: "ProductDetailsPanel",
391
- props: { product: {
392
- ...e,
393
- ...r
394
- } }
395
- } }
396
- },
397
- panelHint: "panel"
398
- };
399
- }
400
- function pt(t) {
401
- const e = R(t.payload.similarProducts ?? [], "chat"), r = e.spec.elements[e.spec.root];
402
- return r && (r.props = {
403
- ...r.props,
404
- similarsAppend: !0
405
- }), {
406
- ...e,
407
- panelHint: "panel"
408
- };
409
- }
410
- function ft(t) {
411
- const e = t.payload.multiple_product_details ?? [], r = t.payload, n = {
412
- key_differences: r.key_differences,
413
- recommended_choice: r.recommended_choice,
414
- recommended_choice_sku: r.recommended_choice_sku,
415
- special_considerations: r.special_considerations,
416
- criteria_view: r.criteria_view,
417
- criteria_view_short: r.criteria_view_short,
418
- compared_field_names: r.compared_field_names,
419
- winner_product: r.winner_product,
420
- winner_hits: r.winner_hits,
421
- ...t.payload.product_comparison_framework ?? {}
422
- }, o = t.payload.table, s = t.payload.features_list, i = [];
423
- for (const _ of e) {
424
- const h = w(_);
425
- i.push(h);
426
- }
427
- const c = yt(o, i, n, s);
428
- let a;
429
- n?.recommended_choice_sku ? a = n.recommended_choice_sku : n?.winner_product && n.winner_product.length > 0 && (a = n.winner_product[0]?.product_detail?.sku ?? n.winner_product[0]?.sku);
430
- const d = a ? i.find((_) => _.sku === a) ?? i[0] : i[0], y = [];
431
- if (Array.isArray(n?.key_differences))
432
- for (const _ of n.key_differences) typeof _ == "string" && y.push(_);
433
- const u = W(n?.special_considerations), m = n?.recommended_choice, f = n?.winner_hits, p = {};
434
- for (const _ of i) {
435
- const h = _.sku;
436
- h && (p[h] = {
437
- title: _.name ?? h,
438
- type: "launchSingleProduct",
439
- payload: { sku: h }
440
- });
441
- }
442
- const g = {
443
- products: i,
444
- attributes: c,
445
- highlights: y,
446
- productActions: p
447
- };
448
- return d && (g.recommended = d), u.length > 0 && (g.specialCases = u), m && (g.recommendedText = m), f && (g.winnerHits = f), typeof n?.key_differences == "string" && (g.keyDifferencesHtml = n.key_differences), n?.special_considerations && (g.specialConsiderations = n.special_considerations), {
449
- type: "ui_spec",
450
- widget: "chat",
451
- spec: {
452
- root: "root",
453
- elements: { root: {
454
- type: "ComparisonTable",
455
- props: g
456
- } }
457
- },
458
- panelHint: "panel"
459
- };
460
- }
461
- function yt(t, e, r, n) {
462
- if (!t) return [];
463
- const o = Object.entries(t);
464
- if (o.length === 0) return [];
465
- const s = o[0]?.[1];
466
- if (Array.isArray(s)) {
467
- const u = r.criteria_view ?? r.criteria_view_short ?? {}, m = r.compared_field_names ?? Object.keys(t), f = [];
468
- for (const p of m) {
469
- const g = t[p];
470
- if (!g || !Array.isArray(g)) continue;
471
- const _ = u[p] ?? p;
472
- f.push({
473
- label: _,
474
- values: g.map((h) => typeof h == "string" ? h : String(h ?? ""))
475
- });
476
- }
477
- return f;
478
- }
479
- const i = t, c = e.map((u) => String(u.sku ?? "")).filter((u) => u.length > 0), a = r.criteria_view ?? r.criteria_view_short ?? {}, d = (n && n.length > 0 ? n : r.compared_field_names && r.compared_field_names.length > 0 ? r.compared_field_names : gt(i)).filter((u) => u !== "name" && u !== "name_short" && !u.endsWith("_short")), y = [];
480
- for (const u of d) {
481
- const m = c.map((p) => {
482
- const g = i[p];
483
- if (!g || typeof g != "object") return "";
484
- const _ = g[`${u}_short`], h = g[u];
485
- return mt(_ ?? h);
486
- });
487
- if (m.every((p) => p.length === 0)) continue;
488
- const f = a[u] ?? u;
489
- y.push({
490
- label: f,
491
- values: m
492
- });
493
- }
494
- return y;
495
- }
496
- function gt(t) {
497
- const e = [], r = /* @__PURE__ */ new Set();
498
- for (const n of Object.values(t))
499
- if (!(!n || typeof n != "object"))
500
- for (const o of Object.keys(n))
501
- r.has(o) || (r.add(o), e.push(o));
502
- return e;
503
- }
504
- function mt(t) {
505
- return typeof t == "string" ? t : typeof t == "number" || typeof t == "boolean" ? String(t) : "";
506
- }
507
- function _t(t) {
508
- return {
509
- type: "metadata",
510
- sessionId: "",
511
- model: "",
512
- meta: {
513
- panel: t.payload.panel,
514
- messages: t.payload.messages,
515
- message_id: t.payload.message_id
516
- }
517
- };
518
- }
519
- function ht() {
520
- return { type: "done" };
521
- }
522
- function bt(t) {
523
- return {
524
- type: "metadata",
525
- sessionId: "",
526
- model: "",
527
- meta: {
528
- loading: !0,
529
- loadingText: t.payload.text,
530
- thinkingMessages: t.payload.thinking_messages,
531
- dynamicLoading: t.payload.is_dynamic === !0
532
- }
533
- };
534
- }
535
- function kt(t) {
536
- return {
537
- type: "metadata",
538
- sessionId: "",
539
- model: "",
540
- meta: {
541
- loading: !0,
542
- panelLoading: !0,
543
- panelPendingType: t.payload?.pending_type,
544
- loadingText: t.payload?.text
545
- }
546
- };
547
- }
548
- function St(t) {
549
- return {
550
- type: "metadata",
551
- sessionId: "",
552
- model: "",
553
- meta: {
554
- loading: !0,
555
- similarPanelLoading: !0,
556
- panelPendingType: t.payload?.pending_type,
557
- loadingText: t.payload?.text
558
- }
559
- };
560
- }
561
- function At(t) {
562
- const e = l(t.payload.url);
563
- return e ? {
564
- type: "action",
565
- action: {
566
- kind: "navigate",
567
- url: e,
568
- newTab: t.payload.new_tab === !0
569
- }
570
- } : {
571
- type: "metadata",
572
- sessionId: "",
573
- model: "",
574
- meta: {
575
- redirect: t.payload,
576
- redirectTarget: l(t.payload.to)
577
- }
578
- };
579
- }
580
- function wt(t) {
581
- return {
582
- type: "error",
583
- code: "BACKEND_ERROR",
584
- message: l(t.payload?.text, t.payload?.message, t.payload?.error) ?? "Backend returned an error"
585
- };
586
- }
587
- function xt(t) {
588
- return {
589
- type: "metadata",
590
- sessionId: "",
591
- model: "",
592
- meta: { noop: !0 }
593
- };
594
- }
595
- function Pt(t) {
596
- const e = t.payload ?? {};
597
- if (Array.isArray(e.action_list)) {
598
- const o = { action_list: e.action_list }, s = typeof e.type == "string" ? e.type : void 0, i = typeof e.theme == "string" ? e.theme : void 0;
599
- return s !== void 0 && (o.type = s), i !== void 0 && (o.theme = i), K({
600
- type: "quick_qna",
601
- payload: o
602
- });
603
- }
604
- const r = b(e.action);
605
- if (r) {
606
- const o = { action: r }, s = typeof e.text == "string" ? e.text : void 0, i = typeof e.image_url == "string" ? e.image_url : void 0, c = typeof e.theme == "string" ? e.theme : void 0;
607
- return s !== void 0 && (o.text = s), i !== void 0 && (o.image_url = i), c !== void 0 && (o.theme = c), F({
608
- type: "text_image",
609
- payload: o
610
- });
611
- }
612
- const n = typeof e.text == "string" ? e.text : "";
613
- if (n) {
614
- const o = { text: n }, s = typeof e.type == "string" ? e.type : void 0, i = typeof e.theme == "string" ? e.theme : void 0, c = b(e.payload) ?? void 0;
615
- return s !== void 0 && (o.type = s), c !== void 0 && (o.payload = c), i !== void 0 && (o.theme = i), v({
616
- type: "text",
617
- payload: o
618
- });
619
- }
620
- return {
621
- type: "metadata",
622
- sessionId: "",
623
- model: "",
624
- meta: { launcherAction: e }
625
- };
626
- }
627
- function v(t) {
628
- const e = { text: l(t.payload.text) ?? "" };
629
- return typeof t.payload.theme == "string" && (e.theme = t.payload.theme), t.payload.payload !== void 0 && (e.payload = t.payload.payload), {
630
- type: "ui_spec",
631
- widget: "qna",
632
- spec: {
633
- root: "root",
634
- elements: { root: {
635
- type: "QuestionHeading",
636
- props: e
637
- } }
638
- }
639
- };
640
- }
641
- function Et(t) {
642
- return $t(t.payload, "qna");
643
- }
644
- function F(t) {
645
- const e = l(t.payload.text) ?? "", r = k(t.payload.action, e);
646
- if (r) {
647
- const o = {
648
- label: e || r.title,
649
- action: r
650
- };
651
- return typeof t.payload.image_url == "string" && (o.image = t.payload.image_url), typeof t.payload.theme == "string" && (o.theme = t.payload.theme), {
652
- type: "ui_spec",
653
- widget: "qna",
654
- spec: {
655
- root: "root",
656
- elements: { root: {
657
- type: "ActionButton",
658
- props: o
659
- } }
660
- }
661
- };
662
- }
663
- const n = { text: e };
664
- return typeof t.payload.theme == "string" && (n.theme = t.payload.theme), v({
665
- type: "text",
666
- payload: n
667
- });
668
- }
669
- function K(t) {
670
- return V((t.payload.action_list ?? []).map((e) => {
671
- const r = l(e.title) ?? "", n = k(e.requestDetails, r), o = n ? {
672
- label: r,
673
- action: n
674
- } : null;
675
- return o ? (typeof e.icon == "string" && (o.icon = e.icon), typeof e.image == "string" && (o.image = e.image), o) : null;
676
- }).filter(C), "qna");
677
- }
678
- function Tt(t) {
679
- const e = { reviews: (t.payload.reviews ?? []).map((r) => {
680
- const n = {};
681
- return typeof r.review_class == "string" && (n.review_class = r.review_class), typeof r.review_text == "string" && (n.review_text = r.review_text), (typeof r.review_rating == "string" || typeof r.review_rating == "number") && (n.review_rating = r.review_rating), typeof r.review_tag == "string" && (n.review_tag = r.review_tag), n;
682
- }) };
683
- return typeof t.payload.sku == "string" && (e.sku = t.payload.sku), {
684
- type: "ui_spec",
685
- widget: "chat",
686
- spec: {
687
- root: "root",
688
- elements: { root: {
689
- type: "ReviewHighlights",
690
- props: e
691
- } }
692
- }
693
- };
694
- }
695
- function Ct(t) {
696
- const e = {};
697
- return Array.isArray(t.payload.pros) && (e.pros = t.payload.pros), Array.isArray(t.payload.cons) && (e.cons = t.payload.cons), typeof t.payload.product_name == "string" && (e.productName = t.payload.product_name), {
698
- type: "ui_spec",
699
- widget: "chat",
700
- spec: {
701
- root: "root",
702
- elements: { root: {
703
- type: "ProsAndCons",
704
- props: e
705
- } }
706
- }
707
- };
708
- }
709
- function It(t) {
710
- return {
711
- type: "metadata",
712
- sessionId: "",
713
- model: "",
714
- meta: { visitorDataResponse: t.payload }
715
- };
716
- }
717
- function Dt(t) {
718
- const e = t.payload.product_suggestions ?? [], r = [];
719
- for (let n = 0; n < e.length; n++) {
720
- const o = e[n];
721
- if (!o) continue;
722
- const s = Kt(o);
723
- if (!s) continue;
724
- const i = { product: s }, c = k(o.requestDetails, s.name);
725
- c && (i.action = c), typeof o.role == "string" && (i.role = o.role), typeof o.reason == "string" && (i.reason = o.reason), typeof o.review_highlight == "string" && (i.reviewHighlight = o.review_highlight), Array.isArray(o.labels) && (i.labels = o.labels), typeof o.expert_quality_score == "number" && (i.expertQualityScore = o.expert_quality_score), r.push(i);
726
- }
727
- return r.length === 0 ? {
728
- type: "metadata",
729
- sessionId: "",
730
- model: "",
731
- meta: { aiProductSuggestions: t.payload.product_suggestions ?? [] }
732
- } : {
733
- type: "ui_spec",
734
- widget: "chat",
735
- spec: {
736
- root: "root",
737
- elements: { root: {
738
- type: "AITopPicks",
739
- props: { suggestions: r }
740
- } }
741
- }
742
- };
743
- }
744
- function Nt(t) {
745
- const e = t.payload.product_groupings ?? [], r = [], n = [];
746
- for (let o = 0; o < e.length; o++) {
747
- const s = e[o];
748
- if (!s) continue;
749
- const i = l(s.name) ?? "", c = l(s.description), a = l(s.highlight), d = Array.isArray(s.labels) ? W(s.labels) : [], y = l(s.sku, s.repr_sku), u = l(s.image, s.repr_image), m = Rt(s, i, y), f = (Array.isArray(s.group_products) ? s.group_products : []).map((_) => {
750
- const h = b(_);
751
- return h ? j(h) : null;
752
- }).filter(C), p = u ?? l(...f.map((_) => _.imageUrl));
753
- if (i && f.length > 0) {
754
- const _ = {
755
- groupName: i,
756
- products: f
757
- };
758
- p && (_.image = p), c && (_.description = c), a && (_.highlight = a), d.length > 0 && (_.labels = d), r.push(_);
759
- }
760
- if (!m) continue;
761
- const g = {
762
- name: i,
763
- action: m
764
- };
765
- d.length > 0 && (g.labels = d), u !== void 0 && (g.image = u), n.push(g);
766
- }
767
- return r.length > 0 ? {
768
- type: "ui_spec",
769
- widget: "chat",
770
- spec: {
771
- root: "root",
772
- elements: { root: {
773
- type: "CategoriesContainer",
774
- props: { groups: r }
775
- } }
776
- },
777
- panelHint: "panel"
778
- } : n.length === 0 ? {
779
- type: "metadata",
780
- sessionId: "",
781
- model: "",
782
- meta: { aiProductGroupings: t.payload.product_groupings ?? [] }
783
- } : {
784
- type: "ui_spec",
785
- widget: "chat",
786
- spec: {
787
- root: "root",
788
- elements: { root: {
789
- type: "AIGroupingCards",
790
- props: { entries: n }
791
- } }
792
- }
793
- };
794
- }
795
- function Rt(t, e, r) {
796
- const n = b(t.requestDetails);
797
- if (n) {
798
- if (n.type !== "findSimilar") return k(n, e);
799
- const i = { ...b(n.payload) ?? {} };
800
- r && !E(i.sku) && (i.sku = r);
801
- const c = T(t.group_skus);
802
- return c.length > 0 && (i.group_skus = c), k({
803
- ...n,
804
- payload: I(i)
805
- }, e);
806
- }
807
- const o = {};
808
- r && (o.sku = r);
809
- const s = T(t.group_skus);
810
- return s.length > 0 && (o.group_skus = s), Object.keys(o).length === 0 ? null : k({
811
- type: "findSimilar",
812
- payload: I(o)
813
- }, e);
814
- }
815
- function Lt(t, e) {
816
- const r = b(t.requestDetails), n = b(r?.payload), o = l(t.detailed_user_message), s = t.group_skus && Array.isArray(t.group_skus) ? T(t.group_skus) : T(n?.group_skus), i = l(t.sku, t.representative_product_sku, n?.sku);
817
- if (r?.type === "findSimilar") {
818
- const a = { ...n ?? {} };
819
- return i && (a.sku = i), s.length > 0 && (a.group_skus = s), o && (a.input = o, E(a.text) || (a.text = o)), a.is_search_suggestion = 1, k({
820
- ...r,
821
- payload: I(a)
822
- }, e);
823
- }
824
- if (i) {
825
- const a = {
826
- sku: i,
827
- is_search_suggestion: 1
828
- };
829
- return s.length > 0 && (a.group_skus = s), o && (a.input = o, a.text = o), k({
830
- type: "findSimilar",
831
- payload: I(a)
832
- }, e);
833
- }
834
- const c = {
835
- ...n ?? {},
836
- is_suggested_text: 1,
837
- is_search_suggestion: 1
838
- };
839
- return o && !E(c.text) && (c.text = o), s.length > 0 && (c.group_skus = s), k(r?.type === "inputText" ? {
840
- ...r,
841
- payload: c
842
- } : {
843
- type: "inputText",
844
- payload: c
845
- }, e);
846
- }
847
- function Ot(t) {
848
- const e = t.payload.suggested_searches ?? [], r = [];
849
- for (let n = 0; n < e.length; n++) {
850
- const o = e[n];
851
- if (!o) continue;
852
- const s = l(o.short_name, o.chosen_attribute, o.detailed_user_message) ?? `Search ${n + 1}`, i = Lt(o, s);
853
- if (!i) continue;
854
- const c = {
855
- shortName: s,
856
- action: i
857
- }, a = l(o.detailed_user_message);
858
- a && a !== s && (c.detailedMessage = a);
859
- const d = st(o);
860
- d && d !== s && d !== (a ?? "") && (c.whyDifferent = d), typeof o.image == "string" && (c.image = o.image), r.push(c);
861
- }
862
- return r.length === 0 ? {
863
- type: "metadata",
864
- sessionId: "",
865
- model: "",
866
- meta: { aiSuggestedSearches: t.payload.suggested_searches ?? [] }
867
- } : {
868
- type: "ui_spec",
869
- widget: "chat",
870
- spec: {
871
- root: "root",
872
- elements: { root: {
873
- type: "AISuggestedSearchCards",
874
- props: { entries: r }
875
- } }
876
- }
877
- };
878
- }
879
- function vt(t) {
880
- const e = t.payload, r = k(e.requestDetails ?? e.request_details, l(e.review_count, e.reviewCount, e.text, e.title) ?? "Show product reviews");
881
- if (!r) return {
882
- type: "metadata",
883
- sessionId: "",
884
- model: "",
885
- meta: { groundingReview: e }
886
- };
887
- const n = { action: r };
888
- return e.title && (n.title = e.title), e.text && (n.text = e.text), e.review_count ? n.reviewCount = e.review_count : typeof e.reviewCount == "string" && e.reviewCount.trim() && (n.reviewCount = e.reviewCount), {
889
- type: "ui_spec",
890
- widget: "chat",
891
- spec: {
892
- root: "root",
893
- elements: { root: {
894
- type: "GroundingReviewCard",
895
- props: n
896
- } }
897
- }
898
- };
899
- }
900
- function qt() {
901
- return {
902
- type: "metadata",
903
- sessionId: "",
904
- model: "",
905
- meta: { analyzeAnimation: !0 }
906
- };
907
- }
908
- function jt(t) {
909
- return {
910
- type: "metadata",
911
- sessionId: "",
912
- model: "",
913
- meta: { voice: t.payload }
914
- };
915
- }
916
- function Bt(t) {
917
- const e = t.payload.group_list ?? [], r = [];
918
- for (const o of e) {
919
- const s = o.group_name ?? "", i = (o.product_list ?? []).map(w);
920
- r.push({
921
- groupName: s,
922
- products: i
923
- });
924
- }
925
- const n = [];
926
- for (const o of t.payload.filter_tags ?? []) {
927
- const s = o.title ?? "";
928
- if (!s) continue;
929
- const i = k(o.requestDetails, s), c = { title: s };
930
- i && (c.action = i), n.push(c);
931
- }
932
- return {
933
- type: "ui_spec",
934
- widget: "chat",
935
- spec: {
936
- root: "root",
937
- elements: { root: {
938
- type: "CategoriesContainer",
939
- props: {
940
- groups: r,
941
- filterTags: n
942
- }
943
- } }
944
- },
945
- panelHint: "panel"
946
- };
947
- }
948
- function Mt(t) {
949
- return {
950
- type: "metadata",
951
- sessionId: "",
952
- model: "",
953
- meta: {
954
- formType: t.type,
955
- formPayload: t.payload ?? {}
956
- }
957
- };
958
- }
959
- function Ut(t) {
960
- const e = {};
961
- return typeof t.payload?.summary == "string" && (e.summary = t.payload.summary), Array.isArray(t.payload?.products_discussed) && (e.products_discussed = t.payload.products_discussed), typeof t.payload?.user_sentiment == "string" && (e.user_sentiment = t.payload.user_sentiment), {
962
- type: "ui_spec",
963
- widget: "chat",
964
- spec: {
965
- root: "root",
966
- elements: { root: {
967
- type: "HandoffNotice",
968
- props: e
969
- } }
970
- }
971
- };
972
- }
973
- function zt(t) {
974
- return {
975
- type: "metadata",
976
- sessionId: "",
977
- model: "",
978
- meta: { launcherContent: t.payload ?? {} }
979
- };
980
- }
981
- function q(t, e) {
982
- const r = {
983
- product: t,
984
- index: e
985
- }, n = t, o = typeof n.sku == "string" ? n.sku : void 0;
986
- return o && (r.action = {
987
- title: typeof n.name == "string" ? n.name : "",
988
- type: "launchSingleProduct",
989
- payload: { sku: o }
990
- }), {
991
- type: "ProductCard",
992
- props: r
993
- };
994
- }
995
- function V(t, e) {
996
- const r = {}, n = [];
997
- for (let o = 0; o < t.length; o++) {
998
- const s = t[o];
999
- if (!s) continue;
1000
- const i = `action-${o}`;
1001
- n.push(i);
1002
- const c = {
1003
- label: s.label,
1004
- action: s.action
1005
- };
1006
- s.icon !== void 0 && (c.icon = s.icon), s.image !== void 0 && (c.image = s.image), s.description !== void 0 && (c.description = s.description), r[i] = {
1007
- type: "ActionButton",
1008
- props: c
1009
- };
1010
- }
1011
- return r.root = {
1012
- type: "ActionButtons",
1013
- props: { buttons: t.map((o) => {
1014
- const s = {
1015
- label: o.label,
1016
- action: o.action
1017
- };
1018
- return o.image !== void 0 && (s.image = o.image), o.description !== void 0 && (s.description = o.description), o.icon !== void 0 && (s.icon = o.icon), s;
1019
- }) },
1020
- children: n
1021
- }, {
1022
- type: "ui_spec",
1023
- widget: e,
1024
- spec: {
1025
- root: "root",
1026
- elements: r
1027
- }
1028
- };
1029
- }
1030
- function R(t, e) {
1031
- const r = {}, n = [];
1032
- for (let o = 0; o < t.length; o++) {
1033
- const s = t[o];
1034
- if (!s) continue;
1035
- const i = w(s), c = `product-${o}`;
1036
- n.push(c), r[c] = q(i, o);
1037
- }
1038
- return r.root = {
1039
- type: "ProductGrid",
1040
- props: { layout: "grid" },
1041
- children: n
1042
- }, {
1043
- type: "ui_spec",
1044
- widget: e,
1045
- spec: {
1046
- root: "root",
1047
- elements: r
1048
- }
1049
- };
1050
- }
1051
- function Ht(t, e, r) {
1052
- const n = /* @__PURE__ */ new Map();
1053
- for (const d of e)
1054
- d?.sku && n.set(d.sku, d);
1055
- if (n.size === 0) return null;
1056
- const o = /* @__PURE__ */ new Set(), s = {}, i = [], c = [];
1057
- let a = 0;
1058
- for (let d = 0; d < t.length; d++) {
1059
- const y = t[d], u = l(y?.title) ?? "", m = Array.isArray(y?.skus) ? y.skus : [], f = [];
1060
- for (let p = 0; p < m.length; p++) {
1061
- const g = typeof m[p] == "string" ? m[p].trim() : "";
1062
- if (!g || o.has(g)) continue;
1063
- const _ = n.get(g);
1064
- if (!_) continue;
1065
- o.add(g);
1066
- const h = `product-${d}-${p}`, Y = w(_);
1067
- i.push(h), f.push(h), s[h] = q(Y, a), a += 1;
1068
- }
1069
- f.length > 0 && c.push({
1070
- title: u,
1071
- children: f
1072
- });
1073
- }
1074
- return c.length === 0 || o.size < n.size ? null : (s.root = {
1075
- type: "ProductGrid",
1076
- props: {
1077
- layout: "grouped",
1078
- productSections: c
1079
- },
1080
- children: i
1081
- }, {
1082
- type: "ui_spec",
1083
- widget: r,
1084
- spec: {
1085
- root: "root",
1086
- elements: s
1087
- }
1088
- });
1089
- }
1090
- function Gt(t, e) {
1091
- const r = {}, n = [];
1092
- for (let o = 0; o < t.length; o++) {
1093
- const s = t[o];
1094
- if (!s) continue;
1095
- const i = `product-${o}`;
1096
- n.push(i), r[i] = q(s, o);
1097
- }
1098
- return r.root = {
1099
- type: "ProductGrid",
1100
- props: { layout: "grid" },
1101
- children: n
1102
- }, {
1103
- type: "ui_spec",
1104
- widget: e,
1105
- spec: {
1106
- root: "root",
1107
- elements: r
1108
- }
1109
- };
1110
- }
1111
- function $t(t, e) {
1112
- return {
1113
- type: "ui_spec",
1114
- widget: e,
1115
- spec: {
1116
- root: "root",
1117
- elements: { root: {
1118
- type: "ProductCard",
1119
- props: {
1120
- product: w(t),
1121
- index: 0
1122
- }
1123
- } }
1124
- }
1125
- };
1126
- }
1127
- function Ft(t) {
1128
- return {
1129
- type: "ui_spec",
1130
- widget: t,
1131
- spec: {
1132
- root: "root",
1133
- elements: { root: {
1134
- type: "ActionButtons",
1135
- props: { buttons: [] },
1136
- children: []
1137
- } }
1138
- }
1139
- };
1140
- }
1141
- function Kt(t) {
1142
- const e = l(t.sku), r = l(t.short_name), n = { ...b(t.product_item) ?? t }, o = l(t.discount_reason, t.discountReason);
1143
- return o && !l(n.discount_reason, n.discountReason) && (n.discount_reason = o), j(n, e, r);
1144
- }
1145
- function j(t, e, r) {
1146
- const n = l(t.sku, e), o = l(t.name, r);
1147
- if (!n || !o) return null;
1148
- const s = {
1149
- sku: n,
1150
- name: o
1151
- }, i = l(t.brand);
1152
- i && (s.brand = i);
1153
- const c = l(t.url);
1154
- c && (s.url = c);
1155
- const a = Vt(t.images);
1156
- if (a.length > 0) s.images = a;
1157
- else {
1158
- const g = l(t.image, t.image_url, t.imageUrl);
1159
- g && (s.images = [g]);
1160
- }
1161
- const d = x(t.price_discounted);
1162
- d !== void 0 && (s.price_discounted = d);
1163
- const y = x(t.price);
1164
- y !== void 0 && (s.price = y);
1165
- const u = x(t.rating);
1166
- u !== void 0 && (s.rating = u);
1167
- const m = x(t.review_count) ?? x(t.reviewCount);
1168
- m !== void 0 && (s.review_count = m);
1169
- const f = l(t.cart_code, t.cartCode);
1170
- f && (s.cart_code = f), typeof t.in_stock == "boolean" && (s.in_stock = t.in_stock), typeof t.inStock == "boolean" && (s.in_stock = t.inStock);
1171
- const p = l(t.discount_reason, t.discountReason);
1172
- return p && (s.discount_reason = p), w(s);
1173
- }
1174
- function k(t, e) {
1175
- const r = b(t);
1176
- if (!r) return null;
1177
- const n = r.type;
1178
- if (typeof n != "string" || n.length === 0) return null;
1179
- const o = {
1180
- title: e || n,
1181
- type: n
1182
- };
1183
- return r.payload !== void 0 && (o.payload = r.payload), o;
1184
- }
1185
- function b(t) {
1186
- return !t || typeof t != "object" || Array.isArray(t) ? null : t;
1187
- }
1188
- function l(...t) {
1189
- for (const e of t) if (typeof e == "string") {
1190
- const r = e.trim();
1191
- if (r.length > 0) return r;
1192
- }
1193
- }
1194
- function Vt(t) {
1195
- return Array.isArray(t) ? t.filter((e) => typeof e == "string" && e.length > 0) : [];
1196
- }
1197
- function W(t) {
1198
- if (typeof t == "string") {
1199
- const e = t.trim();
1200
- return e ? [e] : [];
1201
- }
1202
- return Array.isArray(t) ? t.filter((e) => typeof e == "string").map((e) => e.trim()).filter((e) => e.length > 0) : [];
1203
- }
1204
- function x(t) {
1205
- if (typeof t == "number" && Number.isFinite(t)) return t;
1206
- if (typeof t == "string") {
1207
- let e;
1208
- t.includes(".") && t.includes(",") ? e = t.replace(/\./g, "").replace(",", ".") : e = t.replace(",", ".");
1209
- const r = Number(e);
1210
- if (Number.isFinite(r)) return r;
1211
- }
1212
- }
1213
- function C(t) {
1214
- return t != null;
1215
- }
1216
- var Wt = /* @__PURE__ */ new Set([
1217
- "sku",
1218
- "name",
1219
- "brand",
1220
- "images",
1221
- "price",
1222
- "price_discounted",
1223
- "price_discount_rate",
1224
- "price_currency",
1225
- "discount_reason",
1226
- "url",
1227
- "rating",
1228
- "review_count",
1229
- "cart_code",
1230
- "in_stock",
1231
- "description",
1232
- "description_html",
1233
- "features",
1234
- "specifications",
1235
- "short_name",
1236
- "category_ids",
1237
- "category_names",
1238
- "variants",
1239
- "facet_hits",
1240
- "promotions"
1241
- ]);
1242
- function w(t) {
1243
- const e = t.price_discounted != null && t.price_discounted > 0, r = e ? t.price_discounted : t.price, n = e && t.price != null ? t.price : void 0;
1244
- let o;
1245
- n != null && r != null && n > 0 ? o = Math.round((n - r) / n * 100) : t.price_discount_rate != null && t.price_discount_rate > 0 && (o = t.price_discount_rate);
1246
- const s = l(t.brand), i = l(t.name) ?? t.sku, c = s && !i.toLowerCase().startsWith(s.toLowerCase()) ? `${s} ${i}` : i, a = {
1247
- sku: t.sku,
1248
- name: c,
1249
- url: l(t.url) ?? ""
1250
- }, d = t.images?.[0];
1251
- d && (a.imageUrl = d), t.images && t.images.length > 1 && (a.images = t.images), r != null && r > 0 && (a.price = String(r)), n != null && n > 0 && (a.originalPrice = String(n)), o !== void 0 && (a.discountPercent = o), s !== void 0 && (a.brand = s), t.rating !== void 0 && (a.rating = t.rating), t.review_count !== void 0 && (a.reviewCount = t.review_count), t.cart_code !== void 0 && (a.cartCode = t.cart_code), t.in_stock !== void 0 && (a.inStock = t.in_stock), t.variants && t.variants.length > 0 && (a.variants = t.variants), t.discount_reason !== void 0 && (a.discountReason = t.discount_reason), t.promotions && t.promotions.length > 0 && (a.promotions = t.promotions), t.description !== void 0 && (a.description = t.description), t.description_html !== void 0 && (a.descriptionHtml = t.description_html), t.features && t.features.length > 0 && (a.features = t.features), t.specifications !== void 0 && (a.specifications = t.specifications), t.facet_hits && (a.facetHits = t.facet_hits), t.category_names && t.category_names.length > 0 && (a.categoryNames = t.category_names), t.short_name !== void 0 && (a.shortName = t.short_name);
1252
- const y = t, u = {};
1253
- let m = !1;
1254
- for (const f of Object.keys(y)) Wt.has(f) || (u[f] = y[f], m = !0);
1255
- return m && (a.extras = u), a;
1256
- }
1257
- function Se(t) {
1258
- return t.results.map(w);
1259
- }
1260
- function Ae(t) {
1261
- return t.product_groupings.map((e) => {
1262
- const r = {
1263
- name: e.name,
1264
- products: (e.group_products ?? []).map(w)
1265
- };
1266
- return e.highlight !== void 0 && (r.highlight = e.highlight), r;
1267
- });
1268
- }
1269
- function Jt(t) {
1270
- const e = t.payload.type, { type: r, ...n } = t.payload;
1271
- return {
1272
- type: "ui_spec",
1273
- widget: "chat",
1274
- spec: {
1275
- root: "root",
1276
- elements: { root: {
1277
- type: e,
1278
- props: n
1279
- } }
1280
- }
1281
- };
1282
- }
1283
- var Qt = {
1284
- process_action: "/process_action",
1285
- launcher_action: "/launcher_action",
1286
- similar_products: "/similar_products",
1287
- product_groupings: "/product_groupings"
1288
- };
1289
- function Yt(t) {
1290
- if (t === void 0) throw new Error("[gengage] middlewareUrl is required. Pass your Gengage backend URL in widget config.");
1291
- const e = t.trim();
1292
- return e === "" ? "" : e.replace(/\/+$/, "");
1293
- }
1294
- function we(t, e) {
1295
- return `${Yt(e?.middlewareUrl)}/chat${Qt[t]}`;
1296
- }
1297
- var Xt = 7200 * 1e3, J = 200, Zt = 5 * 1024 * 1024, te = "gengage_request_response_cache", ee = 1, A = "responses", re = /* @__PURE__ */ new Set(["addToCart", "like"]), S = /* @__PURE__ */ new Map(), z = null;
1298
- async function xe(t, e = {}, r = {}) {
1299
- const n = ne(t, e);
1300
- if (!n) return fetch(t, e);
1301
- const o = Date.now(), s = await ae(n, o);
1302
- if (s) return de(s);
1303
- const i = await fetch(t, e);
1304
- return i.ok && ce(n, i.clone(), o, {
1305
- ttlMs: r.ttlMs ?? Xt,
1306
- maxEntries: r.maxEntries ?? J,
1307
- maxBodyBytes: r.maxBodyBytes ?? Zt
1308
- }), i;
1309
- }
1310
- function ne(t, e = {}) {
1311
- if (typeof Request < "u" && t instanceof Request) return null;
1312
- const r = String(e.method || "GET").toUpperCase();
1313
- if (r !== "POST" || typeof e.body != "string") return null;
1314
- const n = oe(e.body);
1315
- if (se(n)) return null;
1316
- const o = ie(n);
1317
- if (o) {
1318
- const s = P(n?.action);
1319
- return `rr:${$(G({
1320
- url: String(t),
1321
- method: r,
1322
- headers: H(e.headers),
1323
- type: n?.type ?? s?.type ?? "",
1324
- account_id: n?.account_id ?? n?.accountId ?? "",
1325
- locale: n?.locale ?? "",
1326
- outputLanguage: P(n?.meta)?.outputLanguage ?? "",
1327
- cache_key: o
1328
- }))}`;
1329
- }
1330
- return `rr:${$(G({
1331
- url: String(t),
1332
- method: r,
1333
- headers: H(e.headers),
1334
- body: e.body
1335
- }))}`;
1336
- }
1337
- function oe(t) {
1338
- try {
1339
- const e = JSON.parse(t);
1340
- return !e || typeof e != "object" || Array.isArray(e) ? null : e;
1341
- } catch {
1342
- return null;
1343
- }
1344
- }
1345
- function se(t) {
1346
- const e = t?.type;
1347
- return typeof e == "string" && re.has(e);
1348
- }
1349
- function ie(t) {
1350
- const e = (P(t?.payload) ?? P(P(t?.action)?.payload))?.cache_key;
1351
- return typeof e == "string" && e.trim().length > 0 ? e.trim() : null;
1352
- }
1353
- async function ae(t, e) {
1354
- const r = S.get(t);
1355
- if (r) if (r.expiresAt <= e) S.delete(t);
1356
- else
1357
- return r.lastAccessedAt = e, S.delete(t), S.set(t, r), L(r), r;
1358
- const n = await pe(t);
1359
- return n ? n.expiresAt <= e ? (S.delete(t), fe(t), null) : (n.lastAccessedAt = e, S.set(t, n), L(n), Q(J), n) : null;
1360
- }
1361
- async function ce(t, e, r, n) {
1362
- try {
1363
- const o = await e.text();
1364
- if (ge(o) > n.maxBodyBytes || ue(o)) return;
1365
- const s = {
1366
- key: t,
1367
- status: e.status,
1368
- statusText: e.statusText,
1369
- headers: le(e.headers),
1370
- body: o,
1371
- createdAt: r,
1372
- expiresAt: r + n.ttlMs,
1373
- lastAccessedAt: r
1374
- };
1375
- S.set(t, s), Q(n.maxEntries), await L(s), await ye(n.maxEntries, r);
1376
- } catch {
1377
- }
1378
- }
1379
- function ue(t) {
1380
- return /"_error"\s*:/u.test(t) || /"type"\s*:\s*"error"/u.test(t);
1381
- }
1382
- function de(t) {
1383
- return new Response(t.body, {
1384
- status: t.status,
1385
- statusText: t.statusText,
1386
- headers: new Headers(t.headers)
1387
- });
1388
- }
1389
- function H(t) {
1390
- const e = new Headers(t), r = [];
1391
- return e.forEach((n, o) => {
1392
- const s = o.toLowerCase();
1393
- s !== "authorization" && s !== "cookie" && r.push([s, n]);
1394
- }), r.sort(([n], [o]) => n.localeCompare(o));
1395
- }
1396
- function le(t) {
1397
- const e = [];
1398
- return t.forEach((r, n) => {
1399
- const o = n.toLowerCase();
1400
- o !== "set-cookie" && e.push([o, r]);
1401
- }), e.sort(([r], [n]) => r.localeCompare(n));
1402
- }
1403
- function Q(t) {
1404
- for (; S.size > t; ) {
1405
- const e = [...S.values()].sort((r, n) => r.lastAccessedAt - n.lastAccessedAt)[0];
1406
- if (!e) break;
1407
- S.delete(e.key);
1408
- }
1409
- }
1410
- async function D() {
1411
- return typeof indexedDB > "u" ? null : (z ??= new Promise((t) => {
1412
- const e = indexedDB.open(te, ee);
1413
- e.onupgradeneeded = () => {
1414
- const r = e.result;
1415
- r.objectStoreNames.contains(A) || r.createObjectStore(A, { keyPath: "key" }).createIndex("lastAccessedAt", "lastAccessedAt", { unique: !1 });
1416
- }, e.onsuccess = () => t(e.result), e.onerror = () => t(null), e.onblocked = () => t(null);
1417
- }), z);
1418
- }
1419
- async function pe(t) {
1420
- const e = await D();
1421
- return e ? new Promise((r) => {
1422
- const n = e.transaction(A, "readonly"), o = n.objectStore(A).get(t);
1423
- o.onsuccess = () => r(o.result ?? null), o.onerror = () => r(null), n.onerror = () => r(null);
1424
- }) : null;
1425
- }
1426
- async function L(t) {
1427
- const e = await D();
1428
- e && await new Promise((r) => {
1429
- const n = e.transaction(A, "readwrite");
1430
- n.objectStore(A).put(t), n.oncomplete = () => r(), n.onerror = () => r(), n.onabort = () => r();
1431
- });
1432
- }
1433
- async function fe(t) {
1434
- const e = await D();
1435
- e && await new Promise((r) => {
1436
- const n = e.transaction(A, "readwrite");
1437
- n.objectStore(A).delete(t), n.oncomplete = () => r(), n.onerror = () => r(), n.onabort = () => r();
1438
- });
1439
- }
1440
- async function ye(t, e) {
1441
- const r = await D();
1442
- r && await new Promise((n) => {
1443
- const o = r.transaction(A, "readwrite"), s = o.objectStore(A), i = [], c = s.openCursor();
1444
- c.onsuccess = () => {
1445
- const a = c.result;
1446
- if (!a) {
1447
- const d = i.filter((u) => u.expiresAt <= e), y = i.filter((u) => u.expiresAt > e).sort((u, m) => u.lastAccessedAt - m.lastAccessedAt).slice(0, Math.max(0, i.length - d.length - t));
1448
- for (const u of [...d, ...y]) s.delete(u.key);
1449
- return;
1450
- }
1451
- i.push(a.value), a.continue();
1452
- }, c.onerror = () => n(), o.oncomplete = () => n(), o.onerror = () => n(), o.onabort = () => n();
1453
- });
1454
- }
1455
- function G(t) {
1456
- return JSON.stringify(O(t));
1457
- }
1458
- function P(t) {
1459
- return t && typeof t == "object" && !Array.isArray(t) ? t : null;
1460
- }
1461
- function O(t) {
1462
- return Array.isArray(t) ? t.map(O) : !t || typeof t != "object" ? t : Object.fromEntries(Object.entries(t).sort(([e], [r]) => e.localeCompare(r)).map(([e, r]) => [e, O(r)]));
1463
- }
1464
- function $(t) {
1465
- let e = 14695981039346656037n;
1466
- const r = 1099511628211n;
1467
- for (let n = 0; n < t.length; n += 1)
1468
- e ^= BigInt(t.charCodeAt(n)), e = BigInt.asUintN(64, e * r);
1469
- return e.toString(16).padStart(16, "0");
1470
- }
1471
- function ge(t) {
1472
- return typeof TextEncoder < "u" ? new TextEncoder().encode(t).byteLength : t.length;
1473
- }
1474
- export {
1475
- Ae as a,
1476
- I as c,
1477
- st as d,
1478
- et as f,
1479
- _e as g,
1480
- Z as h,
1481
- ke as i,
1482
- it as l,
1483
- he as m,
1484
- we as n,
1485
- Se as o,
1486
- be as p,
1487
- Yt as r,
1488
- w as s,
1489
- xe as t,
1490
- ot as u
1491
- };