{%- doc -%}
  This snippet is used to render a product card.
  It is used in the product block,featured product block, and the product card block.

  @param {product} product_resource - The product to render
  @param {boolean} [show_unit_price] - Whether to show the unit price
  @param {boolean} [show_sale_price_first] - Whether to show the sale price first
{%- enddoc -%}

{%- liquid
  assign show_unit_price = show_unit_price | default: false
  assign show_sale_price_first = show_sale_price_first | default: false
  assign selected_variant = product_resource.selected_or_first_available_variant
  assign price = selected_variant.price
  assign compare_at_price = selected_variant.compare_at_price

  assign show_compare_price = false
  if compare_at_price > price
    assign show_compare_price = true
  endif

  if product_resource == blank
    assign price = 1999
  endif

  # Check if variant has volume pricing
  assign has_volume_pricing = null
  assign price_min = null
  assign price_max = null

  # For product cards (collection/search pages), calculate min/max across all variants
  # For product pages, only use the selected variant's pricing
  assign is_product_card = false
  if template.name != 'product'
    assign is_product_card = true
  endif

  if is_product_card
    assign price_min = product_resource.price_min
    assign price_max = product_resource.price_max
    assign has_volume_pricing = product_resource.quantity_price_breaks_configured? | default: false
  else
    # Product page: only use selected variant's pricing
    if selected_variant.quantity_price_breaks.size > 0
      assign has_volume_pricing = true
      assign price_min = selected_variant.quantity_price_breaks.last.price
      assign price_max = selected_variant.price
    endif
  endif

  # Determine if currency code should be shown
  assign use_currency = false
  if product.handle == product_resource.handle
    if settings.currency_code_enabled_product_pages
      assign use_currency = true
    endif
  elsif settings.currency_code_enabled_product_cards
    assign use_currency = true
  endif

  # Format all prices
  if use_currency
    assign price = price | money_with_currency
    assign compare_at_price = compare_at_price | money_with_currency
    assign price_min = price_min | money_with_currency
    assign price_max = price_max | money_with_currency
  else
    assign price = price | money
    assign compare_at_price = compare_at_price | money
    assign price_min = price_min | money
    assign price_max = price_max | money
  endif
-%}

<div ref="priceContainer">
  {% if has_volume_pricing %}
    {% comment %} Volume pricing display {% endcomment %}
    {% if show_compare_price %}
      <span class="price-item__group">
        <span class="visually-hidden">{{ 'content.price_regular' | t }}&nbsp;</span>
        <span class="compare-at-price">{{- compare_at_price -}}</span>
      </span>
    {% endif %}
    <span class="price-item__group">
      <span class="visually-hidden">{{ 'content.price_range' | t }}&nbsp;</span>
      <span class="price">{{ price_min }} - {{ price_max }}</span>
    </span>
  {% else %}
    {% comment %} Standard pricing display {% endcomment %}
    <div
      class="
        price__regular
        {% if show_compare_price %}
          price__hidden
        {% endif %}
      "
    >
      <span class="price">{{ price | default: '&nbsp;' }}</span>
    </div>
    <div
      class="
        price__sale
        {% unless show_compare_price %}
          price__hidden
        {% endunless %}
      "
    >
      {% if show_sale_price_first %}
        <span class="price-item__group">
          <span class="visually-hidden">{{ 'content.price_sale' | t }}&nbsp;</span>
          <span class="price-item--sale price">{{ price | default: '&nbsp;' }}</span>
        </span>
        <span class="price-item__group">
          <span class="visually-hidden">{{ 'content.price_regular' | t }}&nbsp;</span>
          <span class="price-item--regular compare-at-price">{{- compare_at_price -}}</span>
        </span>
      {% else %}
        <span class="price-item__group">
          <span class="visually-hidden">{{ 'content.price_regular' | t }}&nbsp;</span>
          <span class="price-item--regular compare-at-price">{{- compare_at_price -}}</span>
        </span>
        <span class="price-item__group">
          <span class="visually-hidden">{{ 'content.price_sale' | t }}&nbsp;</span>
          <span class="price-item--sale price">{{ price | default: '&nbsp;' }}</span>
        </span>
      {% endif %}
    </div>
  {% endif %}
  {%- if selected_variant.unit_price and show_unit_price %}
    {%- liquid
      if use_currency
        assign unit_price = selected_variant.unit_price | money_with_currency
      else
        assign unit_price = selected_variant.unit_price | money
      endif
    -%}
    {% render 'unit-price', price: unit_price, measurement: selected_variant.unit_price_measurement %}
  {%- endif -%}
</div>
{%- if has_volume_pricing -%}
  <small
    ref="volumePricingNote"
    class="volume-pricing-note"
  >
    {{- 'content.volume_pricing_available' | t -}}
  </small>
{%- endif -%}
