ui.shipaid.com 0.3.146 → 0.3.147

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.
package/dist/widget.es.js CHANGED
@@ -1,6 +1,19 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+ function getIdFromShopifyGid(gid) {
5
+ var _a;
6
+ return ((_a = gid == null ? void 0 : gid.match(/\d+/)) == null ? void 0 : _a[0]) ?? null;
7
+ }
8
+ const normalizeVariantId$1 = (value) => {
9
+ if (value === null || value === void 0) return null;
10
+ if (typeof value === "number") return String(value);
11
+ if (typeof value === "string") {
12
+ const numericId = getIdFromShopifyGid(value);
13
+ return numericId ?? value;
14
+ }
15
+ return null;
16
+ };
4
17
  const getExcludedProducts = (store) => {
5
18
  const excludedProductSkus = Array.isArray(
6
19
  store == null ? void 0 : store.excludedProductSkus
@@ -28,26 +41,43 @@ function calculateProtectionTotal(store, protectionProduct, cart) {
28
41
  throw new Error("Tried to find protection variant, but protection settings for this store are missing.");
29
42
  }
30
43
  const { excludedProductSkus, excludedProductTags, excludedProductIds } = getExcludedProducts(store);
44
+ const excludedVariantIdSet = new Set(
45
+ excludedProductIds.map((id) => String(id))
46
+ );
47
+ const protectionVariantIdSet = new Set(
48
+ ((protectionProduct == null ? void 0 : protectionProduct.variants) ?? []).map((variant) => normalizeVariantId$1(variant == null ? void 0 : variant.id)).filter((id) => Boolean(id))
49
+ );
50
+ const isProtectionVariant = (item) => {
51
+ const normalizedId = normalizeVariantId$1(item.variant_id);
52
+ if (!normalizedId) return false;
53
+ if (protectionVariantIdSet.has(normalizedId)) return true;
54
+ return protectionVariantIdSet.has(String(normalizedId));
55
+ };
31
56
  const isItemExcluded = (item) => {
57
+ if (isProtectionVariant(item)) return false;
32
58
  if (item.isVirtualProduct) {
33
59
  return true;
34
- } else if (item.sku && excludedProductSkus.includes(item.sku.trim())) {
60
+ }
61
+ if (item.sku && excludedProductSkus.includes(item.sku.trim())) {
35
62
  return true;
36
- } else if (item.tags && item.tags.some((t2) => excludedProductTags.includes(t2.toLowerCase().trim()))) {
63
+ }
64
+ if (item.tags && item.tags.some((t2) => excludedProductTags.includes(t2.toLowerCase().trim()))) {
37
65
  return true;
38
- } else if (item.variant_id && excludedProductIds.includes(item.variant_id)) {
66
+ }
67
+ const normalizedId = normalizeVariantId$1(item.variant_id);
68
+ if (normalizedId && excludedVariantIdSet.has(normalizedId)) {
39
69
  return true;
40
70
  }
41
71
  return false;
42
72
  };
73
+ const protectionVariantsInCart = ((_a = cart.items) == null ? void 0 : _a.filter((item) => isProtectionVariant(item))) ?? [];
74
+ const protectionVariantsInCartTotal = protectionVariantsInCart.reduce(
75
+ (total, item) => total + (Number(item.final_line_price) || 0),
76
+ 0
77
+ );
43
78
  const itemTotal = (cart.items ?? []).reduce((total, item) => {
44
- return isItemExcluded(item) ? total - item.final_line_price : total;
45
- }, cart.total_price || 0);
46
- const protectionVariantsInCart = ((_a = cart.items) == null ? void 0 : _a.filter((item) => {
47
- var _a2;
48
- return (_a2 = protectionProduct == null ? void 0 : protectionProduct.variants) == null ? void 0 : _a2.some((variant) => variant.id === item.variant_id);
49
- })) ?? [];
50
- const protectionVariantsInCartTotal = protectionVariantsInCart.reduce((total, item) => total + item.final_line_price, 0);
79
+ return isItemExcluded(item) ? total - (Number(item.final_line_price) || 0) : total;
80
+ }, Number(cart.total_price) || 0);
51
81
  const cartTotal = itemTotal - protectionVariantsInCartTotal;
52
82
  const allowZeroCartValueProtection = (_b = store.widgetConfigurations) == null ? void 0 : _b.allowZeroCartValueProtection;
53
83
  if (!allowZeroCartValueProtection) {
@@ -98,10 +128,6 @@ function findProtectionVariant(store, protectionProduct, protectionFee) {
98
128
  }
99
129
  return matchingVariant;
100
130
  }
101
- function getIdFromShopifyGid(gid) {
102
- var _a;
103
- return ((_a = gid == null ? void 0 : gid.match(/\d+/)) == null ? void 0 : _a[0]) ?? null;
104
- }
105
131
  const common = { calculateProtectionTotal, findProtectionVariant };
