<link rel="preload" href="{{ 'component-card.css' | asset_url }}" as="style">
<link rel="stylesheet" href="{{ 'component-card.css' | asset_url }}">
<link rel="preload" href="{{ 'component-price.css' | asset_url }}" as="style">
<link rel="stylesheet" href="{{ 'component-price.css' | asset_url }}">
<link rel="preload" href="{{ 'section-product-recommendations.css' | asset_url }}" as="style">
<link rel="stylesheet" href="{{ 'section-product-recommendations.css' | asset_url }}">

<noscript>{{ 'component-card.css' | asset_url | stylesheet_tag }}</noscript>
<noscript>{{ 'component-price.css' | asset_url | stylesheet_tag }}</noscript>
<noscript>{{ 'section-product-recommendations.css' | asset_url | stylesheet_tag }}</noscript>
{%- if section.settings.quick_add == 'standard' -%}
  {{ 'quick-add.css' | asset_url | stylesheet_tag }}
  <script src="{{ 'quick-add.js' | asset_url }}" defer="defer"></script>
{%- endif -%}
{%- style -%}
  .section-{{ section.id }}-padding {
    padding-top: {{ section.settings.padding_top | times: 0.75 | round: 0 }}px;
    padding-bottom: {{ section.settings.padding_bottom | times: 0.75 | round: 0 }}px;
  }

  @media screen and (min-width: 750px) {
    .section-{{ section.id }}-padding {
      padding-top: {{ section.settings.padding_top }}px;
      padding-bottom: {{ section.settings.padding_bottom }}px;
    }
  }
{%- endstyle -%}

<div class="color-{{ section.settings.color_scheme }} gradient recommendations-section-{{ section.id }}">
  <product-recommendations class="product-recommendations page-width section-{{ section.id }}-padding isolate" data-url="{{ routes.product_recommendations_url }}?section_id={{ section.id }}&product_id={{ product.id }}&limit={{ section.settings.products_to_show }}" data-desktop-slides="{{ section.settings.columns_desktop | plus: 0.5 }}" data-mobile-slides="{{ section.settings.columns_mobile | plus: 0.3 }}">
    {% if recommendations.performed and recommendations.products_count > 0 %}
      <div class="recommendations-container">
      <h2 class="product-recommendations__heading {{ section.settings.heading_size }} {{ section.settings.text_align }}">{{ section.settings.heading | escape }}</h2>
      <div class="swiper product-recommendations-slider">
        <ul class="swiper-wrapper" role="list">
          {% for recommendation in recommendations.products %}
            <li class="swiper-slide">
              {% render 'card-product',
                card_product: recommendation,
                media_aspect_ratio: section.settings.image_ratio,
                show_secondary_image: section.settings.show_secondary_image,
                show_vendor: section.settings.show_vendor,
                show_rating: section.settings.show_rating,
                quick_add: section.settings.quick_add,
                section_id: section.id,
              %}
            </li>
          {% endfor %}
        </ul>
      </div>
      <div class="swiper-footer recommendations-arrow">
        <div class="swiper-button-prev"><svg fill="" width="24px" height="24px" viewBox="0 0 24 24" id="left-arrow" data-name="Flat Color" xmlns="http://www.w3.org/2000/svg" class="icon flat-color" stroke=""><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"><path id="primary" d="M21,11H5.41l1.3-1.29A1,1,0,0,0,5.29,8.29l-3,3a1,1,0,0,0,0,1.42l3,3a1,1,0,0,0,1.42,0,1,1,0,0,0,0-1.42L5.41,13H21a1,1,0,0,0,0-2Z" style="fill: ;"></path></g></svg></div>
        <div class="swiper-button-next"><svg fill="" width="24px" height="24px" viewBox="0 0 24 24" id="left-arrow" data-name="Flat Color" xmlns="http://www.w3.org/2000/svg" class="icon flat-color rotated" stroke="" transform="rotate(180)"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"><path id="primary" d="M21,11H5.41l1.3-1.29A1,1,0,0,0,5.29,8.29l-3,3a1,1,0,0,0,0,1.42l3,3a1,1,0,0,0,1.42,0,1,1,0,0,0,0-1.42L5.41,13H21a1,1,0,0,0,0-2Z" style="fill: ;"></path></g></svg></div>
        <div class="swiper-pagination"></div>
      </div>
    </div>
    {% endif %}
  </product-recommendations>
