{% comment %}
  Renders shipping bar
  Accepts:
  - class {String} Additional classes
  Usage:
  {%- render 'shipping-bar', class: 'additional_classes' -%}
{% endcomment %}

{%- liquid
  assign currency = cart.currency.iso_code | default: localization.country.currency.iso_code

  # Pick threshold (major units) from new settings
  assign threshold_major = settings.shipping_notification_threshold_cad | default: settings.shipping_notification_threshold

  case currency
    when 'GBP'
      assign threshold_major = settings.shipping_notification_threshold_gbp | default: 100
    when 'USD'
      assign threshold_major = settings.shipping_notification_threshold_usd | default: 150
    when 'CAD'
      assign threshold_major = settings.shipping_notification_threshold_cad | default: settings.shipping_notification_threshold
    else
      assign threshold_major = settings.shipping_notification_threshold_cad | default: settings.shipping_notification_threshold
  endcase

  # Coerce to number + convert to minor units (cents/pence)
  assign threshold = threshold_major | plus: 0 | times: 100

  # Guard: if threshold is blank/0, don't show bar (prevents "always qualified")
  if threshold <= 0
    assign threshold_percentage = 0
  else
    assign threshold_percentage = cart.total_price | times: 100.0 | divided_by: threshold
  endif

  if threshold_percentage < 0
    assign threshold_percentage = 0
  elsif threshold_percentage > 100
    assign threshold_percentage = 100
  endif

  assign remaining = threshold | minus: cart.total_price
-%}

{%- style -%}
  {%- if cart == empty -%}
    .shipping-bar {
      visibility: hidden;
    }
  {%- endif -%}
{%- endstyle -%}

{%- if settings.enable_shipping_notification and threshold > 0 -%}
  <shipping-bar
    data-cart-total="{{ cart.total_price }}"
    data-shipping-threshold="{{ threshold }}"
    data-progress="calc({{ threshold_percentage }}% + 2px)"
    class="shipping-bar progress-bar {{ class }}"
    aria-live="polite"
  >
    <div class="progress-bar__top">
      {%- render 'icon', icon_name: 'theme-package-check' -%}
      <p class="progress-bar__text">
        {%- if cart.total_price >= threshold -%}
          Congrats! You're qualified for free shipping!
        {%- else -%}
          You're {{ remaining | money }} away from free shipping.
        {%- endif -%}
      </p>
    </div>

    <div class="progress-bar__progress">
      <span data-progress-line class="progress-bar__progress-inner" style="width: {{ threshold_percentage }}%;"></span>
    </div>
  </shipping-bar>
{%- endif -%}