/** Shopify CDN: Minification failed

Line 16:0 Unexpected "{"
Line 16:1 Expected identifier but found "%"
Line 16:33 Unexpected "-"
Line 16:52 Expected identifier but found "%"
Line 18:0 Unexpected "<"
Line 19:7 Unexpected "{"
Line 19:16 Expected ":"
Line 19:34 Unexpected "<"
Line 22:5 Expected identifier but found "%"
Line 23:6 Unexpected "{"
... and 51 more hidden warnings

**/
{% comment %} Quantity Discounts - Dynamic Pricing {% endcomment %}

<div class="quantity-discounts" id="quantity-discounts">
  <h3>{{ section.settings.title }}</h3>

  <div class="qd-options">
    {% for block in section.blocks %}
      {% assign qty = block.settings.quantity %}
      {% assign discount = block.settings.discount_percent %}
      <div class="qd-option {% if forloop.first %}selected{% endif %}"
           data-qty="{{ qty }}"
           data-discount="{{ discount }}"
           {{ block.shopify_attributes }}>
        <div class="qd-label">
          {% if block.settings.label != blank %}
            <span class="qd-tag">{{ block.settings.label }}</span>
          {% endif %}
          <strong>{{ qty }}x {{ product.title }}</strong>
        </div>
        <div class="qd-price">
          <span class="qd-total" data-base-price="{{ product.selected_or_first_available_variant.price }}">
            {%- assign total = product.selected_or_first_available_variant.price | times: qty -%}
            {%- if discount > 0 -%}
              {%- assign discounted = total | times: discount | divided_by: 100 -%}
              {%- assign final = total | minus: discounted -%}
              <s>{{ total | money }}</s>
              <strong>{{ final | money }}</strong>
              <span class="qd-saving">({{ discount }}% OFF)</span>
            {%- else -%}
              <strong>{{ total | money }}</strong>
            {%- endif -%}
          </span>
        </div>
      </div>
    {% endfor %}
  </div>
</div>

<script>
  document.addEventListener('DOMContentLoaded', function () {
    const options = document.querySelectorAll('.qd-option');

    // Actualizar precios si cambia la variante
    document.addEventListener('variant:changed', function (e) {
      const price = e.detail.variant.price;
      updatePrices(price);
    });

    options.forEach(function (option) {
      option.addEventListener('click', function () {
        options.forEach(o => o.classList.remove('selected'));
        this.classList.add('selected');

        const qty = parseInt(this.dataset.qty);
        const discount = parseFloat(this.dataset.discount) || 0;
        const basePrice = parseInt(this.querySelector('.qd-total').dataset.basePrice);

        // Actualizar cantidad en el formulario del producto
        const qtyInput = document.querySelector('input[name="quantity"]');
        if (qtyInput) qtyInput.value = qty;

        // Aplicar descuento si corresponde
        updateCartPrice(basePrice, qty, discount);
      });
    });

    function updatePrices(newBasePrice) {
      options.forEach(function (option) {
        const qty = parseInt(option.dataset.qty);
        const discount = parseFloat(option.dataset.discount) || 0;
        const totalEl = option.querySelector('.qd-total');
        if (totalEl) totalEl.dataset.basePrice = newBasePrice;
        updateCartPrice(newBasePrice, qty, discount);
      });
    }

    function updateCartPrice(basePrice, qty, discount) {
      // Formatear en moneda local
      const formatter = new Intl.NumberFormat('es-AR', {
        style: 'currency', currency: '{{ shop.currency }}'
      });
      const total = basePrice * qty;
      const final = discount > 0 ? total * (1 - discount / 100) : total;
      console.log(`Qty: ${qty} | Total: ${formatter.format(final / 100)}`);
    }
  });
</script>

{% schema %}
{
  "name": "Quantity Discounts",
  "settings": [
    {
      "type": "text",
      "id": "title",
      "label": "Título",
      "default": "Elegí tu pack"
    }
  ],
  "blocks": [
    {
      "type": "option",
      "name": "Opción de cantidad",
      "settings": [
        {
          "type": "number",
          "id": "quantity",
          "label": "Cantidad",
          "default": 1
        },
        {
          "type": "number",
          "id": "discount_percent",
          "label": "Descuento (%)",
          "default": 0
        },
        {
          "type": "text",
          "id": "label",
          "label": "Etiqueta (ej: MÁS POPULAR)",
          "default": ""
        }
      ]
    }
  ],
  "presets": [
    {
      "name": "Quantity Discounts",
      "blocks": [
        { "type": "option", "settings": { "quantity": 1, "discount_percent": 0, "label": "" } },
        { "type": "option", "settings": { "quantity": 2, "discount_percent": 10, "label": "MÁS POPULAR" } },
        { "type": "option", "settings": { "quantity": 3, "discount_percent": 15, "label": "MEJOR VALOR" } }
      ]
    }
  ]
}
{% endschema %}