</div>

{% javascript %}
  (() => {
    class ProductRecommendations extends HTMLElement {
      constructor() {
        super();
        
        const handleIntersection = (entries, observer) => {
          if (!entries[0].isIntersecting) return;
          observer.unobserve(this);
          
          fetch(this.dataset.url)
            .then(response => response.text())
            .then(text => {
              const html = new DOMParser().parseFromString(text, 'text/html');
              const recommendations = html.querySelector('product-recommendations');

              if (recommendations && recommendations.innerHTML.trim()) {
                this.innerHTML = recommendations.innerHTML;
                this.classList.add('product-recommendations--loaded');
              }
              
              if (html.querySelector('.swiper-slide')) {
                this.initializeSwiper();
              }
            })
            .catch(console.error);
        };

        new IntersectionObserver(handleIntersection, { rootMargin: '0px 0px 200px 0px' }).observe(this);
      }

      initializeSwiper() {
        new Swiper('.product-recommendations-slider', {
          slidesPerView: this.dataset.mobileSlides,
          spaceBetween: 15,
          slidesPerGroup: 1,
          autoHeight: false,
          observer: true,
          observeParents: true,
          parallax: true,
          navigation: {
            nextEl: '.recommendations-arrow .swiper-button-next',
            prevEl: '.recommendations-arrow .swiper-button-prev',
          },
          pagination: {
            el: '.recommendations-arrow .swiper-pagination',
            clickable: true
          },
          breakpoints: {
            991: { slidesPerView: this.dataset.desktopSlides, spaceBetween: 20 },
            600: { slidesPerView: 2.5, spaceBetween: 20 }
          }
        });
      }
    }
    customElements.define('product-recommendations', ProductRecommendations);
  })();
{% endjavascript %}

