@oxvo/ai-live-assist 7.5.19 → 7.5.21

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 (99) hide show
  1. package/cjs/AiLiveAssist.d.ts +8 -1
  2. package/cjs/AiLiveAssist.js +113 -8
  3. package/cjs/client.d.ts +5 -3
  4. package/cjs/client.js +44 -5
  5. package/cjs/commerce/cartContracts.d.ts +158 -0
  6. package/cjs/commerce/cartContracts.js +228 -0
  7. package/cjs/commerce/cartSession.d.ts +86 -0
  8. package/cjs/commerce/cartSession.js +475 -0
  9. package/cjs/commerce/contracts.d.ts +152 -0
  10. package/cjs/commerce/contracts.js +322 -0
  11. package/cjs/commerce/pageContext.d.ts +50 -0
  12. package/cjs/commerce/pageContext.js +173 -0
  13. package/cjs/commerce/productPanel.d.ts +65 -0
  14. package/cjs/commerce/productPanel.js +662 -0
  15. package/cjs/commerce/realtimeCommerce.d.ts +73 -0
  16. package/cjs/commerce/realtimeCommerce.js +342 -0
  17. package/cjs/commerce/shopifyCart.d.ts +130 -0
  18. package/cjs/commerce/shopifyCart.js +1135 -0
  19. package/cjs/commerce/shopifyCartPresentation.d.ts +12 -0
  20. package/cjs/commerce/shopifyCartPresentation.js +379 -0
  21. package/cjs/commerce/shoppingNotes.d.ts +61 -0
  22. package/cjs/commerce/shoppingNotes.js +313 -0
  23. package/cjs/commerce/shoppingNotesView.d.ts +46 -0
  24. package/cjs/commerce/shoppingNotesView.js +269 -0
  25. package/cjs/control.d.ts +9 -1
  26. package/cjs/control.js +51 -1
  27. package/cjs/guideAdmission.d.ts +5 -2
  28. package/cjs/guideAdmission.js +21 -2
  29. package/cjs/guideInstaller.js +1 -1
  30. package/cjs/guideMeasurement.d.ts +5 -0
  31. package/cjs/guideMeasurement.js +29 -0
  32. package/cjs/guideSubjectStore.js +2 -0
  33. package/cjs/guideTypes.d.ts +18 -0
  34. package/cjs/liveControlChannel.d.ts +43 -1
  35. package/cjs/liveControlChannel.js +166 -14
  36. package/cjs/liveGuideAdmission.d.ts +3 -2
  37. package/cjs/liveGuideAdmission.js +10 -2
  38. package/cjs/liveGuideWidget.d.ts +18 -0
  39. package/cjs/liveGuideWidget.js +228 -23
  40. package/cjs/locales/resources.js +2240 -0
  41. package/cjs/messages.js +56 -0
  42. package/cjs/tabLock.d.ts +2 -0
  43. package/cjs/tabLock.js +11 -1
  44. package/cjs/types.d.ts +6 -2
  45. package/cjs/types.js +56 -0
  46. package/cjs/ui.d.ts +37 -0
  47. package/cjs/ui.js +261 -9
  48. package/cjs/version.d.ts +1 -1
  49. package/cjs/version.js +1 -1
  50. package/lib/AiLiveAssist.d.ts +8 -1
  51. package/lib/AiLiveAssist.js +113 -8
  52. package/lib/client.d.ts +5 -3
  53. package/lib/client.js +44 -5
  54. package/lib/commerce/cartContracts.d.ts +158 -0
  55. package/lib/commerce/cartContracts.js +221 -0
  56. package/lib/commerce/cartSession.d.ts +86 -0
  57. package/lib/commerce/cartSession.js +471 -0
  58. package/lib/commerce/contracts.d.ts +152 -0
  59. package/lib/commerce/contracts.js +300 -0
  60. package/lib/commerce/pageContext.d.ts +50 -0
  61. package/lib/commerce/pageContext.js +168 -0
  62. package/lib/commerce/productPanel.d.ts +65 -0
  63. package/lib/commerce/productPanel.js +657 -0
  64. package/lib/commerce/realtimeCommerce.d.ts +73 -0
  65. package/lib/commerce/realtimeCommerce.js +337 -0
  66. package/lib/commerce/shopifyCart.d.ts +130 -0
  67. package/lib/commerce/shopifyCart.js +1130 -0
  68. package/lib/commerce/shopifyCartPresentation.d.ts +12 -0
  69. package/lib/commerce/shopifyCartPresentation.js +376 -0
  70. package/lib/commerce/shoppingNotes.d.ts +61 -0
  71. package/lib/commerce/shoppingNotes.js +307 -0
  72. package/lib/commerce/shoppingNotesView.d.ts +46 -0
  73. package/lib/commerce/shoppingNotesView.js +265 -0
  74. package/lib/control.d.ts +9 -1
  75. package/lib/control.js +51 -1
  76. package/lib/guideAdmission.d.ts +5 -2
  77. package/lib/guideAdmission.js +21 -2
  78. package/lib/guideInstaller.js +1 -1
  79. package/lib/guideMeasurement.d.ts +5 -0
  80. package/lib/guideMeasurement.js +24 -0
  81. package/lib/guideSubjectStore.js +2 -0
  82. package/lib/guideTypes.d.ts +18 -0
  83. package/lib/liveControlChannel.d.ts +43 -1
  84. package/lib/liveControlChannel.js +166 -14
  85. package/lib/liveGuideAdmission.d.ts +3 -2
  86. package/lib/liveGuideAdmission.js +10 -2
  87. package/lib/liveGuideWidget.d.ts +18 -0
  88. package/lib/liveGuideWidget.js +228 -23
  89. package/lib/locales/resources.js +2240 -0
  90. package/lib/messages.js +56 -0
  91. package/lib/tabLock.d.ts +2 -0
  92. package/lib/tabLock.js +11 -1
  93. package/lib/types.d.ts +6 -2
  94. package/lib/types.js +56 -0
  95. package/lib/ui.d.ts +37 -0
  96. package/lib/ui.js +261 -9
  97. package/lib/version.d.ts +1 -1
  98. package/lib/version.js +1 -1
  99. package/package.json +1 -1
