/** Shopify CDN: Minification failed

Line 16:0 Unexpected "{"
Line 16:1 Expected identifier but found "%"
Line 17:52 Expected identifier but found whitespace
Line 19:3 Expected identifier but found "%"
Line 20:0 Unexpected "{"
Line 20:1 Expected identifier but found "%"
Line 21:0 Unexpected "{"
Line 21:1 Unexpected "{"
Line 21:3 Expected identifier but found "'cart-free-shipping.css'"
Line 23:0 Unexpected "{"
... and 4 more hidden warnings

**/
{% comment %}
  Renders a message about free shipping in the cart.
  Usage:
  {% render 'cart-free-shipping' %}
{% endcomment %}
{{ 'cart-free-shipping.css' | asset_url | stylesheet_tag }}

{% if settings.show_cart_free_shipping %}
<div
  id="xo-free-shipping-bar"
  data-threshold="5000"
  data-notice="{{ 'general.cart.free_shipping_notice_2' | t }}"
  data-progress="{{ 'general.cart.free_shipping_progress' | t }}"
  style="font-size: 1.4rem; font-weight: 500; opacity: 0.65; padding-bottom: 1rem;"
>
  <div id="xo-free-shipping-text"></div>
  <div class="xo-cart-mini-footer__separator-pd pb-0">
    <div class="xo-cart-mini-footer__separator"></div>
  </div>
</div>

<script>
(function() {
  var THRESHOLD = 5000;
  var _lastFetched = -1;
  var _pollTimer = null;

  function formatMoney(cents) {
    if (cents <= 0) return '$0.00';
    return '$' + (cents / 100).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  }

  function renderText(totalPriceCents) {
    var bar = document.getElementById('xo-free-shipping-bar');
    var textEl = document.getElementById('xo-free-shipping-text');
    if (!bar || !textEl) return;
    var pct = (totalPriceCents / THRESHOLD) * 100;
    var remaining = Math.max(THRESHOLD - totalPriceCents, 0);
    if (pct >= 100) {
      textEl.innerHTML = bar.dataset.notice;
    } else {
      textEl.innerHTML = bar.dataset.progress.replace(/\{\{[^}]*\}\}/g, formatMoney(remaining));
    }
  }

  function fetchAndRender() {
    fetch('/cart.js')
      .then(function(r) { return r.json(); })
      .then(function(cart) {
        if (cart.total_price !== _lastFetched) {
          _lastFetched = cart.total_price;
          renderText(cart.total_price);
        }
      })
      .catch(function() {});
  }

  function stopPolling() {
    if (_pollTimer) { clearInterval(_pollTimer); _pollTimer = null; }
  }

  function startPolling() {
    stopPolling();
    var elapsed = 0;
    _pollTimer = setInterval(function() {
      fetchAndRender();
      elapsed += 300;
      if (elapsed >= 4000) stopPolling();
    }, 300);
  }

  // Always initialize from live cart — no Liquid values in JS
  fetchAndRender();

  document.addEventListener('click', function(e) {
    var t = e.target;
    if (
      t.closest('[data-action="increment"]') ||
      t.closest('[data-action="decrement"]') ||
      t.closest('[class*="quantity"]') ||
      t.closest('[class*="remove"]') ||
      t.closest('[class*="delete"]') ||
      t.closest('cart-remove-button')
    ) { startPolling(); }
  });

  document.addEventListener('submit', function(e) {
    if (e.target && e.target.action && e.target.action.indexOf('/cart') !== -1) {
      startPolling();
    }
  });

  document.addEventListener('cart:updated', function(e) {
    var cart = ((e.detail || {}).cart) || {};
    if (cart.total_price !== undefined) {
      _lastFetched = cart.total_price;
      renderText(cart.total_price);
      stopPolling();
    }
  });

})();
</script>
{% endif %}