@gengage/assistant-fe 0.3.36 → 0.3.37

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,694 +0,0 @@
1
- import { S as F, a as S, b as U, c as C, d as x, h as z, m as $, n as W, o as E, p as M, t as G, u as J, w as Q } from "./schemas-ZYhTx11k.js";
2
- import { $ as Y, C as V, F as w, H as Z, Q as K, U as X, Z as P, b as ee, c as te, h as A, p as ne, t as re, tt as L, y as ie, z as ae } from "./ga-datalayer-BUM5z5Pq.js";
3
- import { a as se, n as oe, r as ce, t as R } from "./price-formatter-CR6VT5z4.js";
4
- import { n as de } from "./connection-warning-nm_yulO-.js";
5
- function O(e) {
6
- const t = [];
7
- for (const n of Object.values(e)) if (n.type === "ProductCard" && n.props) {
8
- const r = n.props.product ?? n.props;
9
- typeof r.sku == "string" && typeof r.name == "string" && t.push(r);
10
- }
11
- return t;
12
- }
13
- function B(e) {
14
- const t = e.headers.get("Content-Type") ?? "";
15
- return t.includes("application/x-ndjson") || t.includes("text/event-stream");
16
- }
17
- async function le(e, t) {
18
- const n = [], r = { onEvent: (a) => {
19
- const s = M(a);
20
- !s || s.type !== "ui_spec" || n.push(...O(s.spec.elements));
21
- } };
22
- return t !== void 0 && (r.signal = t), await U(e, r), n;
23
- }
24
- async function ge(e, t, n) {
25
- const r = x("similar_products", t), a = {
26
- method: "POST",
27
- headers: { "Content-Type": "application/json" },
28
- body: JSON.stringify(e)
29
- };
30
- n !== void 0 && (a.signal = n);
31
- const s = await fetch(r, a);
32
- if (!s.ok) throw new Error(`HTTP ${s.status}: ${s.statusText}`);
33
- if (B(s)) return le(s, n);
34
- const o = await s.text();
35
- if (!o) throw new Error("Empty response body from similar_products endpoint");
36
- try {
37
- return z(JSON.parse(o));
38
- } catch {
39
- throw new Error("Invalid JSON from similar_products endpoint");
40
- }
41
- }
42
- async function ue(e, t) {
43
- const n = [];
44
- let r = null;
45
- const a = { onEvent: (s) => {
46
- const o = M(s);
47
- if (o) {
48
- if (o.type === "metadata" && o.meta) {
49
- const d = o.meta.group_name;
50
- if (typeof d == "string") {
51
- r = {
52
- name: d,
53
- products: []
54
- };
55
- const l = o.meta.highlight;
56
- typeof l == "string" && (r.highlight = l), n.push(r);
57
- }
58
- }
59
- o.type === "ui_spec" && r && r.products.push(...O(o.spec.elements));
60
- }
61
- } };
62
- return t !== void 0 && (a.signal = t), await U(e, a), n;
63
- }
64
- async function pe(e, t, n) {
65
- const r = x("product_groupings", t), a = {
66
- method: "POST",
67
- headers: { "Content-Type": "application/json" },
68
- body: JSON.stringify(e)
69
- };
70
- n !== void 0 && (a.signal = n);
71
- const s = await fetch(r, a);
72
- if (!s.ok) throw new Error(`HTTP ${s.status}: ${s.statusText}`);
73
- if (B(s)) return ue(s, n);
74
- const o = await s.text();
75
- if (!o) return [];
76
- try {
77
- return $(JSON.parse(o));
78
- } catch {
79
- throw new Error("Invalid JSON from product_groupings endpoint");
80
- }
81
- }
82
- function D(e) {
83
- const { product: t, index: n, discountType: r, onClick: a, onAddToCart: s, renderCard: o } = e, d = e.i18n, l = e.pricing;
84
- if (e.renderCardElement) {
85
- const i = e.renderCardElement(t, n);
86
- if (i) return i;
87
- }
88
- if (o) {
89
- const i = document.createElement("div");
90
- return i.className = "gengage-simrel-card gengage-simrel-card--custom gds-card gds-product-card gds-card-interactive", i.dataset.gengagePart = "simrel-product-card", i.innerHTML = Z(o(t, n)), i.addEventListener("click", (u) => {
91
- u.target.closest(".gengage-simrel-atc") || u.target.closest(".gengage-chat-product-card-atc") || a(t);
92
- }), i;
93
- }
94
- const c = document.createElement("article");
95
- c.className = "gengage-simrel-card gengage-chat-product-card gds-card gds-product-card gds-card-interactive", c.dataset.gengagePart = "simrel-product-card", t.inStock === !1 && c.classList.add("gengage-simrel-card--out-of-stock"), c.setAttribute("role", "listitem"), c.dataset.sku = t.sku;
96
- const g = document.createElement("div");
97
- if (g.className = "gengage-simrel-card-image gengage-chat-product-card-img-wrapper", g.dataset.gengagePart = "simrel-product-card-image", t.imageUrl && ae(t.imageUrl)) {
98
- const i = document.createElement("img");
99
- i.className = "gengage-chat-product-card-img", i.src = t.imageUrl, i.alt = t.name, i.loading = "lazy", oe(i), g.appendChild(i);
100
- }
101
- if (r === "badge" && t.discountPercent && t.discountPercent > 0) {
102
- const i = document.createElement("span");
103
- i.className = "gengage-simrel-badge gengage-chat-product-card-discount-badge", i.textContent = `%${ce(t.discountPercent)}`, g.appendChild(i);
104
- }
105
- c.appendChild(g);
106
- const m = document.createElement("div");
107
- if (m.className = "gengage-simrel-card-info gengage-chat-product-card-body", m.dataset.gengagePart = "simrel-product-card-body", t.brand) {
108
- const i = document.createElement("div");
109
- i.className = "gengage-simrel-card-brand gengage-chat-product-card-brand", i.textContent = t.brand, m.appendChild(i);
110
- }
111
- const f = document.createElement("div");
112
- if (f.className = "gengage-simrel-card-name gengage-chat-product-card-name", f.textContent = t.name, f.title = t.name, m.appendChild(f), t.rating != null && t.rating > 0) {
113
- const i = document.createElement("div");
114
- if (i.className = "gengage-simrel-card-rating gengage-chat-product-card-rating", i.appendChild(se(t.rating)), t.reviewCount != null) {
115
- const u = document.createElement("span");
116
- u.className = "gengage-simrel-card-review-count gengage-chat-product-card-review-count", u.textContent = ` (${t.reviewCount})`, i.appendChild(u);
117
- }
118
- m.appendChild(i);
119
- }
120
- const b = document.createElement("div");
121
- if (b.className = "gengage-simrel-card-price gengage-chat-product-card-price", t.originalPrice && t.originalPrice !== t.price && (r === "strike-through" || !r)) {
122
- const i = document.createElement("span");
123
- i.className = "gengage-simrel-card-price-original gengage-chat-product-card-original-price", i.textContent = R(t.originalPrice, l), b.appendChild(i);
124
- }
125
- if (t.price && parseFloat(t.price) > 0) {
126
- const i = document.createElement("span");
127
- i.className = "gengage-simrel-card-price-current gengage-chat-product-card-price-current", i.textContent = R(t.price, l), b.appendChild(i);
128
- }
129
- m.appendChild(b), c.appendChild(m);
130
- const h = document.createElement("button");
131
- if (h.className = "gengage-simrel-card-cta gengage-chat-product-card-cta gds-btn gds-btn-secondary", h.type = "button", h.dataset.gengagePart = "simrel-product-card-cta", h.textContent = d?.ctaLabel ?? "View", h.addEventListener("click", (i) => {
132
- i.preventDefault(), i.stopPropagation(), a(t);
133
- }), c.appendChild(h), t.inStock === !1) {
134
- const i = document.createElement("div");
135
- i.className = "gengage-simrel-card-oos", i.textContent = d?.outOfStockLabel ?? "Out of Stock", c.appendChild(i);
136
- } else if (t.cartCode) {
137
- const i = document.createElement("button");
138
- i.className = "gengage-simrel-atc gengage-simrel-atc-button gds-btn gds-btn-secondary", i.type = "button", i.textContent = d?.addToCartButton ?? "Add to Cart", i.addEventListener("click", (u) => {
139
- u.preventDefault(), u.stopPropagation(), s({
140
- sku: t.sku,
141
- quantity: 1,
142
- cartCode: t.cartCode
143
- });
144
- }), c.appendChild(i);
145
- }
146
- return c.addEventListener("click", (i) => {
147
- i.target.closest(".gengage-simrel-atc") || i.target.closest(".gengage-chat-product-card-atc") || i.target.closest(".gengage-chat-product-card-cta") || a(t);
148
- }), c;
149
- }
150
- function N(e) {
151
- const t = document.createElement("div");
152
- t.className = "gengage-simrel-grid", t.setAttribute("role", "list"), t.setAttribute("aria-label", e.i18n?.similarProductsAriaLabel ?? "Similar products"), e.columns && t.style.setProperty("--gengage-simrel-columns", String(e.columns));
153
- for (let n = 0; n < e.products.length; n++) {
154
- const r = {
155
- product: e.products[n],
156
- index: n,
157
- onClick: e.onClick,
158
- onAddToCart: e.onAddToCart
159
- };
160
- e.i18n !== void 0 && (r.i18n = e.i18n), e.discountType !== void 0 && (r.discountType = e.discountType), e.renderCard !== void 0 && (r.renderCard = e.renderCard), e.renderCardElement !== void 0 && (r.renderCardElement = e.renderCardElement);
161
- const a = D(r);
162
- t.appendChild(a);
163
- }
164
- return e.products.length === 0 && (t.style.display = "none", t.dataset.empty = "true"), t;
165
- }
166
- var me = 0;
167
- function fe(e) {
168
- const t = me++, n = document.createElement("div");
169
- if (n.className = "gengage-simrel-groups", n.dataset.gengagePart = "simrel-groups", e.groups.length === 0)
170
- return n.style.display = "none", n.dataset.empty = "true", n;
171
- const r = document.createElement("div");
172
- r.className = "gengage-simrel-tabs gds-toolbar", r.dataset.gengagePart = "simrel-tab-bar", r.setAttribute("role", "tablist");
173
- const a = [], s = [], o = (i) => {
174
- const u = {
175
- products: i.products,
176
- onClick: e.onClick,
177
- onAddToCart: e.onAddToCart
178
- };
179
- return e.i18n !== void 0 && (u.i18n = e.i18n), e.discountType !== void 0 && (u.discountType = e.discountType), e.renderCard !== void 0 && (u.renderCard = e.renderCard), e.renderCardElement !== void 0 && (u.renderCardElement = e.renderCardElement), e.columns !== void 0 && (u.columns = e.columns), u;
180
- }, d = (i) => {
181
- for (let p = 0; p < a.length; p++) {
182
- const y = p === i;
183
- a[p].classList.toggle("gengage-simrel-tab--active", y), a[p].setAttribute("aria-selected", String(y)), a[p].tabIndex = y ? 0 : -1;
184
- }
185
- const u = e.groups[i], v = s[i];
186
- v.innerHTML = "";
187
- const T = N(o(u));
188
- v.appendChild(T);
189
- for (let p = 0; p < s.length; p++) {
190
- const y = p === i;
191
- s[p].style.display = y ? "" : "none", s[p].tabIndex = y ? 0 : -1;
192
- }
193
- };
194
- for (let i = 0; i < e.groups.length; i++) {
195
- const u = e.groups[i], v = `gengage-simrel-tab-${t}-${i}`, T = `gengage-simrel-panel-${t}-${i}`, p = document.createElement("button");
196
- p.className = "gengage-simrel-tab gds-tab", p.type = "button", p.dataset.gengagePart = "simrel-tab", p.id = v, p.textContent = u.name, p.setAttribute("role", "tab"), p.setAttribute("aria-controls", T), p.setAttribute("aria-selected", String(i === 0)), p.tabIndex = i === 0 ? 0 : -1, i === 0 && p.classList.add("gengage-simrel-tab--active"), p.addEventListener("click", () => d(i)), p.addEventListener("keydown", (k) => {
197
- let _ = -1;
198
- k.key === "ArrowRight" || k.key === "ArrowDown" ? _ = (i + 1) % e.groups.length : k.key === "ArrowLeft" || k.key === "ArrowUp" ? _ = (i - 1 + e.groups.length) % e.groups.length : k.key === "Home" ? _ = 0 : k.key === "End" && (_ = e.groups.length - 1), _ >= 0 && (k.preventDefault(), d(_), a[_].focus());
199
- }), a.push(p), r.appendChild(p);
200
- const y = document.createElement("div");
201
- y.className = "gengage-simrel-tab-panel", y.dataset.gengagePart = "simrel-tab-panel", y.id = T, y.setAttribute("role", "tabpanel"), y.setAttribute("aria-labelledby", v), y.tabIndex = i === 0 ? 0 : -1, i !== 0 && (y.style.display = "none"), s.push(y);
202
- }
203
- const l = document.createElement("button");
204
- l.type = "button", l.className = "gengage-simrel-tabs-arrow gengage-simrel-tabs-arrow--left", l.setAttribute("aria-label", e.i18n?.scrollTabsLeft ?? "Scroll tabs left"), l.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="15 18 9 12 15 6"/></svg>';
205
- const c = document.createElement("button");
206
- c.type = "button", c.className = "gengage-simrel-tabs-arrow gengage-simrel-tabs-arrow--right", c.setAttribute("aria-label", e.i18n?.scrollTabsRight ?? "Scroll tabs right"), c.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 18 15 12 9 6"/></svg>';
207
- const g = 200;
208
- l.addEventListener("click", () => {
209
- r.scrollBy({
210
- left: -g,
211
- behavior: "smooth"
212
- });
213
- }), c.addEventListener("click", () => {
214
- r.scrollBy({
215
- left: g,
216
- behavior: "smooth"
217
- });
218
- });
219
- const m = () => {
220
- const i = r.scrollLeft <= 4, u = r.scrollLeft + r.clientWidth >= r.scrollWidth - 4;
221
- l.style.display = i ? "none" : "", c.style.display = u ? "none" : "", r.classList.toggle("gengage-simrel-tabs--peek-right", !u), r.classList.toggle("gengage-simrel-tabs--peek-left", !i);
222
- };
223
- r.addEventListener("scroll", m, { passive: !0 }), typeof ResizeObserver < "u" && new ResizeObserver(m).observe(r), l.style.display = "none", c.style.display = "none", requestAnimationFrame(m);
224
- const f = document.createElement("div");
225
- f.className = "gengage-simrel-tabs-wrapper", f.appendChild(l), f.appendChild(r), f.appendChild(c), n.appendChild(f);
226
- const b = s[0], h = N(o(e.groups[0]));
227
- b.appendChild(h);
228
- for (const i of s) n.appendChild(i);
229
- return n;
230
- }
231
- function I(e) {
232
- if (!e || typeof e != "object") return null;
233
- const t = e;
234
- if (typeof t.sku != "string" || typeof t.name != "string" || typeof t.url != "string") return null;
235
- const n = {
236
- sku: t.sku,
237
- name: t.name,
238
- url: t.url
239
- }, r = t.imageUrl;
240
- typeof r == "string" && (n.imageUrl = r);
241
- const a = t.price;
242
- typeof a == "string" && (n.price = a);
243
- const s = t.originalPrice;
244
- typeof s == "string" && (n.originalPrice = s);
245
- const o = t.discountPercent;
246
- typeof o == "number" && (n.discountPercent = o);
247
- const d = t.brand;
248
- typeof d == "string" && (n.brand = d);
249
- const l = t.rating;
250
- typeof l == "number" && (n.rating = l);
251
- const c = t.reviewCount;
252
- typeof c == "number" && (n.reviewCount = c);
253
- const g = t.cartCode;
254
- typeof g == "string" && (n.cartCode = g);
255
- const m = t.inStock;
256
- typeof m == "boolean" && (n.inStock = m);
257
- const f = t.extras;
258
- return f != null && typeof f == "object" && (n.extras = f), n;
259
- }
260
- function he(e) {
261
- if (!e || typeof e != "object") return null;
262
- const t = e, n = t.title, r = t.type;
263
- if (typeof n != "string" || typeof r != "string") return null;
264
- const a = {
265
- title: n,
266
- type: r
267
- };
268
- return t.payload !== void 0 && (a.payload = t.payload), a;
269
- }
270
- var j = {
271
- ProductGrid: ({ element: e, renderElement: t, context: n }) => {
272
- const r = document.createElement("div");
273
- r.className = "gengage-simrel-grid", r.setAttribute("role", "list");
274
- const a = e.props?.columns;
275
- let s;
276
- typeof a == "number" && Number.isFinite(a) && a > 0 ? s = Math.floor(a) : typeof n.gridColumns == "number" && Number.isFinite(n.gridColumns) && n.gridColumns > 0 && (s = Math.floor(n.gridColumns)), s !== void 0 && r.style.setProperty("--gengage-simrel-columns", String(s));
277
- for (const o of e.children ?? []) {
278
- const d = t(o);
279
- d && r.appendChild(d);
280
- }
281
- if (r.children.length === 0) {
282
- const o = document.createElement("div");
283
- o.className = "gengage-simrel-empty", o.textContent = n.i18n.emptyStateMessage, r.appendChild(o);
284
- }
285
- return r;
286
- },
287
- ProductCard: ({ element: e, context: t }) => {
288
- const n = I(e.props?.product ?? e.props);
289
- if (!n) return null;
290
- const r = e.props?.index, a = typeof r == "number" && Number.isFinite(r) ? r : 0, s = e.props?.discountType, o = s === "strike-through" || s === "badge" ? s : t.discountType, d = {
291
- product: n,
292
- index: a,
293
- onClick: t.onClick,
294
- onAddToCart: t.onAddToCart,
295
- i18n: t.i18n
296
- };
297
- return o !== void 0 && (d.discountType = o), t.renderCard !== void 0 && (d.renderCard = t.renderCard), t.renderCardElement !== void 0 && (d.renderCardElement = t.renderCardElement), t.pricing !== void 0 && (d.pricing = t.pricing), D(d);
298
- },
299
- GroupTabs: ({ element: e, context: t }) => {
300
- const n = e.props?.groups;
301
- if (!Array.isArray(n)) return null;
302
- const r = [];
303
- for (const o of n) {
304
- if (!o || typeof o != "object") continue;
305
- const d = o;
306
- if (typeof d.name != "string") continue;
307
- const l = [];
308
- if (Array.isArray(d.products)) for (const g of d.products) {
309
- const m = I(g);
310
- m && l.push(m);
311
- }
312
- const c = {
313
- name: d.name,
314
- products: l
315
- };
316
- typeof d.highlight == "string" && (c.highlight = d.highlight), r.push(c);
317
- }
318
- const a = {
319
- groups: r,
320
- onClick: t.onClick,
321
- onAddToCart: t.onAddToCart,
322
- i18n: t.i18n
323
- }, s = e.props?.columns;
324
- return typeof s == "number" && Number.isFinite(s) && s > 0 ? a.columns = Math.floor(s) : typeof t.gridColumns == "number" && Number.isFinite(t.gridColumns) && t.gridColumns > 0 && (a.columns = Math.floor(t.gridColumns)), t.discountType !== void 0 && (a.discountType = t.discountType), t.renderCard !== void 0 && (a.renderCard = t.renderCard), t.renderCardElement !== void 0 && (a.renderCardElement = t.renderCardElement), fe(a);
325
- },
326
- EmptyState: ({ element: e, context: t }) => {
327
- const n = document.createElement("div");
328
- n.className = "gengage-simrel-empty";
329
- const r = e.props?.message;
330
- return n.textContent = typeof r == "string" ? r : t.i18n.emptyStateMessage, n;
331
- },
332
- AddToCartButton: ({ element: e, context: t }) => {
333
- const n = e.props?.sku, r = e.props?.cartCode;
334
- if (typeof n != "string" || typeof r != "string") return null;
335
- const a = document.createElement("button");
336
- a.className = "gengage-simrel-atc gengage-chat-product-card-cta", a.type = "button";
337
- const s = e.props?.label;
338
- return a.textContent = typeof s == "string" ? s : t.i18n.addToCartButton, a.addEventListener("click", (o) => {
339
- o.preventDefault(), o.stopPropagation(), t.onAddToCart({
340
- sku: n,
341
- quantity: 1,
342
- cartCode: r
343
- });
344
- }), a;
345
- },
346
- QuickActions: ({ element: e, context: t }) => {
347
- const n = document.createElement("div");
348
- n.className = "gengage-simrel-quick-actions";
349
- const r = e.props?.actions;
350
- if (!Array.isArray(r) || !t.onAction) return n;
351
- for (const a of r) {
352
- if (!a || typeof a != "object") continue;
353
- const s = a, o = s.label, d = he(s.action);
354
- if (typeof o != "string" || !d) continue;
355
- const l = document.createElement("button");
356
- l.className = "gengage-simrel-quick-action", l.type = "button", l.textContent = o, l.addEventListener("click", (c) => {
357
- c.preventDefault(), c.stopPropagation(), t.onAction?.(d);
358
- }), n.appendChild(l);
359
- }
360
- return n;
361
- }
362
- }, H = ({ element: e, renderElement: t }) => {
363
- if (!e.children || e.children.length === 0) return null;
364
- const n = document.createElement("div");
365
- for (const r of e.children) {
366
- const a = t(r);
367
- a && n.appendChild(a);
368
- }
369
- return n;
370
- };
371
- function ye() {
372
- return { ...j };
373
- }
374
- function Ce(e, t, n = j, r = H) {
375
- return Q({
376
- spec: e,
377
- context: t,
378
- registry: n,
379
- containerClassName: "gengage-simrel-uispec",
380
- unknownRenderer: r
381
- });
382
- }
383
- var q = {
384
- similarProductsAriaLabel: "Benzer ürünler",
385
- emptyStateMessage: "Benzer ürün bulunamadı.",
386
- addToCartButton: "Sepete Ekle",
387
- ctaLabel: "İncele",
388
- outOfStockLabel: "Stokta Yok",
389
- decreaseLabel: "Azalt",
390
- increaseLabel: "Artır",
391
- errorLoadingMessage: "Benzer ürünler yüklenemedi.",
392
- retryButtonText: "Tekrar dene",
393
- priceSuffix: " TL",
394
- scrollTabsLeft: "Sola kaydır",
395
- scrollTabsRight: "Sağa kaydır"
396
- }, be = {
397
- similarProductsAriaLabel: "Similar products",
398
- emptyStateMessage: "No similar products found.",
399
- addToCartButton: "Add to cart",
400
- ctaLabel: "View",
401
- outOfStockLabel: "Out of Stock",
402
- decreaseLabel: "Decrease",
403
- increaseLabel: "Increase",
404
- errorLoadingMessage: "Could not load similar products.",
405
- retryButtonText: "Try again",
406
- priceSuffix: "",
407
- scrollTabsLeft: "Scroll tabs left",
408
- scrollTabsRight: "Scroll tabs right"
409
- };
410
- function ve(e) {
411
- return e ? e.toLowerCase().split("-")[0] ?? "tr" : "tr";
412
- }
413
- function Ee(e) {
414
- return ve(e) === "en" ? be : q;
415
- }
416
- var ke = E({
417
- sku: C(),
418
- name: C(),
419
- imageUrl: C().url().optional(),
420
- price: C().optional(),
421
- originalPrice: C().optional(),
422
- discountPercent: S().optional(),
423
- url: C().url(),
424
- brand: C().optional(),
425
- rating: S().min(0).max(5).optional(),
426
- reviewCount: S().int().nonnegative().optional()
427
- }), _e = E({
428
- layout: G(["grid", "carousel"]).optional(),
429
- columns: S().int().positive().optional()
430
- }), Se = E({
431
- product: ke,
432
- index: S().int().nonnegative(),
433
- discountType: G(["strike-through", "badge"]).optional()
434
- }), Te = E({
435
- sku: C(),
436
- label: C().optional(),
437
- cartCode: C()
438
- }), we = E({ actions: W(E({
439
- label: C(),
440
- action: E({
441
- title: C(),
442
- type: C(),
443
- payload: J().optional()
444
- })
445
- })) }), Pe = E({ message: C().optional() }), Ue = { components: {
446
- ProductGrid: {
447
- schema: _e,
448
- description: "Outer grid or carousel container for similar products."
449
- },
450
- ProductCard: {
451
- schema: Se,
452
- description: "A single product card with image, title, price, and actions."
453
- },
454
- AddToCartButton: {
455
- schema: Te,
456
- description: "Add-to-cart CTA rendered inside or below a product card."
457
- },
458
- QuickActions: {
459
- schema: we,
460
- description: "A row of quick-action buttons below product info."
461
- },
462
- EmptyState: {
463
- schema: Pe,
464
- description: "Empty state shown when no similar products are available."
465
- }
466
- } }, Ae = class extends ee {
467
- constructor(...e) {
468
- super(...e), this._abortController = null, this._contentEl = null, this._lastResultCount = -1, this._i18n = q;
469
- }
470
- async onInit(e) {
471
- this._i18n = this._resolveI18n(e), this._contentEl = document.createElement("div"), this._contentEl.className = "gengage-simrel-container", this._contentEl.dataset.gengagePart = "simrel-container", this._contentEl.lang = ie(e.locale);
472
- const t = this._clampGridColumns(e.gridColumns);
473
- t !== void 0 && this._contentEl.style.setProperty("--gengage-simrel-columns", String(t)), this.root.appendChild(this._contentEl), this._lastSku = e.sku, await this._fetchAndRender(e.sku), te("simrel");
474
- }
475
- onUpdate(e) {
476
- const t = e.sku;
477
- !t || t === this._lastSku && this._lastResultCount !== 0 || (this._lastSku = t, this._fetchAndRender(t));
478
- }
479
- onShow() {
480
- this._contentEl && (this._contentEl.style.opacity = "0", this._contentEl.style.transition = "opacity 0.3s ease-in", requestAnimationFrame(() => {
481
- this._contentEl && (this._contentEl.style.opacity = "1");
482
- }));
483
- }
484
- onHide() {
485
- }
486
- onDestroy() {
487
- this._abort(), this._contentEl && (this._contentEl.remove(), this._contentEl = null);
488
- }
489
- _handleProductClick(e) {
490
- const t = {
491
- sku: e.sku,
492
- name: e.name,
493
- url: e.url
494
- };
495
- if (e.imageUrl !== void 0 && (t.imageUrl = e.imageUrl), e.price !== void 0 && (t.price = e.price), e.originalPrice !== void 0 && (t.originalPrice = e.originalPrice), e.discountPercent !== void 0 && (t.discountPercent = e.discountPercent), e.brand !== void 0 && (t.brand = e.brand), e.rating !== void 0 && (t.rating = e.rating), e.reviewCount !== void 0 && (t.reviewCount = e.reviewCount), e.cartCode !== void 0 && (t.cartCode = e.cartCode), e.inStock !== void 0 && (t.inStock = e.inStock), this.config.onProductClick?.(t) === !1) return;
496
- ne(e.sku, e.name);
497
- const n = this.config.session?.sessionId ?? null;
498
- w("gengage:similar:product-click", {
499
- sku: e.sku,
500
- url: e.url,
501
- sessionId: n
502
- }), this.config.onProductNavigate?.(e.url, e.sku, n);
503
- }
504
- _handleAddToCart(e) {
505
- re(e.sku, e.quantity), this.config.onAddToCart?.(e), w("gengage:similar:add-to-cart", e), this.track(X(this.analyticsContext(), {
506
- attribution_source: "simrel",
507
- attribution_action_id: crypto.randomUUID(),
508
- cart_value: 0,
509
- currency: this.config.pricing?.currencyCode ?? "TRY",
510
- line_items: e.quantity,
511
- sku: e.sku
512
- }));
513
- }
514
- _abort() {
515
- this._abortController?.abort(), this._abortController = null;
516
- }
517
- _isSuperseded(e) {
518
- return this._abortController?.signal !== e;
519
- }
520
- async _fetchAndRender(e) {
521
- this._abort(), this._abortController = new AbortController();
522
- const t = this._abortController.signal, n = setTimeout(() => this._abortController?.abort(), 1e4);
523
- if (t.addEventListener("abort", () => clearTimeout(n)), !this._contentEl) return;
524
- this._contentEl.innerHTML = "", this._contentEl.style.display = "";
525
- const r = document.createElement("div");
526
- r.className = "gengage-simrel-loading", r.dataset.gengagePart = "simrel-loading";
527
- const a = document.createElement("div");
528
- a.className = "gengage-simrel-spinner", a.dataset.gengagePart = "simrel-loading-spinner", r.appendChild(a), this._contentEl.appendChild(r);
529
- const s = { middlewareUrl: this.config.middlewareUrl }, o = crypto.randomUUID(), d = Date.now(), l = de({
530
- source: "simrel",
531
- locale: this.config.locale
532
- });
533
- this.track(Y(this.analyticsContext(), {
534
- endpoint: "similar_products",
535
- request_id: o,
536
- widget: "simrel"
537
- }));
538
- try {
539
- const c = {
540
- account_id: this.config.accountId,
541
- session_id: this.config.session?.sessionId ?? "",
542
- correlation_id: this.config.session?.sessionId ?? "",
543
- sku: e
544
- };
545
- this.config.domain !== void 0 && (c.domain = this.config.domain);
546
- const g = await ge(c, s, t);
547
- if (this._lastResultCount = g.length, !this._contentEl) return;
548
- if (this._contentEl.innerHTML = "", g.length > 0) try {
549
- const m = g.map((h) => h.sku), f = new Map(g.map((h) => [h.sku, h])), b = (await pe({
550
- account_id: this.config.accountId,
551
- session_id: this.config.session?.sessionId ?? "",
552
- correlation_id: this.config.session?.sessionId ?? "",
553
- skus: m
554
- }, s, t)).map((h) => ({
555
- ...h,
556
- products: h.products.map((i) => ({
557
- ...f.get(i.sku),
558
- ...i
559
- })).filter((i) => typeof i.sku == "string" && typeof i.name == "string" && typeof i.url == "string")
560
- })).filter((h) => h.products.length > 0);
561
- if (b.length > 0 && this._contentEl) {
562
- const h = this._buildGroupsSpec(b), i = this._renderUISpec(h);
563
- this._contentEl.appendChild(i), A("simrel"), this.track(P(this.analyticsContext(), {
564
- request_id: o,
565
- latency_ms: Date.now() - d,
566
- chunk_count: b.reduce((u, v) => u + v.products.length, 0),
567
- widget: "simrel"
568
- })), this.track(L(this.analyticsContext(), {
569
- message_count: b.reduce((u, v) => u + v.products.length, 0),
570
- history_ref: o,
571
- redaction_level: "none",
572
- widget: "simrel"
573
- }));
574
- return;
575
- }
576
- } catch {
577
- }
578
- if (this._isSuperseded(t)) return;
579
- if (this._contentEl) {
580
- const m = this._buildProductsSpec(g), f = this._renderUISpec(m);
581
- this._contentEl.appendChild(f);
582
- }
583
- g.length > 0 && A("simrel"), this.track(P(this.analyticsContext(), {
584
- request_id: o,
585
- latency_ms: Date.now() - d,
586
- chunk_count: g.length,
587
- widget: "simrel"
588
- })), this.track(L(this.analyticsContext(), {
589
- message_count: g.length,
590
- history_ref: o,
591
- redaction_level: "none",
592
- widget: "simrel"
593
- }));
594
- } catch (c) {
595
- if (c instanceof DOMException && c.name === "AbortError" && this._isSuperseded(t)) return;
596
- if (w("gengage:global:error", {
597
- source: "simrel",
598
- code: "FETCH_ERROR",
599
- message: V(this.config.locale, c)
600
- }), this.track(K(this.analyticsContext(), {
601
- request_id: o,
602
- error_code: "FETCH_ERROR",
603
- error_message: c instanceof Error ? c.message : String(c),
604
- widget: "simrel"
605
- })), this._contentEl) {
606
- this._contentEl.innerHTML = "";
607
- const g = document.createElement("div");
608
- g.className = "gengage-simrel-error";
609
- const m = document.createElement("span");
610
- m.textContent = this._i18n.errorLoadingMessage, g.appendChild(m);
611
- const f = document.createElement("button");
612
- f.className = "gengage-simrel-retry", f.textContent = this._i18n.retryButtonText, f.addEventListener("click", () => {
613
- this._fetchAndRender(this.config.sku);
614
- }), g.appendChild(f), this._contentEl.appendChild(g);
615
- }
616
- } finally {
617
- l();
618
- }
619
- }
620
- _clampGridColumns(e) {
621
- if (!(e === void 0 || typeof e != "number" || !Number.isFinite(e)))
622
- return Math.max(1, Math.min(12, Math.floor(e)));
623
- }
624
- _resolveI18n(e) {
625
- return {
626
- ...Ee(e.locale),
627
- ...e.i18n
628
- };
629
- }
630
- _resolveUISpecRegistry() {
631
- return F(ye(), this.config.renderer?.registry);
632
- }
633
- _buildRenderContext() {
634
- const e = this.config.renderCard, t = {
635
- onClick: (r) => this._handleProductClick(r),
636
- onAddToCart: (r) => this._handleAddToCart(r),
637
- i18n: this._i18n
638
- };
639
- this.config.discountType !== void 0 && (t.discountType = this.config.discountType), e !== void 0 && (t.renderCard = e), this.config.renderCardElement !== void 0 && (t.renderCardElement = this.config.renderCardElement), this.config.pricing !== void 0 && (t.pricing = this.config.pricing);
640
- const n = this._clampGridColumns(this.config.gridColumns);
641
- return n !== void 0 && (t.gridColumns = n), t;
642
- }
643
- _renderUISpec(e) {
644
- const t = this._resolveUISpecRegistry(), n = this._buildRenderContext(), r = this.config.renderer?.unknownRenderer ?? H, a = (o, d) => Ce(o, d, t, r), s = this.config.renderer?.renderUISpec;
645
- return s ? s(e, n, {
646
- registry: t,
647
- unknownRenderer: r,
648
- defaultRender: a
649
- }) : a(e, n);
650
- }
651
- _buildProductsSpec(e) {
652
- const t = {}, n = [];
653
- for (let s = 0; s < e.length; s++) {
654
- const o = e[s], d = `product-${s}`;
655
- n.push(d), t[d] = {
656
- type: "ProductCard",
657
- props: {
658
- product: o,
659
- index: s,
660
- discountType: this.config.discountType
661
- }
662
- };
663
- }
664
- const r = { layout: "grid" }, a = this._clampGridColumns(this.config.gridColumns);
665
- return a !== void 0 && (r.columns = a), t.root = {
666
- type: "ProductGrid",
667
- props: r,
668
- children: n
669
- }, {
670
- root: "root",
671
- elements: t
672
- };
673
- }
674
- _buildGroupsSpec(e) {
675
- return {
676
- root: "root",
677
- elements: { root: {
678
- type: "GroupTabs",
679
- props: { groups: e }
680
- } }
681
- };
682
- }
683
- };
684
- function xe() {
685
- return new Ae();
686
- }
687
- export {
688
- H as a,
689
- ye as i,
690
- xe as n,
691
- Ce as o,
692
- Ue as r,
693
- Ae as t
694
- };