{% liquid
  assign max_items = section.settings.max_products
%}

<div class="section-background color-{{ section.settings.color_scheme }}"></div>
<div
  data-testid="product-list"
  class="
    section
    section--{{ section.settings.section_width }}
    color-{{ section.settings.color_scheme }}
    section-resource-list
    spacing-style
    gap-style
  "
  style="
    {% render 'spacing-style', settings: section.settings %}
    {% render 'gap-style', value: section.settings.gap %}
  "
>
<div
  id="{{section.settings.id}}"
  class="
  {{section.settings.css}}"
>
  <div class="section-resource-list__header">
    {%- content_for 'block', type: '_product-list-content', id: 'static-header' -%}
  </div>

  {% capture list_items %}
    {% if section.settings.collection != blank and shop.products_count != 0 %}
      {% paginate section.settings.collection.products by max_items %}
        {% for product in section.settings.collection.products limit: max_items %}
          <div
            class="resource-list__item"
          >
            {% # theme-check-disable UniqueStaticBlockId %}
              {% content_for 'block', type: 'custom_product-card', id: 'static-product-card', closest.product: product %}
            {% # theme-check-enable UniqueStaticBlockId %}
          </div>

          {% unless forloop.last %}
            <!--@list/split-->
          {% endunless %}
        {% endfor %}
      {% endpaginate %}
    {% else %}
      {% for i in (1..max_items) %}
        <div
          class="resource-list__item"
        >
          {% # theme-check-disable UniqueStaticBlockId %}
            {% content_for 'block', type: 'custom_product-card', id: 'static-product-card', closest.product: null %}
          {% # theme-check-enable UniqueStaticBlockId %}
        </div>

        {% unless forloop.last %}
          <!--@list/split-->
        {% endunless %}
      {% endfor %}
    {% endif %}
  {% endcapture %}

  {% liquid
    # Create an array from the list items to be used for different layout types
    assign list_items_array = list_items | strip | split: '<!--@list/split-->'
  %}

  {% render 'resource-list',
    list_items: list_items,
    list_items_array: list_items_array,
    settings: section.settings,
    slide_count: max_items,
    content_type: 'products',
    test_id: 'resource-list-grid'
  %}

  <div
    class="section-resource-list__content"
    style="--horizontal-alignment: {{ section.settings.horizontal_alignment }};"
  >
    {%- content_for 'blocks' -%}
  </div>
</div>
</div>

{% schema %}
{
  "name": "t:names.product_list",
  "class": "ui-test-product-list",
  "blocks": [
    {
      "type": "@theme"
    },
    {
      "type": "@app"
    },
    {
      "type": "_divider"
    }
  ],
  "disabled_on": {
    "groups": ["header", "footer"]
  },
  "settings": [
    {
      "type": "text",
      "id": "id",
      "label": "Ajout d'un ID"
    },
    {
      "type": "text",
      "id": "css",
      "label": "Ajout de class CSS"
    },
    {
      "type": "collection",
      "id": "collection",
      "label": "t:settings.collection"
    },
    {
      "type": "select",
      "id": "layout_type",
      "label": "t:settings.layout_type",
      "options": [
        {
          "value": "grid",
          "label": "t:options.grid"
        },
        {
          "value": "carousel",
          "label": "t:options.carousel"
        },
        {
          "value": "editorial",
          "label": "t:options.editorial"
        }
      ],
      "default": "grid"
    },
    {
      "type": "checkbox",
      "id": "carousel_on_mobile",
      "label": "t:settings.carousel_on_mobile",
      "default": false,
      "visible_if": "{{ section.settings.layout_type != 'carousel' }}"
    },
    {
      "type": "range",
      "id": "max_products",
      "label": "t:settings.product_count",
      "min": 1,
      "max": 16,
      "step": 1,
      "default": 4
    },
    {
      "type": "range",
      "id": "columns",
      "label": "t:settings.columns",
      "min": 1,
      "max": 8,
      "step": 1,
      "default": 4,
      "visible_if": "{{ section.settings.layout_type != 'editorial' }}"
    },
    {
      "type": "select",
      "id": "mobile_columns",
      "label": "t:settings.mobile_columns",
      "options": [
        {
          "value": "1",
          "label": "t:options.one_number"
        },
        {
          "value": "2",
          "label": "t:options.two_number"
        }
      ],
      "default": "2",
      "visible_if": "{{ section.settings.layout_type == 'grid' and section.settings.carousel_on_mobile == false }}"
    },
    {
      "type": "select",
      "id": "mobile_card_size",
      "label": "t:settings.mobile_columns",
      "options": [
        {
          "value": "60cqw",
          "label": "t:options.one_number"
        },
        {
          "value": "44cqw",
          "label": "t:options.two_number"
        }
      ],
      "default": "60cqw",
      "visible_if": "{{ section.settings.layout_type == 'carousel' or section.settings.carousel_on_mobile == true }}"
    },
    {
      "type": "range",
      "id": "columns_gap",
      "label": "t:settings.horizontal_gap",
      "min": 0,
      "max": 100,
      "step": 1,
      "unit": "px",
      "default": 8,
      "visible_if": "{{ section.settings.layout_type == 'grid' or section.settings.layout_type == 'carousel' }}"
    },
    {
      "type": "range",
      "id": "rows_gap",
      "label": "t:settings.vertical_gap",
      "min": 0,
      "max": 100,
      "step": 1,
      "unit": "px",
      "default": 8,
      "visible_if": "{{ section.settings.layout_type == 'grid'}}"
    },
    {
      "type": "header",
      "content": "t:content.carousel_navigation",
      "visible_if": "{{ section.settings.layout_type == 'carousel' or section.settings.carousel_on_mobile == true }}"
    },
    {
      "type": "select",
      "id": "icons_style",
      "label": "t:settings.icon",
      "options": [
        {
          "value": "arrow",
          "label": "t:options.arrows"
        },
        {
          "value": "chevron",
          "label": "t:options.chevrons"
        },
        {
          "value": "arrows_large",
          "label": "t:options.arrows_large"
        },
        {
          "value": "chevron_large",
          "label": "t:options.chevron_large"
        },
        {
          "value": "none",
          "label": "t:options.none"
        }
      ],
      "default": "arrow",
      "visible_if": "{{ section.settings.layout_type == 'carousel' or section.settings.carousel_on_mobile == true }}"
    },
    {
      "type": "select",
      "id": "icons_shape",
      "label": "t:settings.icon_background",
      "options": [
        {
          "value": "none",
          "label": "t:options.none"
        },
        {
          "value": "circle",
          "label": "t:options.circle"
        },
        {
          "value": "square",
          "label": "t:options.square"
        }
      ],
      "default": "none",
      "visible_if": "{{ section.settings.icons_style != 'none' and section.settings.layout_type == 'carousel' or section.settings.carousel_on_mobile == true }}"
    },
    {
      "type": "header",
      "content": "t:content.section_layout"
    },
    {
      "type": "select",
      "id": "section_width",
      "label": "t:settings.width",
      "options": [
        {
          "value": "page-width",
          "label": "t:options.page"
        },
        {
          "value": "full-width",
          "label": "t:options.full"
        }
      ],
      "default": "page-width"
    },
    {
      "type": "select",
      "id": "horizontal_alignment",
      "label": "t:settings.alignment",
      "options": [
        {
          "value": "flex-start",
          "label": "t:options.left"
        },
        {
          "value": "center",
          "label": "t:options.center"
        },
        {
          "value": "flex-end",
          "label": "t:options.right"
        }
      ],
      "default": "flex-start"
    },
    {
      "type": "range",
      "id": "gap",
      "label": "t:settings.gap",
      "min": 0,
      "max": 100,
      "step": 1,
      "unit": "px",
      "default": 12
    },
    {
      "type": "color_scheme",
      "id": "color_scheme",
      "label": "t:settings.color_scheme",
      "default": "scheme-1"
    },
    {
      "type": "header",
      "content": "t:content.padding"
    },
    {
      "type": "range",
      "id": "padding-block-start",
      "label": "t:settings.top",
      "min": 0,
      "max": 100,
      "step": 1,
      "unit": "px",
      "default": 0
    },
    {
      "type": "range",
      "id": "padding-block-end",
      "label": "t:settings.bottom",
      "min": 0,
      "max": 100,
      "step": 1,
      "unit": "px",
      "default": 0
    }
  ],
  "presets": [
    {
      "name": "t:names.products_grid",
      "category": "Web Artisan - Custom section"
    }
  ]
}
{% endschema %}