{% schema %}
{
  "name": "t:sections.product-recommendations.name",
  "tag": "section",
  "class": "section",
  "settings": [
    {
      "type": "paragraph",
      "content": "t:sections.product-recommendations.settings.paragraph__1.content"
    },
    {
      "type": "text",
      "id": "heading",
      "default": "You may also like",
      "label": "t:sections.product-recommendations.settings.heading.label"
    },
    {
      "type": "select",
      "id": "heading_size",
      "options": [
        {
          "value": "h1",
          "label": "t:sections.all.heading_size_1.options__1.label"
        },
        {
          "value": "h2",
          "label": "t:sections.all.heading_size_1.options__2.label"
        },
        {
          "value": "h3",
          "label": "t:sections.all.heading_size_1.options__3.label"
        },
        {
          "value": "h4",
          "label": "t:sections.all.heading_size_1.options__4.label"
        },
        {
          "value": "h5",
          "label": "t:sections.all.heading_size_1.options__5.label"
        },
        {
          "value": "blockquote-font",
          "label": "t:sections.all.heading_size_1.options__6.label"
        },
        {
          "value": "paragraph-font",
          "label": "t:sections.all.heading_size_1.options__7.label"
        }
      ],
      "default": "h3",
      "label": "t:sections.all.heading_size.label"
    },
    {
      "type": "select",
      "id": "text_align",
      "label": "Heading align",
      "options": [
        {
          "value": "left",
          "label": "Left"
        },
        {
          "value": "center",
          "label": "Center"
        },
        {
          "value": "right",
          "label": "Right"
        }
      ],
      "default": "center"
    },
    {
      "type": "range",
      "id": "products_to_show",
      "min": 2,
      "max": 10,
      "step": 1,
      "default": 4,
      "label": "t:sections.product-recommendations.settings.products_to_show.label"
    },
    {
      "type": "range",
      "id": "columns_desktop",
      "min": 1,
      "max": 5,
      "step": 1,
      "default": 4,
      "label": "t:sections.product-recommendations.settings.columns_desktop.label"
    },
    {
      "type": "select",
      "id": "color_scheme",
      "options": [
        {
          "value": "accent-1",
          "label": "t:sections.all.colors.accent_1.label"
        },
        {
          "value": "accent-2",
          "label": "t:sections.all.colors.accent_2.label"
        },
        {
          "value": "background-1",
          "label": "t:sections.all.colors.background_1.label"
        },
        {
          "value": "background-2",
          "label": "t:sections.all.colors.background_2.label"
        },
        {
          "value": "inverse",
          "label": "t:sections.all.colors.inverse.label"
        }
      ],
      "default": "background-1",
      "label": "t:sections.all.colors.label",
      "info": "t:sections.all.colors.has_cards_info"
    },
    {
      "type": "header",
      "content": "t:sections.product-recommendations.settings.header__2.content"
    },
    {
      "type": "select",
      "id": "image_ratio",
      "options": [
        {
          "value": "adapt",
          "label": "t:sections.product-recommendations.settings.image_ratio.options__1.label"
        },
        {
          "value": "portrait",
          "label": "t:sections.product-recommendations.settings.image_ratio.options__2.label"
        },
        {
          "value": "square",
          "label": "t:sections.product-recommendations.settings.image_ratio.options__3.label"
        }
      ],
      "default": "adapt",
      "label": "t:sections.product-recommendations.settings.image_ratio.label"
    },
    {
      "type": "checkbox",
      "id": "show_secondary_image",
      "default": false,
      "label": "t:sections.product-recommendations.settings.show_secondary_image.label"
    },
    {
      "type": "checkbox",
      "id": "show_vendor",
      "default": false,
      "label": "t:sections.product-recommendations.settings.show_vendor.label"
    },
    {
      "type": "checkbox",
      "id": "show_rating",
      "default": false,
      "label": "t:sections.product-recommendations.settings.show_rating.label",
      "info": "t:sections.product-recommendations.settings.show_rating.info"
    },
          {
        "type": "select",
        "id": "quick_add",
        "default": "none",
        "label": "t:sections.main-collection-product-grid.settings.quick_add.label",
        "options": [
          {
            "value": "none",
            "label": "t:sections.main-collection-product-grid.settings.quick_add.options.option_1"
          },
          {
            "value": "standard",
            "label": "t:sections.main-collection-product-grid.settings.quick_add.options.option_2"
          }
        ]
      },
    {
      "type": "header",
      "content": "t:sections.product-recommendations.settings.header_mobile.content"
    },
    {
      "type": "select",
      "id": "columns_mobile",
      "default": "2",
      "label": "t:sections.product-recommendations.settings.columns_mobile.label",
      "options": [
        {
          "value": "1",
          "label": "t:sections.product-recommendations.settings.columns_mobile.options__1.label"
        },
        {
          "value": "2",
          "label": "t:sections.product-recommendations.settings.columns_mobile.options__2.label"
        }
      ]
    },
    {
      "type": "header",
      "content": "t:sections.all.padding.section_padding_heading"
    },
    {
      "type": "range",
      "id": "padding_top",
      "min": 0,
      "max": 100,
      "step": 4,
      "unit": "px",
      "label": "t:sections.all.padding.padding_top",
      "default": 36
    },
    {
      "type": "range",
      "id": "padding_bottom",
      "min": 0,
      "max": 100,
      "step": 4,
      "unit": "px",
      "label": "t:sections.all.padding.padding_bottom",
      "default": 36
    }
  ]
}
{% endschema %}