106
132
  /**
107
133
  * @license
@@ -3101,6 +3127,7 @@ var SHIPAID_EVENTS = /* @__PURE__ */ ((SHIPAID_EVENTS2) => {
3101
3127
  })(SHIPAID_EVENTS || {});
3102
3128
  var EXTERNAL_EVENTS = /* @__PURE__ */ ((EXTERNAL_EVENTS2) => {
3103
3129
  EXTERNAL_EVENTS2["CART_UPDATE"] = "cart:update";
3130
+ EXTERNAL_EVENTS2["CART_UPDATED"] = "cart:updated";
3104
3131
  EXTERNAL_EVENTS2["VARIANT_CHANGE"] = "variant:change";
3105
3132
  return EXTERNAL_EVENTS2;
3106
3133
  })(EXTERNAL_EVENTS || {});
@@ -3479,6 +3506,78 @@ const openCarousel = (from) => {
3479
3506
  } catch {
3480
3507
  }
3481
3508
  };
3509
+ const refreshShopifyCartUI = async () => {
3510
+ try {
3511
+ const sectionIds = ["cart-drawer", "cart-icon-bubble", "main-cart-items"];
3512
+ const url = `/?sections=${encodeURIComponent(sectionIds.join(","))}`;
3513
+ const res = await fetch(url, { credentials: "same-origin" });
3514
+ if (!res.ok) return;
3515
+ const htmlById = await res.json().catch(() => null);
3516
+ if (!htmlById || typeof htmlById !== "object") return;
3517
+ for (const id of Object.keys(htmlById)) {
3518
+ const html = htmlById[id];
3519
+ if (typeof html !== "string") continue;
3520
+ const tmp = document.createElement("div");
3521
+ tmp.innerHTML = html;
3522
+ if (id === "cart-drawer") {
3523
+ try {
3524
+ const newDrawer = tmp.querySelector("cart-drawer");
3525
+ const oldDrawer = document.querySelector("cart-drawer");
3526
+ if (newDrawer && oldDrawer) {
3527
+ const shouldBeEmpty = newDrawer.classList.contains("is-empty");
3528
+ if (shouldBeEmpty) oldDrawer.classList.add("is-empty");
3529
+ else oldDrawer.classList.remove("is-empty");
3530
+ }
3531
+ } catch {
3532
+ }
3533
+ try {
3534
+ const newDrawerItems = tmp.querySelector("cart-drawer-items");
3535
+ const oldDrawerItems = document.querySelector("cart-drawer cart-drawer-items, .cart-drawer cart-drawer-items");
3536
+ if (newDrawerItems && oldDrawerItems) {
3537
+ oldDrawerItems.outerHTML = newDrawerItems.outerHTML;
3538
+ }
3539
+ } catch {
3540
+ }
3541
+ const newItems = tmp.querySelector("#CartDrawer-CartItems");
3542
+ const oldItems = document.getElementById("CartDrawer-CartItems");
3543
+ if (newItems && oldItems) {
3544
+ oldItems.outerHTML = newItems.outerHTML;
3545
+ }
3546
+ const newFooter = tmp.querySelector(".cart-drawer__footer");
3547
+ const oldFooter = document.querySelector(".cart-drawer .cart-drawer__footer");
3548
+ if (newFooter && oldFooter) {
3549
+ oldFooter.outerHTML = newFooter.outerHTML;
3550
+ }
3551
+ const newCtas = tmp.querySelector(".cart__ctas");
3552
+ const oldCtas = document.querySelector(".cart-drawer .cart__ctas");
3553
+ if (newCtas && oldCtas) {
3554
+ oldCtas.outerHTML = newCtas.outerHTML;
3555
+ }
3556
+ continue;
3557
+ }
3558
+ if (id === "main-cart-items") {
3559
+ const newMainItems = tmp.querySelector("#main-cart-items");
3560
+ const oldMainItems = document.getElementById("main-cart-items");
3561
+ if (newMainItems && oldMainItems) {
3562
+ oldMainItems.outerHTML = newMainItems.outerHTML;
3563
+ } else {
3564
+ const section = document.getElementById("shopify-section-main-cart-items");
3565
+ if (section) section.outerHTML = html;
3566
+ }
3567
+ continue;
3568
+ }
3569
+ if (id === "cart-icon-bubble") {
3570
+ const newBubble = tmp.querySelector("#cart-icon-bubble, [data-cart-count-bubble]");
3571
+ const oldBubble = document.querySelector("#cart-icon-bubble, [data-cart-count-bubble]");
3572
+ if (newBubble && oldBubble) {
3573
+ oldBubble.replaceWith(newBubble);
3574
+ }
3575
+ continue;
3576
+ }
3577
+ }
3578
+ } catch {
3579
+ }
3580
+ };
3482
3581
  var __defProp$2 = Object.defineProperty;