@@ -0,0 +1,662 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CommerceProductPanel = exports.formatCommercePrice = exports.COMMERCE_PRODUCT_PANEL_STYLES = void 0;
4
+ exports.COMMERCE_PRODUCT_PANEL_STYLES = `
5
+ .commerce-panel { box-sizing: border-box; min-width: 0; max-width: 100%; padding: 12px; color: var(--assist-text, var(--voice-text)); background: var(--assist-surface, var(--voice-surface)); border: 1px solid var(--assist-border, var(--voice-border)); border-radius: 16px; font: 400 14px/1.45 system-ui, sans-serif; pointer-events: auto; }
6
+ .commerce-panel *, .commerce-panel *::before, .commerce-panel *::after { box-sizing: border-box; }
7
+ .commerce-panel[hidden], .commerce-panel [hidden] { display: none !important; }
8
+ .commerce-panel-header, .commerce-panel-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
9
+ .commerce-panel-header { justify-content: space-between; }
10
+ .commerce-panel h2, .commerce-panel h3, .commerce-panel p { margin: 0; }
11
+ .commerce-panel h2 { flex: 1; font: 600 15px/1.4 system-ui, sans-serif; }
12
+ .commerce-panel h3 { font: 600 14px/1.4 system-ui, sans-serif; overflow-wrap: anywhere; }
13
+ .commerce-panel-status, .commerce-panel-ranking { margin-block: 8px !important; color: var(--assist-muted, var(--voice-muted)); font-size: 12px; }
14
+ .commerce-panel-content { max-height: min(540px, 55dvh); overflow: auto; overscroll-behavior: contain; scrollbar-gutter: stable; padding: 2px; }
15
+ .commerce-panel-products { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 210px), 1fr)); align-items: start; gap: 12px; }
16
+ .commerce-panel[data-comparing="true"] .commerce-panel-products { grid-template-columns: repeat(auto-fit, minmax(min(100%, 180px), 1fr)); }
17
+ .commerce-product { display: grid; min-width: 0; gap: 10px; padding: 12px; border: 1px solid var(--assist-border, var(--voice-border)); border-radius: 12px; background: var(--assist-surface-muted, var(--voice-surface)); }
18
+ .commerce-product-summary { display: grid; gap: 4px; }
19
+ .commerce-product-image { display: block; width: 100%; height: 112px; object-fit: contain; border-radius: 8px; }
20
+ .commerce-product-price { font-weight: 600; }
21
+ .commerce-product-availability, .commerce-product-description, .commerce-product-note { font-size: 12px; overflow-wrap: anywhere; }
22
+ .commerce-product-description { white-space: pre-line; }
23
+ .commerce-product-note { color: var(--assist-muted, var(--voice-muted)); }
24
+ .commerce-product-options { display: grid; min-width: 0; gap: 8px; border: 0; padding: 0; margin: 0; }
25
+ .commerce-product-label { display: grid; min-width: 0; gap: 4px; font-size: 12px; }
26
+ .commerce-product-compare { display: flex; align-items: center; gap: 8px; min-height: 44px; cursor: pointer; font-size: 12px; }
27
+ .commerce-panel button, .commerce-panel select, .commerce-panel input[type="number"], .commerce-product-link { min-height: 44px; max-width: 100%; padding: 8px 10px; border: 1px solid var(--assist-border, var(--voice-border)); border-radius: 8px; color: inherit; background: var(--assist-surface, var(--voice-surface)); font: inherit; }
28
+ .commerce-panel select, .commerce-panel input[type="number"] { width: 100%; min-width: 0; }
29
+ .commerce-panel button { cursor: pointer; white-space: normal; overflow-wrap: anywhere; }
30
+ .commerce-panel button:disabled, .commerce-panel select:disabled, .commerce-panel input:disabled { opacity: .55; cursor: default; }
31
+ .commerce-panel :is(a, button, input, select):focus-visible { outline: 2px solid currentColor; outline-offset: 2px; }
32
+ .commerce-product-link { display: flex; align-items: center; justify-content: center; text-align: center; text-decoration: underline; text-underline-offset: 3px; }
33
+ .commerce-panel-actions { margin-block-start: 10px; }
34
+ .commerce-panel-actions > button { flex: 1; }
35
+ .commerce-panel-close { flex: none; font-size: 12px !important; }
36
+ @media (forced-colors: active) { .commerce-panel, .commerce-product { color: CanvasText; background: Canvas; border-color: CanvasText; } .commerce-product-link { color: LinkText; } .commerce-panel :is(a, button, input, select):focus-visible { outline-color: Highlight; } }
37
+ `;
38
+ const validQuantityRule = (rule) => Number.isSafeInteger(rule.minimum) &&
39
+ rule.minimum > 0 &&
40
+ Number.isSafeInteger(rule.increment) &&
41
+ rule.increment > 0 &&
42
+ rule.minimum % rule.increment === 0 &&
43
+ (rule.maximum === null ||
44
+ (Number.isSafeInteger(rule.maximum) &&
45
+ rule.maximum >= rule.minimum &&
46
+ rule.maximum % rule.increment === 0));
47
+ const rankingMessages = {
48
+ shopify_best_selling: "commerceBestSelling",
49
+ relevance: "commerceRelevant",
50
+ price_ascending: "commercePriceOrder",
51
+ catalog: "commerceCatalogOrder",
52
+ };
53
+ const parentElement = (element) => {
54
+ if (element.parentElement)
55
+ return element.parentElement;
56
+ const root = element.getRootNode();
57
+ return root instanceof ShadowRoot ? root.host : null;
58
+ };
59
+ /** Includes clipping and hidden shadow hosts: attachment alone is not display. */
60
+ const visiblyRendered = (element) => {
61
+ if (!element.isConnected || document.visibilityState === "hidden")
62
+ return false;
63
+ const rect = element.getBoundingClientRect();
64
+ if (rect.width <= 0 || rect.height <= 0 || !element.getClientRects().length)
65
+ return false;
66
+ let left = Math.max(0, rect.left);
67
+ let top = Math.max(0, rect.top);
68
+ let right = Math.min(window.innerWidth, rect.right);
69
+ let bottom = Math.min(window.innerHeight, rect.bottom);
70
+ for (let current = element; current; current = parentElement(current)) {
71
+ const style = window.getComputedStyle(current);
72
+ if (current.hasAttribute("hidden") ||
73
+ current.getAttribute("aria-hidden") === "true" ||
74
+ style.display === "none" ||
75
+ style.visibility === "hidden" ||
76
+ style.visibility === "collapse" ||
77
+ style.opacity === "0")
78
+ return false;
79
+ if (current !== element) {
80
+ const bounds = current.getBoundingClientRect();
81
+ if (/(hidden|clip|auto|scroll)/u.test(style.overflowX || style.overflow)) {
82
+ left = Math.max(left, bounds.left);
83
+ right = Math.min(right, bounds.right);
84
+ }
85
+ if (/(hidden|clip|auto|scroll)/u.test(style.overflowY || style.overflow)) {
86
+ top = Math.max(top, bounds.top);
87
+ bottom = Math.min(bottom, bounds.bottom);
88
+ }
89
+ }
90
+ }
91
+ // A summary must be fully in the visible area before claiming it was shown.
92
+ // Tall product descriptions and controls are deliberately outside this summary.
93
+ return right - left >= rect.width - 1 && bottom - top >= rect.height - 1;
94
+ };
95
+ const safeUrl = (value, origin) => {
96
+ try {
97
+ const url = new URL(value);
98
+ return url.protocol === "https:" &&
99
+ !url.username &&
100
+ !url.password &&
101
+ (!origin || url.origin === origin)
102
+ ? url.href
103
+ : null;
104
+ }
105
+ catch {
106
+ return null;
107
+ }
108
+ };
109
+ // Keep the catalog's decimal exact, including large values and sub-cent prices.
110
+ // Formatting a Number first can round a valid decimal into a different price.
111
+ const formatCommercePrice = (amount, currency, locale) => {
112
+ const [integer = "0", decimal = ""] = amount.split(".");
113
+ const currencyDigits = new Intl.NumberFormat(locale, {
114
+ style: "currency",
115
+ currency,
116
+ }).resolvedOptions().minimumFractionDigits ?? 0;
117
+ const fraction = decimal.replace(/0+$/u, "").padEnd(currencyDigits, "0");
118
+ const formatter = new Intl.NumberFormat(locale, {
119
+ style: "currency",
120
+ currency,
121
+ minimumFractionDigits: fraction.length,
122
+ maximumFractionDigits: fraction.length,
123
+ });
124
+ const digits = new Intl.NumberFormat(locale, { useGrouping: false });
125
+ return formatter
126
+ .formatToParts(BigInt(integer))
127
+ .map((part) => part.type === "fraction"
128
+ ? [...fraction].map((digit) => digits.format(Number(digit))).join("")
129
+ : part.value)
130
+ .join("");
131
+ };
132
+ exports.formatCommercePrice = formatCommercePrice;
133
+ class CommerceProductPanel {
134
+ constructor(message, callbacks) {
135
+ this.message = message;
136
+ this.callbacks = callbacks;
137
+ this.element = document.createElement("section");
138
+ this.content = document.createElement("div");
139
+ this.products = document.createElement("div");
140
+ this.status = document.createElement("p");
141
+ this.ranking = document.createElement("p");
142
+ this.catalog = null;
143
+ this.states = [];
144
+ this.comparison = null;
145
+ this.seen = new Set();
146
+ this.displayed = false;
147
+ this.reported = new Set();
148
+ this.busy = false;
149
+ this.failed = false;
150
+ this.errorMessage = "commerceError";
151
+ this.destroyed = false;
152
+ this.expiryTimer = null;
153
+ this.visibilityFrame = null;
154
+ this.observer = null;
155
+ this.addHandler = null;
156
+ this.canAdd = null;
157
+ /** Call after opening/moving the widget; observers also cover clipping/scroll. */
158
+ this.refreshVisibility = () => {
159
+ if (this.destroyed ||
160
+ this.displayed ||
161
+ !this.catalog ||
162
+ this.visibilityFrame !== null)
163
+ return;
164
+ this.visibilityFrame = window.requestAnimationFrame(() => {
165
+ this.visibilityFrame = null;
166
+ const catalog = this.catalog;
167
+ if (!catalog || !this.fresh() || this.busy || this.displayed)
168
+ return;
169
+ for (const state of this.states) {
170
+ if (!state.dirty && visiblyRendered(state.summary))
171
+ this.seen.add(state.product.productRef);
172
+ }
173
+ if (this.callbacks.displayMode !== "visible" &&
174
+ this.states.some((state) => !this.seen.has(state.product.productRef)))
175
+ return;
176
+ if (this.states.length && !this.seen.size)
177
+ return;
178
+ if (!this.states.length && !visiblyRendered(this.status))
179
+ return;
180
+ const newlySeen = this.states.filter((state) => this.seen.has(state.product.productRef) &&
181
+ !this.reported.has(state.product.productRef));
182
+ if (this.states.length && !newlySeen.length)
183
+ return;
184
+ for (const state of newlySeen)
185
+ this.reported.add(state.product.productRef);
186
+ this.displayed = this.reported.size === this.states.length;
187
+ this.callbacks.onDisplayed?.(catalog.resultSetId, this.states
188
+ .filter((state) => this.reported.has(state.product.productRef))
189
+ .map((state) => ({
190
+ productRef: state.product.productRef,
191
+ variantRef: state.product.variant?.variantRef ?? null,
192
+ })));
193
+ });
194
+ };
195
+ this.element.className = "commerce-panel";
196
+ this.element.hidden = true;
197
+ this.element.setAttribute("aria-label", message("commerceTitle"));
198
+ const header = document.createElement("div");
199
+ header.className = "commerce-panel-header";
200
+ const title = document.createElement("h2");
201
+ title.textContent = message("commerceTitle");
202
+ const close = this.button("commerceClose", () => {
203
+ if (this.destroyed)
204
+ return;
205
+ this.clear();
206
+ callbacks.onClose?.();
207
+ });
208
+ close.className = "commerce-panel-close";
209
+ header.append(title);
210
+ if (callbacks.onShoppingNotes)
211
+ header.append(this.button("shoppingNotesTitle", () => {
212
+ if (!this.destroyed)
213
+ callbacks.onShoppingNotes?.();
214
+ }));
215
+ header.append(close);
216
+ this.status.className = "commerce-panel-status";
217
+ this.status.setAttribute("role", "status");
218
+ this.status.setAttribute("aria-live", "polite");
219
+ this.status.setAttribute("aria-atomic", "true");
220
+ this.ranking.className = "commerce-panel-ranking";
221
+ this.content.className = "commerce-panel-content";
222
+ this.products.className = "commerce-panel-products";
223
+ this.content.append(this.products);
224
+ this.compareButton = this.button("commerceCompare", () => this.compare());
225
+ this.backButton = this.button("commerceBack", () => {
226
+ this.comparison = null;
227
+ this.update();
228
+ this.refreshVisibility();
229
+ this.compareButton.focus({ preventScroll: true });
230
+ });
231
+ this.refreshButton = this.button("commerceRefresh", () => {
232
+ if (this.busy || this.destroyed)
233
+ return;
234
+ this.setBusy(true);
235
+ this.runAction(() => callbacks.onRefresh());
236
+ });
237
+ const actions = document.createElement("div");
238
+ actions.className = "commerce-panel-actions";
239
+ actions.append(this.compareButton, this.backButton, this.refreshButton);
240
+ this.element.append(header, this.ranking, this.status, this.content, actions);
241
+ if (typeof IntersectionObserver !== "undefined") {
242
+ this.observer = new IntersectionObserver(() => this.refreshVisibility(), {
243
+ threshold: [0, 1],
244
+ });
245
+ }
246
+ document.addEventListener("scroll", this.refreshVisibility, true);
247
+ document.addEventListener("visibilitychange", this.refreshVisibility);
248
+ window.addEventListener("resize", this.refreshVisibility);
249
+ }
250
+ /** Input must already pass parseCommerceCatalog for the current binding/page. */
251
+ setCatalog(catalog) {
252
+ if (this.destroyed)
253
+ return;
254
+ this.clear();
255
+ if (catalog.products.length > 3) {
256
+ this.showError();
257
+ return;
258
+ }
259
+ // Isolate click handlers and receipts from subsequent caller object changes.
260
+ this.catalog = JSON.parse(JSON.stringify(catalog));
261
+ this.element.hidden = false;
262
+ this.ranking.textContent = this.message(rankingMessages[catalog.ranking.basis]);
263
+ this.states = this.catalog.products.map((product) => this.renderProduct(product));
264
+ this.products.replaceChildren(...this.states.map((state) => state.element));
265
+ for (const state of this.states)
266
+ this.observer?.observe(state.summary);
267
+ if (!this.states.length)
268
+ this.observer?.observe(this.status);
269
+ this.update();
270
+ const remaining = Date.parse(catalog.expiresAt) - Date.now();
271
+ if (remaining > 0 && Number.isFinite(remaining)) {
272
+ this.expiryTimer = setTimeout(() => this.update(), Math.min(remaining, 300000));
273
+ }
274
+ this.refreshVisibility();
275
+ }
276
+ /** The parent supplies real operation authority; a catalog never enables cart writes. */
277
+ setCartActionHandler(handler, canAdd) {
278
+ this.addHandler = handler;
279
+ this.canAdd = handler && canAdd ? canAdd : null;
280
+ this.update();
281
+ }
282
+ setBusy(busy) {
283
+ if (this.destroyed)
284
+ return;
285
+ this.busy = busy;
286
+ if (busy)
287
+ this.element.hidden = false;
288
+ this.update();
289
+ if (!busy)
290
+ this.refreshVisibility();
291
+ }
292
+ showError(message = "commerceError") {
293
+ if (this.destroyed)
294
+ return;
295
+ this.busy = false;
296
+ this.failed = true;
297
+ this.errorMessage = message;
298
+ this.element.hidden = false;
299
+ this.update();
300
+ }
301
+ clear() {
302
+ if (this.expiryTimer !== null)
303
+ clearTimeout(this.expiryTimer);
304
+ if (this.visibilityFrame !== null)
305
+ window.cancelAnimationFrame(this.visibilityFrame);
306
+ this.expiryTimer = null;
307
+ this.visibilityFrame = null;
308
+ this.observer?.disconnect();
309
+ this.catalog = null;
310
+ this.states = [];
311
+ this.comparison = null;
312
+ this.seen.clear();
313
+ this.displayed = false;
314
+ this.reported.clear();
315
+ this.busy = false;
316
+ this.failed = false;
317
+ this.errorMessage = "commerceError";
318
+ this.products.replaceChildren();
319
+ this.status.textContent = "";
320
+ this.ranking.textContent = "";
321
+ this.element.hidden = true;
322
+ this.element.removeAttribute("aria-busy");
323
+ this.element.removeAttribute("data-comparing");
324
+ }
325
+ destroy() {
326
+ this.clear();
327
+ this.destroyed = true;
328
+ this.addHandler = null;
329
+ this.canAdd = null;
330
+ document.removeEventListener("scroll", this.refreshVisibility, true);
331
+ document.removeEventListener("visibilitychange", this.refreshVisibility);
332
+ window.removeEventListener("resize", this.refreshVisibility);
333
+ this.element.remove();
334
+ }
335
+ button(key, action) {
336
+ const button = document.createElement("button");
337
+ button.type = "button";
338
+ button.textContent = this.message(key);
339
+ button.addEventListener("click", action);
340
+ return button;
341
+ }
342
+ renderProduct(product) {
343
+ const card = document.createElement("article");
344
+ card.className = "commerce-product";
345
+ card.dataset.productRef = product.productRef;
346
+ const imageUrl = product.image && safeUrl(product.image.url);
347
+ let image = null;
348
+ if (imageUrl) {
349
+ image = document.createElement("img");
350
+ image.className = "commerce-product-image";
351
+ image.src = imageUrl;
352
+ image.alt = product.image.alt;
353
+ image.loading = "lazy";
354
+ image.decoding = "async";
355
+ image.referrerPolicy = "no-referrer";
356
+ image.addEventListener("error", () => {
357
+ image.hidden = true;
358
+ image.dataset.failed = "true";
359
+ });
360
+ card.append(image);
361
+ }
362
+ const summary = document.createElement("div");
363
+ summary.className = "commerce-product-summary";
364
+ const title = document.createElement("h3");
365
+ title.textContent = product.title;
366
+ const price = document.createElement("p");
367
+ price.className = "commerce-product-price";
368
+ const availability = document.createElement("p");
369
+ availability.className = "commerce-product-availability";
370
+ summary.append(title, price, availability);
371
+ card.append(summary);
372
+ if (product.description) {
373
+ const description = document.createElement("p");
374
+ description.className = "commerce-product-description";
375
+ description.textContent = product.description;
376
+ card.append(description);
377
+ }
378
+ const compareLabel = document.createElement("label");
379
+ compareLabel.className = "commerce-product-compare";
380
+ const compare = document.createElement("input");
381
+ compare.type = "checkbox";
382
+ const compareText = document.createElement("span");
383
+ compareText.textContent = this.message("commerceCompareSelect")
384
+ .split("{product}")
385
+ .join(product.title);
386
+ compareLabel.append(compare, compareText);
387
+ compare.addEventListener("change", () => this.update());
388
+ card.append(compareLabel);
389
+ const state = {
390
+ product,
391
+ element: card,
392
+ image,
393
+ planNote: null,
394
+ summary,
395
+ price,
396
+ availability,
397
+ inputs: [],
398
+ select: null,
399
+ compare,
400
+ quantity: null,
401
+ add: null,
402
+ save: null,
403
+ dirty: false,
404
+ };
405
+ if (product.options.length) {
406
+ const options = document.createElement("fieldset");
407
+ options.className = "commerce-product-options";
408
+ const legend = document.createElement("legend");
409
+ legend.textContent = this.message("commerceOptions");
410
+ options.append(legend);
411
+ for (const option of product.options) {
412
+ const label = document.createElement("label");
413
+ label.className = "commerce-product-label";
414
+ const text = document.createElement("span");
415
+ text.textContent = option.name;
416
+ const input = document.createElement("select");
417
+ input.name = option.name;
418
+ const placeholder = document.createElement("option");
419
+ placeholder.value = "";
420
+ placeholder.textContent = this.message("commerceChoose")
421
+ .split("{option}")
422
+ .join(option.name);
423
+ input.append(placeholder);
424
+ for (const value of option.values) {
425
+ const entry = document.createElement("option");
426
+ entry.value = value;
427
+ entry.textContent = value;
428
+ input.append(entry);
429
+ }
430
+ input.value =
431
+ product.variant?.selectedOptions.find((choice) => choice.name === option.name)?.value ?? "";
432
+ input.addEventListener("change", () => {
433
+ state.dirty =
434
+ !product.variant ||
435
+ state.inputs.some((select) => product.variant.selectedOptions.find((choice) => choice.name === select.name)?.value !== select.value);
436
+ // The previously rendered variant cannot attest to a new option selection.
437
+ if (state.dirty)
438
+ this.seen.delete(product.productRef);
439
+ this.update();
440
+ this.refreshVisibility();
441
+ });
442
+ state.inputs.push(input);
443
+ label.append(text, input);
444
+ options.append(label);
445
+ }
446
+ state.select = this.button("commerceOptions", () => {
447
+ if (!this.fresh() ||
448
+ this.busy ||
449
+ state.inputs.some((input) => !input.value))
450
+ return;
451
+ const selected = state.inputs.map((input) => ({
452
+ name: input.name,
453
+ value: input.value,
454
+ }));
455
+ this.setBusy(true);
456
+ this.runAction(() => this.callbacks.onSelection(product.productRef, selected));
457
+ });
458
+ options.append(state.select);
459
+ card.append(options);
460
+ }
461
+ if (product.variant) {
462
+ const quantityLabel = document.createElement("label");
463
+ quantityLabel.className =
464
+ "commerce-product-label commerce-product-quantity";
465
+ const text = document.createElement("span");
466
+ text.textContent = this.message("commerceQuantity");
467
+ const quantity = document.createElement("input");
468
+ quantity.type = "number";
469
+ quantity.inputMode = "numeric";
470
+ quantity.required = true;
471
+ const rule = product.variant.quantityRule;
472
+ quantity.min = String(rule.minimum);
473
+ if (rule.maximum !== null)
474
+ quantity.max = String(rule.maximum);
475
+ quantity.step = String(rule.increment);
476
+ quantity.value = validQuantityRule(rule) ? String(rule.minimum) : "";
477
+ quantity.addEventListener("input", () => this.update());
478
+ quantityLabel.append(text, quantity);
479
+ state.quantity = quantity;
480
+ state.add = this.button("commerceAdd", () => {
481
+ if (!this.addAllowed(state))
482
+ return;
483
+ const amount = Number(quantity.value);
484
+ this.setBusy(true);
485
+ this.runAction(() => this.addHandler(product.productRef, product.variant.variantRef, amount), "commerceCartUncertain");
486
+ });
487
+ card.append(quantityLabel, state.add);
488
+ }
489
+ if (product.variant?.requiresSellingPlan) {
490
+ const note = document.createElement("p");
491
+ note.className = "commerce-product-note";
492
+ note.textContent = this.message("commerceSubscription");
493
+ state.planNote = note;
494
+ card.append(note);
495
+ }
496
+ if (this.callbacks.onSaveItem) {
497
+ state.save = this.button("shoppingNotesSaveItem", () => {
498
+ if (!this.saveAllowed(state))
499
+ return;
500
+ const label = product.title.trim();
501
+ this.callbacks.onSaveItem?.({
502
+ productRef: product.productRef,
503
+ variantRef: product.variant?.variantRef ?? null,
504
+ selectedOptions: product.variant?.selectedOptions.map((option) => ({ ...option })) ??
505
+ [],
506
+ ...(label && label.length <= 120 ? { label } : {}),
507
+ });
508
+ });
509
+ card.append(state.save);
510
+ }
511
+ const url = safeUrl(product.url, this.catalog.store.origin);
512
+ if (url) {
513
+ const link = document.createElement("a");
514
+ link.className = "commerce-product-link";
515
+ link.href = url;
516
+ link.target = "_self";
517
+ link.textContent = this.message(product.options.some((option) => !option.complete)
518
+ ? "commerceMoreOptions"
519
+ : "commerceView");
520
+ card.append(link);
521
+ }
522
+ return state;
523
+ }
524
+ fresh() {
525
+ return (!this.destroyed &&
526
+ !this.failed &&
527
+ !!this.catalog &&
528
+ Date.parse(this.catalog.expiresAt) > Date.now());
529
+ }
530
+ saveAllowed(state) {
531
+ return (this.fresh() &&
532
+ !this.busy &&
533
+ !state.dirty &&
534
+ !state.element.hidden &&
535
+ state.inputs.every((input) => input.value ===
536
+ (state.product.variant?.selectedOptions.find((option) => option.name === input.name)?.value ?? "")));
537
+ }
538
+ addAllowed(state) {
539
+ const variant = state.product.variant;
540
+ if (!this.fresh() ||
541
+ this.busy ||
542
+ state.dirty ||
543
+ !variant ||
544
+ !variant.available ||
545
+ variant.requiresSellingPlan ||
546
+ !state.quantity ||
547
+ !this.addHandler ||
548
+ !this.canAdd)
549
+ return false;
550
+ const quantity = Number(state.quantity.value);
551
+ const rule = variant.quantityRule;
552
+ if (!validQuantityRule(rule) ||
553
+ !state.quantity.value ||
554
+ !Number.isSafeInteger(quantity) ||
555
+ quantity < rule.minimum ||
556
+ (rule.maximum !== null && quantity > rule.maximum) ||
557
+ quantity % rule.increment !== 0)
558
+ return false;
559
+ try {
560
+ return (this.canAdd(state.product.productRef, variant.variantRef, quantity) ===
561
+ true);
562
+ }
563
+ catch {
564
+ return false;
565
+ }
566
+ }
567
+ update() {
568
+ this.element.setAttribute("aria-busy", String(this.busy));
569
+ const fresh = this.fresh();
570
+ this.status.textContent = this.busy
571
+ ? this.message("commerceLoading")
572
+ : this.failed
573
+ ? this.message(this.errorMessage)
574
+ : this.catalog && !fresh
575
+ ? this.message("commerceExpired")
576
+ : this.catalog && !this.states.length
577
+ ? this.message("commerceEmpty")
578
+ : "";
579
+ this.status.hidden = !this.status.textContent;
580
+ this.ranking.hidden = !this.catalog || !this.states.length;
581
+ this.element.dataset.comparing = String(!!this.comparison);
582
+ let checked = 0;
583
+ for (const state of this.states) {
584
+ const product = state.product;
585
+ if (state.image)
586
+ state.image.hidden =
587
+ state.dirty || state.image.dataset.failed === "true";
588
+ if (state.planNote)
589
+ state.planNote.hidden = state.dirty;
590
+ state.element.hidden =
591
+ !!this.comparison && !this.comparison.has(product.productRef);
592
+ if (state.save)
593
+ state.save.disabled = !this.saveAllowed(state);
594
+ state.compare.disabled = !fresh || this.busy;
595
+ state.compare.parentElement.hidden = !!this.comparison;
596
+ if (state.compare.checked)
597
+ checked += 1;
598
+ for (const input of state.inputs)
599
+ input.disabled = !fresh || this.busy;
600
+ if (state.select)
601
+ state.select.disabled =
602
+ !fresh ||
603
+ this.busy ||
604
+ !state.dirty ||
605
+ state.inputs.some((input) => !input.value);
606
+ const variant = !state.dirty ? product.variant : null;
607
+ state.price.textContent = variant
608
+ ? (0, exports.formatCommercePrice)(variant.price.amount, variant.price.currency, this.catalog.market.locale)
609
+ : "";
610
+ state.price.hidden = !variant;
611
+ state.availability.textContent = this.message(!variant
612
+ ? "commerceUnknown"
613
+ : variant.available
614
+ ? "commerceAvailable"
615
+ : "commerceUnavailable");
616
+ if (state.quantity && state.add) {
617
+ const hasCartHandler = !!this.addHandler &&
618
+ !!this.canAdd &&
619
+ !!variant &&
620
+ variant.available &&
621
+ validQuantityRule(variant.quantityRule) &&
622
+ !variant.requiresSellingPlan;
623
+ state.quantity.parentElement.hidden = !hasCartHandler;
624
+ state.quantity.disabled = !fresh || this.busy || !hasCartHandler;
625
+ state.add.hidden = !hasCartHandler;
626
+ state.add.disabled = !this.addAllowed(state);
627
+ }
628
+ }
629
+ this.compareButton.hidden = !!this.comparison || this.states.length < 2;
630
+ this.compareButton.disabled = !fresh || this.busy || checked < 2;
631
+ this.backButton.hidden = !this.comparison;
632
+ this.backButton.disabled = this.busy;
633
+ this.refreshButton.disabled = this.busy;
634
+ }
635
+ compare() {
636
+ if (!this.fresh() || this.busy)
637
+ return;
638
+ const selected = this.states.filter((state) => state.compare.checked);
639
+ if (selected.length < 2)
640
+ return;
641
+ this.comparison = new Set(selected.map((state) => state.product.productRef));
642
+ this.update();
643
+ this.refreshVisibility();
644
+ this.backButton.focus({ preventScroll: true });
645
+ const productRefs = [...this.comparison];
646
+ this.runAction(() => this.callbacks.onCompare(productRefs));
647
+ }
648
+ runAction(action, error = "commerceError") {
649
+ const catalog = this.catalog;
650
+ const reject = () => {
651
+ if (!this.destroyed && this.catalog === catalog)
652
+ this.showError(error);
653
+ };
654
+ try {
655
+ void Promise.resolve(action()).catch(reject);
656
+ }
657
+ catch {
658
+ reject();
659
+ }
660
+ }
661
+ }
662
+ exports.CommerceProductPanel = CommerceProductPanel;