{% 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 raw_price = selected_variant.price
  assign raw_compare_at_price = selected_variant.compare_at_price

  if product_resource == blank and request.design_mode
    assign raw_price = 1999
  endif

  comment
    Generate fallback compare-at price between 12% and 35%
  endcomment

  assign sh_discount_percent = selected_variant.id | modulo: 24 | plus: 12
  assign sh_discount_base = 100 | minus: sh_discount_percent
  assign sh_generated_compare_at_price = raw_price | times: 100 | divided_by: sh_discount_base

  if raw_compare_at_price != blank and raw_compare_at_price > raw_price
    assign final_compare_at_price = raw_compare_at_price
  else
    assign final_compare_at_price = sh_generated_compare_at_price
  endif

  assign sh_saving = final_compare_at_price | minus: raw_price
  assign sh_final_discount = sh_saving | times: 100 | divided_by: final_compare_at_price

  if product.handle == closest.product.handle and settings.currency_code_enabled_product_pages
    assign price = raw_price | money_with_currency
    assign compare_at_price = final_compare_at_price | money_with_currency
    assign saving_price = sh_saving | money_with_currency

  elsif product.handle != closest.product.handle and settings.currency_code_enabled_product_cards
    assign price = raw_price | money_with_currency
    assign compare_at_price = final_compare_at_price | money_with_currency
    assign saving_price = sh_saving | money_with_currency

  else
    assign price = raw_price | money
    assign compare_at_price = final_compare_at_price | money
    assign saving_price = sh_saving | money
  endif
-%}

<div ref="priceContainer" class="sh-price-container">

  {% if show_sale_price_first == false and final_compare_at_price > raw_price %}
    <s class="compare-at-price sh-compare-price">
      {{ compare_at_price }}
    </s>
  {% endif %}

  <span class="price">
    {{ price | default: '&nbsp;' }}
  </span>

  {% if show_sale_price_first == true and final_compare_at_price > raw_price %}
    <s class="compare-at-price sh-compare-price">
      {{ compare_at_price }}
    </s>
  {% endif %}

  {% if final_compare_at_price > raw_price %}
    <span class="sh-discount-tag">
      {{ sh_final_discount }}% OFF
    </span>

    <div class="sh-save-line">
      You save {{ saving_price }} today
    </div>
  {% endif %}

  {%- if selected_variant.unit_price and show_unit_price %}
    {%- liquid
      if product.handle == closest.product.handle and settings.currency_code_enabled_product_pages
        assign unit_price = selected_variant.unit_price | money_with_currency
      elsif product.handle != closest.product.handle and settings.currency_code_enabled_product_cards
        assign unit_price = selected_variant.unit_price | money_with_currency
      else
        assign unit_price = selected_variant.unit_price | money
      endif
    -%}

    {% render 'unit-price',
      variant: selected_variant,
      price: unit_price
    %}
  {%- endif -%}

</div>