3483
3582
  var __decorateClass$2 = (decorators, target, key, kind) => {
3484
3583
  var result = void 0;
@@ -3527,6 +3626,39 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
3527
3626
  };
3528
3627
  this.intervalId = null;
3529
3628
  this._sellingPlanId = null;
3629
+ this._isUpdatingShipAid = false;
3630
+ this._lastCartEventAt = 0;
3631
+ this._isReplacingCartSections = false;
3632
+ this._onShipAidCartEvent = (event) => {
3633
+ var _a2, _b2;
3634
+ if (((_b2 = (_a2 = event == null ? void 0 : event.detail) == null ? void 0 : _a2.data) == null ? void 0 : _b2.source) === "shipaid-widget") return;
3635
+ if (this._isUpdatingShipAid) return;
3636
+ const now = Date.now();
3637
+ if (now - this._lastCartEventAt < 500) return;
3638
+ this._lastCartEventAt = now;
3639
+ try {
3640
+ this.updateCart().catch(() => {
3641
+ });
3642
+ } catch {
3643
+ }
3644
+ };
3645
+ this._onShipAidInternalCartUpdated = (event) => {
3646
+ var _a2, _b2, _c2, _d;
3647
+ const src = ((_b2 = (_a2 = event == null ? void 0 : event.detail) == null ? void 0 : _a2.data) == null ? void 0 : _b2.source) || ((_c2 = event == null ? void 0 : event.detail) == null ? void 0 : _c2.source);
3648
+ if (src === "shipaid-widget") return;
3649
+ if (this._isUpdatingShipAid) return;
3650
+ const providedCart = (_d = event == null ? void 0 : event.detail) == null ? void 0 : _d.cart;
3651
+ try {
3652
+ if (providedCart) {
3653
+ this.updateCart(providedCart).catch(() => {
3654
+ });
3655
+ } else {
3656
+ this.updateCart().catch(() => {
3657
+ });
3658
+ }
3659
+ } catch {
3660
+ }
3661
+ };
3530
3662
  this._pendingLearnMoreSource = null;
3531
3663
  this._handleHelpClick = (event) => {
3532
3664
  if (event) event.preventDefault();
@@ -3727,11 +3859,16 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
3727
3859
  async _handleRefresh(input) {
3728
3860
  const isCart = Reflect.has(input, "items");
3729
3861
  if (this.shouldRefreshOnUpdate) return window.location.reload();
3730
- if (!isCart) await this.updateCart();
3862
+ if (isCart) {
3863
+ await this.updateCart(input, { dispatchExternalEvents: true });
3864
+ } else {
3865
+ await this.updateCart(void 0, { dispatchExternalEvents: true });
3866
+ }
3731
3867
  this._dispatchEvent(SHIPAID_EVENTS.CART_UPDATED, {
3732
3868
  protection: this._hasProtectionInCart,
3733
3869
  cart: isCart ? input : this._cart,
3734
- lineItem: !isCart ? input : this._protectionCartItem
3870
+ lineItem: !isCart ? input : this._protectionCartItem,
3871
+ data: { source: "shipaid-widget" }
3735
3872
  });
3736
3873
  }
3737
3874
  /** Given the current order, it calculates the protection total according to the store protection settings. */
@@ -3995,9 +4132,28 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
3995
4132
  return this._hasProtectionInCart;
3996
4133
  }
3997
4134
  /** Update the internal cart, which will trigger any protection fee updates */
3998
- async updateCart(cart) {
4135
+ async updateCart(cart, options) {
3999
4136
  if (!cart) cart = await this._fetchCart();
4000
4137
  this._cart = cart;
4138
+ const shouldDispatchExternal = (options == null ? void 0 : options.dispatchExternalEvents) ?? false;
4139
+ if (shouldDispatchExternal) {
4140
+ try {
4141
+ document.dispatchEvent(new CustomEvent(EXTERNAL_EVENTS.CART_UPDATE, {
4142
+ detail: { data: { source: "shipaid-widget" }, cart }
4143
+ }));
4144
+ window.dispatchEvent(new CustomEvent(EXTERNAL_EVENTS.CART_UPDATE, {
4145
+ detail: { data: { source: "shipaid-widget" }, cart }
4146
+ }));
4147
+ } catch {
4148
+ }
4149
+ }
4150
+ try {
4151
+ this._isReplacingCartSections = true;
4152
+ await refreshShopifyCartUI();
4153
+ } catch {
4154
+ } finally {
4155
+ this._isReplacingCartSections = false;
4156
+ }
4001
4157
  }
4002
4158
  async addCartProtectionVariant() {
4003
4159
  var _a, _b;
@@ -4116,12 +4272,28 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
4116
4272
  /** Add ShipAid shipping protection. */
4117
4273
  async addProtection() {
4118
4274
  var _a, _b;
4275
+ if (this._isUpdatingShipAid) {
4276
+ return;
4277
+ }
4119
4278
  try {
4279
+ this._isUpdatingShipAid = true;
4280
+ this._disableCheckoutButtons();
4120
4281
  if (!this._store) throw new Error("Store has not been loaded.");
4121
- if (!((_a = this._cart) == null ? void 0 : _a.items)) throw new Error("Cart has not been loaded.");
4122
- if (!((_b = this._protectionVariant) == null ? void 0 : _b.id)) {
4282
+ if (!((_a = this._protectionVariant) == null ? void 0 : _a.id)) {
4123
4283
  throw new Error("No protection variant found.");
4124
4284
  }
4285
+ this._cart = await this._fetchCart();
4286
+ if (!((_b = this._cart) == null ? void 0 : _b.items)) throw new Error("Cart has not been loaded.");
4287
+ const existingProtection = this._cart.items.find((item) => {
4288
+ var _a2, _b2;
4289
+ return (_b2 = (_a2 = this._protectionProduct) == null ? void 0 : _a2.variants) == null ? void 0 : _b2.some(
4290
+ (variant) => variant.id === item.variant_id
4291
+ );
4292
+ });
4293
+ if (existingProtection) {
4294
+ this._hasProtectionInCart = true;
4295
+ return;
4296
+ }
4125
4297
  this._setState("loading");
4126
4298
  const cart = await this.addCartProtectionVariant();
4127
4299
  await this._handleRefresh(cart);
@@ -4132,11 +4304,15 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
4132
4304
  } finally {
4133
4305
  this._cart = await this._fetchCart();
4134
4306
  this._setState("success");
4307
+ this._isUpdatingShipAid = false;
4308
+ this._enableCheckoutButtons();
4135
4309
  }
4136
4310
  }
4137
4311
  /** Remove ShipAid shipping protection. */
4138
4312
  async removeProtection() {
4139
4313
  try {
4314
+ this._isUpdatingShipAid = true;
4315
+ this._disableCheckoutButtons();
4140
4316
  if (!this._store) throw new Error("Store has not been loaded.");
4141
4317
  if (!this._protectionCartItem) {
4142
4318
  throw new Error("Protection product not found.");
@@ -4155,6 +4331,8 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
4155
4331
  } finally {
4156
4332
  this._cart = await this._fetchCart();
4157
4333
  this._setState("success");
4334
+ this._isUpdatingShipAid = false;
4335
+ this._enableCheckoutButtons();
4158
4336
  }
4159
4337
  }
4160
4338
  /** Try adding ShipAid shipping protection during polling if applicable */
@@ -4162,6 +4340,7 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
4162
4340
  var _a, _b, _c, _d, _e, _f;
4163
4341
  if (!((_a = this._store) == null ? void 0 : _a.widgetAutoOptIn) || this.useShipAidCheckout) return;
4164
4342
  if (!((_b = this._cart) == null ? void 0 : _b.items) || !((_c = this._cart) == null ? void 0 : _c.item_count)) return;
4343
+ if (this._isUpdatingShipAid) return;
4165
4344
  const protectionCartItemIndex = (_d = this._cart.items) == null ? void 0 : _d.findIndex((item) => {
4166
4345
  var _a2, _b2;
4167
4346
  return (_b2 = (_a2 = this._protectionProduct) == null ? void 0 : _a2.variants) == null ? void 0 : _b2.some(
@@ -4187,29 +4366,63 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
4187
4366
  }
4188
4367
  }
4189
4368
  async handleMultipleProtectionVariants() {
4190
- var _a, _b, _c, _d, _e;
4369
+ var _a, _b, _c;
4191
4370
  if (!((_a = this._cart) == null ? void 0 : _a.items) || !((_b = this._cart) == null ? void 0 : _b.item_count)) return;
4192
- let count = 0;
4371
+ const protectionItems = [];
4193
4372
  (_c = this._cart.items) == null ? void 0 : _c.forEach((item) => {
4194
4373
  var _a2, _b2;
4195
4374
  const isProtectionItem = (_b2 = (_a2 = this._protectionProduct) == null ? void 0 : _a2.variants) == null ? void 0 : _b2.some(
4196
4375
  (variant) => variant.id === item.variant_id
4197
4376
  );
4198
- if (isProtectionItem) count++;
4377
+ if (isProtectionItem) {
4378
+ protectionItems.push(item);
4379
+ }
4199
4380
  });
4200
- if (count > 1) {
4201
- const protectionCartItemIndex = (_d = this._cart.items) == null ? void 0 : _d.findIndex((item) => {
4202
- var _a2, _b2;
4203
- return (_b2 = (_a2 = this._protectionProduct) == null ? void 0 : _a2.variants) == null ? void 0 : _b2.some(
4204
- (variant) => variant.id === item.variant_id
4205
- );
4381
+ if (protectionItems.length > 1) {
4382
+ for (let i3 = 1; i3 < protectionItems.length; i3++) {
4383
+ await this.updateCartProtectionVariant(0, protectionItems[i3]);
4384
+ }
4385
+ this._cart = await this._fetchCart();
4386
+ return await this._handleRefresh(this._cart);
4387
+ }
4388
+ }
4389
+ /** Disable checkout buttons while updating protection */
4390
+ _disableCheckoutButtons() {
4391
+ var _a, _b;
4392
+ try {
4393
+ const providedSelector = (_b = (_a = this._store) == null ? void 0 : _a.widgetConfigurations) == null ? void 0 : _b.checkoutButtonSelector;
4394
+ const selectors = providedSelector ? providedSelector.split(",").map((s3) => s3.trim()).filter(Boolean) : DEFAULT_CHECKOUT_SELECTORS;
4395
+ const buttons = /* @__PURE__ */ new Set();
4396
+ selectors.forEach((selector) => {
4397
+ try {
4398
+ document.querySelectorAll(selector).forEach((btn) => {
4399
+ buttons.add(btn);
4400
+ });
4401
+ } catch (e3) {
4402
+ }
4206
4403
  });
4207
- const protectionCartItem = (_e = this._cart) == null ? void 0 : _e.items[protectionCartItemIndex];
4208
- const cart = await this.updateCartProtectionVariant(
4209
- 0,
4210
- protectionCartItem
4211
- );
4212
- return await this._handleRefresh(cart);
4404
+ buttons.forEach((button) => {
4405
+ button.setAttribute("disabled", "true");
4406
+ button.style.opacity = "0.6";
4407
+ button.style.pointerEvents = "none";
4408
+ button.setAttribute("data-shipaid-disabled", "true");
4409
+ });
4410
+ } catch (error) {
4411
+ console.error("[ShipAid] Error disabling checkout buttons:", error);
4412
+ }
4413
+ }
4414
+ /** Re-enable checkout buttons after updating protection */
4415
+ _enableCheckoutButtons() {
4416
+ try {
4417
+ const buttons = document.querySelectorAll('[data-shipaid-disabled="true"]');
4418
+ buttons.forEach((button) => {
4419
+ button.removeAttribute("disabled");
4420
+ button.style.opacity = "";
4421
+ button.style.pointerEvents = "";
4422
+ button.removeAttribute("data-shipaid-disabled");
4423
+ });
4424
+ } catch (error) {
4425
+ console.error("[ShipAid] Error enabling checkout buttons:", error);
4213
4426
  }
4214
4427
  }
4215
4428
  /** Templates */
@@ -4959,57 +5172,103 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
4959
5172
  await use(this.lang);
4960
5173
  this.hasLoadedStrings = true;
4961
5174
  this.fetchInterceptorCleanup = interceptFetch(async (args, promise) => {
4962
- var _a, _b, _c, _d;
5175
+ var _a, _b;
4963
5176
  if ((_b = (_a = args[1]) == null ? void 0 : _a.headers) == null ? void 0 : _b["X-ShipAid"]) {
4964
5177
  return;
4965
5178
  }
4966
5179
  if (!args[0].startsWith("/cart/change") && !args[0].startsWith("/cart/update")) {
4967
5180
  return;
4968
5181
  }
4969
- const buttonSelector = ((_d = (_c = this._store) == null ? void 0 : _c.widgetConfigurations) == null ? void 0 : _d.checkoutButtonSelector) || 'button[type="submit"][name="checkout"][form="cart"]';
4970
- const button = document.querySelector(buttonSelector);
4971
- console.log("q", button);
4972
- if (!button) {
5182
+ if (this._isUpdatingShipAid) {
4973
5183
  return;
4974
5184
  }
4975
- button.setAttribute("disabled", "true");
4976
- console.debug("button", "t");
4977
5185
  try {
5186
+ this._isUpdatingShipAid = true;
5187
+ this._disableCheckoutButtons();
4978
5188
  await promise;
4979
5189
  await this.updateCart();
4980
- await this.updateProtection();
5190
+ await this.updateProtection(true);
5191
+ } catch (error) {
5192
+ console.error("[ShipAid] Interceptor error:", error);
4981
5193
  } finally {
4982
- button.removeAttribute("disabled");
4983
- console.debug("button", "f");
5194
+ this._isUpdatingShipAid = false;
5195
+ this._enableCheckoutButtons();
4984
5196
  }
4985
5197
  });
5198
+ try {
5199
+ document.addEventListener(EXTERNAL_EVENTS.CART_UPDATE, this._onShipAidCartEvent);
5200
+ } catch {
5201
+ }
5202
+ try {
5203
+ window.addEventListener(EXTERNAL_EVENTS.CART_UPDATE, this._onShipAidCartEvent);
5204
+ } catch {
5205
+ }
5206
+ try {
5207
+ document.addEventListener(SHIPAID_EVENTS.CART_UPDATED, this._onShipAidInternalCartUpdated);
5208
+ } catch {
5209
+ }
4986
5210
  }
4987
5211
  disconnectedCallback() {
4988
5212
  var _a;
4989
5213
  super.disconnectedCallback();
4990
5214
  (_a = this.fetchInterceptorCleanup) == null ? void 0 : _a.call(this);
5215
+ try {
5216
+ document.removeEventListener(EXTERNAL_EVENTS.CART_UPDATE, this._onShipAidCartEvent);
5217
+ } catch {
5218
+ }
5219
+ try {
5220
+ window.removeEventListener(EXTERNAL_EVENTS.CART_UPDATE, this._onShipAidCartEvent);
5221
+ } catch {
5222
+ }
5223
+ try {
5224
+ document.removeEventListener(SHIPAID_EVENTS.CART_UPDATED, this._onShipAidInternalCartUpdated);
5225
+ } catch {
5226
+ }
4991
5227
  }
4992
- async updateProtection() {
4993
- var _a, _b, _c, _d;
5228
+ async updateProtection(force = false) {
5229
+ var _a, _b, _c;
4994
5230
  this._cartLastUpdated = /* @__PURE__ */ new Date();
4995
5231
  if (!((_a = this._cart) == null ? void 0 : _a.items)) return;
4996
- const protectionCartItemIndex = (_b = this._cart.items) == null ? void 0 : _b.findIndex((item) => {
5232
+ if (this._isUpdatingShipAid && !force) {
5233
+ return;
5234
+ }
5235
+ const allProtectionItems = this._cart.items.filter((item) => {
4997
5236
  var _a2, _b2;
4998
5237
  return (_b2 = (_a2 = this._protectionProduct) == null ? void 0 : _a2.variants) == null ? void 0 : _b2.some(
4999
5238
  (variant) => variant.id === item.variant_id
5000
5239
  );
5001
5240
  });
5002
- const protectionCartItem = (_c = this._cart) == null ? void 0 : _c.items[protectionCartItemIndex];
5241
+ if (allProtectionItems.length > 1) {
5242
+ try {
5243
+ this._isUpdatingShipAid = true;
5244
+ this._disableCheckoutButtons();
5245
+ for (let i3 = 1; i3 < allProtectionItems.length; i3++) {
5246
+ await this.updateCartProtectionVariant(0, allProtectionItems[i3]);
5247
+ await new Promise((resolve) => setTimeout(resolve, 100));
5248
+ }
5249
+ this._cart = await this._fetchCart();
5250
+ } finally {
5251
+ this._isUpdatingShipAid = false;
5252
+ this._enableCheckoutButtons();
5253
+ }
5254
+ }
5255
+ const protectionCartItemIndex = ((_b = this._cart.items) == null ? void 0 : _b.findIndex((item) => {
5256
+ var _a2, _b2;
5257
+ return (_b2 = (_a2 = this._protectionProduct) == null ? void 0 : _a2.variants) == null ? void 0 : _b2.some(
5258
+ (variant) => variant.id === item.variant_id
5259
+ );
5260
+ })) ?? -1;
5261
+ const protectionCartItem = protectionCartItemIndex >= 0 && this._cart.items ? this._cart.items[protectionCartItemIndex] : void 0;
5003
5262
  this._hasProtectionInCart = !!protectionCartItem;
5004
5263
  if (!this._store) return;
5005
5264
  const protectionFee = await this.calculateProtectionTotal(this._cart);
5006
5265
  if (this._cart.item_count > 0 && !!protectionCartItem && (this._cart.total_price === (protectionCartItem == null ? void 0 : protectionCartItem.final_line_price) || !protectionFee)) {
5007
- const cart2 = await this.updateCartProtectionVariant(
5266
+ const cart = await this.updateCartProtectionVariant(
5008
5267
  0,
5009
5268
  protectionCartItem
5010
5269
  );
5011
5270
  sessionStorage.removeItem(LOCAL_STORAGE_KEY);
5012
- return await this._handleRefresh(cart2);
5271
+ return await this._handleRefresh(cart);
5013
5272
  }
5014
5273
  const protectionVariant = this._findProtectionVariant(protectionFee);
5015
5274
  if (!protectionFee) {
@@ -5026,7 +5285,7 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
5026
5285
  logger.error("No matching protection variant found.");
5027
5286
  return;
5028
5287
  }
5029
- if (!((_d = this._protectionVariant) == null ? void 0 : _d.id)) {
5288
+ if (!((_c = this._protectionVariant) == null ? void 0 : _c.id)) {
5030
5289
  this._shouldShowWidget = false;
5031
5290
  return;
5032
5291
  }
@@ -5034,7 +5293,7 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
5034
5293
  this._sellingPlanId = await this.getSubscription(this._cart);
5035
5294
  }
5036
5295
  if (!protectionCartItem) return;
5037
- if (this.supportSubscriptions && !this.useShipAidCheckout) {
5296
+ if (this.supportSubscriptions && !this.useShipAidCheckout && this._cart.items) {
5038
5297
  const itemWithSubscription = this._cart.items.find(
5039
5298
  (item) => {
5040
5299
  var _a2;
@@ -5050,38 +5309,60 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
5050
5309
  shouldUpdateSubscription = false;
5051
5310
  }
5052
5311
  if (shouldUpdateSubscription) {
5053
- const cart2 = await this.updateCartProtectionVariant(
5054
- 1,
5055
- protectionCartItem
5056
- );
5057
- await this._handleRefresh(cart2);
5312
+ try {
5313
+ this._isUpdatingShipAid = true;
5314
+ this._disableCheckoutButtons();
5315
+ const cart = await this.updateCartProtectionVariant(
5316
+ 1,
5317
+ protectionCartItem
5318
+ );
5319
+ await this._handleRefresh(cart);
5320
+ } finally {
5321
+ this._isUpdatingShipAid = false;
5322
+ this._enableCheckoutButtons();
5323
+ }
5058
5324
  }
5059
5325
  }
5060
- if (protectionVariant.id === protectionCartItem.variant_id) {
5326
+ if (protectionCartItem && protectionVariant.id === protectionCartItem.variant_id) {
5061
5327
  this._protectionCartItem = {
5062
5328
  ...protectionCartItem,
5063
5329
  index: protectionCartItemIndex,
5064
5330
  position: protectionCartItemIndex + 1
5065
5331
  };
5066
5332
  if (protectionCartItem.quantity === 1) return;
5067
- const cart2 = await this.updateCartProtectionVariant(
5068
- 1,
5069
- protectionCartItem
5333
+ try {
5334
+ this._isUpdatingShipAid = true;
5335
+ this._disableCheckoutButtons();
5336
+ const cart = await this.updateCartProtectionVariant(
5337
+ 1,
5338
+ protectionCartItem
5339
+ );
5340
+ this._handleRefreshCart();
5341
+ await this._handleRefresh(cart);
5342
+ } finally {
5343
+ this._isUpdatingShipAid = false;
5344
+ this._enableCheckoutButtons();
5345
+ }
5346
+ return;
5347
+ }
5348
+ try {
5349
+ this._isUpdatingShipAid = true;
5350
+ this._disableCheckoutButtons();
5351
+ const updatePayload = {
5352
+ updates: {
5353
+ [protectionCartItem.variant_id]: 0,
5354
+ [protectionVariant.id]: 1
5355
+ }
5356
+ };
5357
+ const cart = await this._fetch.post(
5358
+ "/cart/update.js",
5359
+ updatePayload
5070
5360
  );
5071
- this._handleRefreshCart();
5072
- return await this._handleRefresh(cart2);
5361
+ await this.updateCart(cart, { dispatchExternalEvents: true });
5362
+ } finally {
5363
+ this._isUpdatingShipAid = false;
5364
+ this._enableCheckoutButtons();
5073
5365
  }
5074
- const updatePayload = {
5075
- updates: {
5076
- [protectionCartItem.variant_id]: 0,
5077
- [protectionVariant.id]: 1
5078
- }
5079
- };
5080
- const cart = await this._fetch.post(
5081
- "/cart/update.js",
5082
- updatePayload
5083
- );
5084
- await this._handleRefresh(cart);
5085
5366
  }
5086
5367
  render() {
5087
5368
  useOnce(this, async () => {
@@ -5851,74 +6132,8 @@ class ShipAidCheckoutPlus extends s$1 {
5851
6132
  }
5852
6133
  async _refreshShopifyCartUI() {
5853
6134
  try {
5854
- const sectionIds = ["cart-drawer", "cart-icon-bubble", "main-cart-items"];
5855
- const url = `/?sections=${encodeURIComponent(sectionIds.join(","))}`;
5856
- const res = await fetch(url, { credentials: "same-origin" });
5857
- if (!res.ok) return;
5858
- const htmlById = await res.json().catch(() => null);
5859
- if (!htmlById || typeof htmlById !== "object") return;
5860
6135
  this._isReplacingCartSections = true;
5861
- for (const id of Object.keys(htmlById)) {
5862
- const html2 = htmlById[id];
5863
- if (typeof html2 !== "string") continue;
5864
- const tmp = document.createElement("div");
5865
- tmp.innerHTML = html2;
5866
- if (id === "cart-drawer") {
5867
- try {
5868
- const newDrawer = tmp.querySelector("cart-drawer");
5869
- const oldDrawer = document.querySelector("cart-drawer");
5870
- if (newDrawer && oldDrawer) {
5871
- const shouldBeEmpty = newDrawer.classList.contains("is-empty");
5872
- if (shouldBeEmpty) oldDrawer.classList.add("is-empty");
5873
- else oldDrawer.classList.remove("is-empty");
5874
- }
5875
- } catch {
5876
- }
5877
- try {
5878
- const newDrawerItems = tmp.querySelector("cart-drawer-items");
5879
- const oldDrawerItems = document.querySelector("cart-drawer cart-drawer-items, .cart-drawer cart-drawer-items");
5880
- if (newDrawerItems && oldDrawerItems) {
5881
- oldDrawerItems.outerHTML = newDrawerItems.outerHTML;
5882
- }
5883
- } catch {
5884
- }
5885
- const newItems = tmp.querySelector("#CartDrawer-CartItems");
5886
- const oldItems = document.getElementById("CartDrawer-CartItems");
5887
- if (newItems && oldItems) {
5888
- oldItems.outerHTML = newItems.outerHTML;
5889
- }
5890
- const newFooter = tmp.querySelector(".cart-drawer__footer");
5891
- const oldFooter = document.querySelector(".cart-drawer .cart-drawer__footer");
5892
- if (newFooter && oldFooter) {
5893
- oldFooter.outerHTML = newFooter.outerHTML;
5894
- }
5895
- const newCtas = tmp.querySelector(".cart__ctas");
5896
- const oldCtas = document.querySelector(".cart-drawer .cart__ctas");
5897
- if (newCtas && oldCtas) {
5898
- oldCtas.outerHTML = newCtas.outerHTML;
5899
- }
5900
- continue;
5901
- }
5902
- if (id === "main-cart-items") {
5903
- const newMainItems = tmp.querySelector("#main-cart-items");
5904
- const oldMainItems = document.getElementById("main-cart-items");
5905
- if (newMainItems && oldMainItems) {
5906
- oldMainItems.outerHTML = newMainItems.outerHTML;
5907
- } else {
5908
- const section = document.getElementById("shopify-section-main-cart-items");
5909
- if (section) section.outerHTML = html2;
5910
- }
5911
- continue;
5912
- }
5913
- if (id === "cart-icon-bubble") {
5914
- const newBubble = tmp.querySelector("#cart-icon-bubble, [data-cart-count-bubble]");
5915
- const oldBubble = document.querySelector("#cart-icon-bubble, [data-cart-count-bubble]");
5916
- if (newBubble && oldBubble) {
5917
- oldBubble.replaceWith(newBubble);
5918
- }
5919
- continue;
5920
- }
5921
- }
6136
+ await refreshShopifyCartUI();
5922
6137
  } catch {
5923
6138
  } finally {
5924
6139
  this._isReplacingCartSections = false;
@@ -6004,7 +6219,6 @@ class ShipAidCheckoutPlus extends s$1 {
6004
6219
  console.warn("[ShipAid] Plan is not active, skipping widget mount");
6005
6220
  return false;
6006
6221
  }
6007
- if (!this._hasCheckoutButtons) return false;
6008
6222
  return ((_a = this._store.checkoutPlus) == null ? void 0 : _a.enabled) === true;
6009
6223
  }
6010
6224
  updated(changed) {
@@ -6201,6 +6415,7 @@ class ShipAidCheckoutPlus extends s$1 {
6201
6415
  this._hasCheckoutButtons = false;
6202
6416
  return;
6203
6417
  }
6418
+ this._hasCheckoutButtons = true;
6204
6419
  if (this.isClone) {
6205
6420
  const target = matches[0];
6206
6421
  this._originalCheckoutButton = target;
@@ -6599,7 +6814,7 @@ class ShipAidCheckoutPlus extends s$1 {
6599
6814
  }
6600
6815
  render() {
6601
6816
  var _a, _b, _c, _d, _e;
6602
- if (!this._hasFinishedSetup || !this._shouldShowWidget()) return A;
6817
+ if (!this._hasFinishedSetup || !this._shouldShowWidget() || !this._hasCheckoutButtons) return A;
6603
6818
  const isGreen = Boolean((_b = (_a = this._store) == null ? void 0 : _a.greenProtection) == null ? void 0 : _b.enabled);
6604
6819
  const isFreeGiftEnabled = Boolean((_d = (_c = this._store) == null ? void 0 : _c.freeGifts) == null ? void 0 : _d.enabled);
6605
6820
  const iconImg = isGreen || isFreeGiftEnabled ? ShipAidImpactIcon : ShipAidBlueIcon;