/** Shopify CDN: Minification failed

Line 17:0 Unexpected "("
Line 20:2 Comments in CSS use "/* ... */" instead of "//"
Line 29:27 Comments in CSS use "/* ... */" instead of "//"
Line 40:6 Comments in CSS use "/* ... */" instead of "//"
Line 53:6 Comments in CSS use "/* ... */" instead of "//"
Line 56:6 Comments in CSS use "/* ... */" instead of "//"
Line 69:10 Comments in CSS use "/* ... */" instead of "//"
Line 101:10 Comments in CSS use "/* ... */" instead of "//"
Line 107:10 Comments in CSS use "/* ... */" instead of "//"
Line 115:10 Comments in CSS use "/* ... */" instead of "//"
... and 11 more hidden warnings

**/
/* Quick View (fixed) */
(function ($, window, document) {
  'use strict';

  // Load Fancybox v2 then init
  $(function () {
    if (!$.fancybox) {
      $.getScript('https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.js')
        .done(function () {
          quickViewInit();
        })
        .fail(function () {
          console.warn('[QuickView] Fancybox failed to load.');
          quickViewInit(); // still allow basic flow without fancybox
        });
    } else {
      quickViewInit();
    }
  });

  function quickViewInit() {
    $(document).on('click', '.quick-view', function (e) {
      e.preventDefault();

      // Ensure container exists
      var $qv = $('#quick-view');
      if ($qv.length === 0) {
        $('body').append('<div id="quick-view" style="display:none"></div>');
        $qv = $('#quick-view');
      }

      var product_handle = $(this).data('handle');
      if (!product_handle) {
        console.warn('[QuickView] Missing data-handle on .quick-view trigger');
        return;
      }

      // Clear any previous content/classes
      $qv.removeAttr('class').empty().addClass(product_handle);

      // Fetch product JSON
      $.getJSON('/products/' + product_handle + '.js')
        .done(function (product) {
          var home = 'https://wesolveskin.com';
          var title = product.title || '';
          var type = product.type || '';
          var desc = product.description || '';
          var featured_image = product.featured_image || '';
          var images = Array.isArray(product.images) ? product.images : [];
          var options = Array.isArray(product.options) ? product.options : [];
          var variants = Array.isArray(product.variants) ? product.variants : [];
          var url = '/products/' + product_handle;

          // Build modal content
          var content = '' +
            '<div class="qv-wrap">' +
              '<div class="qv-left">' +
                '<div class="product-images-for"></div>' +
                '<div class="product-images-nav"></div>' +
              '</div>' +
              '<div class="qv-right">' +
                '<h3 class="qv-product-title"></h3>' +
                '<div class="qv-product-type"></div>' +
                '<div class="qv-product-description rte"></div>' +
                '<div class="qv-product-prices">' +
                  '<span class="qv-product-price"></span> ' +
                  '<span class="qv-product-original-price" style="display:none;text-decoration:line-through;margin-left:8px;"></span>' +
                '</div>' +
                '<div class="qv-product-options"></div>' +
                '<div class="qv-actions" style="margin-top:12px;">' +
                  '<input type="number" class="qv-quantity" min="1" value="1" style="width:70px;"> ' +
                  '<button class="qv-add-button btn">Add to Cart</button> ' +
                  '<a class="qv-add-to-cart view-product btn btn--secondary" href="' + url + '">View product</a>' +
                '</div>' +
                '<div class="qv-share" style="margin-top:12px;">' +
                  '<span class="fb-share"><a target="_blank" rel="noopener">Share on Facebook</a></span> · ' +
                  '<span class="twitter-share"><a target="_blank" rel="noopener">Share on X</a></span> · ' +
                  '<span class="pinterest-share"><a target="_blank" rel="noopener">Pin it</a></span>' +
                '</div>' +
                '<div class="qv-add-to-cart-response" style="margin-top:10px;"></div>' +
              '</div>' +
            '</div>';

          $qv.html(content);

          // Populate basics
          $('.qv-product-title').text(title);
          $('.qv-product-type').text(type);
          $('.qv-product-description').html(desc);
          $('.view-product').attr('href', url);

          // Share links
          var shareUrl = encodeURIComponent(home + url);
          var shareTitle = encodeURIComponent(title);
          var shareImg = encodeURIComponent(featured_image || (images[0] || ''));
          $('.fb-share a').attr('href', 'https://www.facebook.com/sharer.php?u=' + shareUrl);
          $('.twitter-share a').attr('href', 'https://twitter.com/share?text=' + shareTitle + '&url=' + shareUrl);
          $('.pinterest-share a').attr('href', 'https://pinterest.com/pin/create/button/?url=' + shareUrl + '&media=' + shareImg);

          // Build image sliders
          var $for = $('.product-images-for');
          var $nav = $('.product-images-nav');

          if (images.length === 0 && featured_image) {
            images = [featured_image];
          }

          images.forEach(function (image) {
            var img = String(image || '');
            if (!img) return;
            var embed = '<div><img class="lazy2" src="' + img
              .replace('.jpg', '_800x.jpg').replace('.png', '_800x.png') + '"></div>';
            $for.append(embed);
            $nav.append(embed);
          });

          // Init slick if available (won’t crash if absent)
          if ($.fn.slick) {
            $for.slick({
              dots: false,
              arrows: false,
              respondTo: 'min',
              fade: true,
              cssEase: 'linear',
              useTransform: false,
              asNavFor: '.product-images-nav'
            }).css('opacity', '1');

            $nav.slick({
              slidesToShow: 3,
              slidesToScroll: 1,
              asNavFor: '.product-images-for',
              dots: false,
              arrows: true,
              respondTo: 'min',
              focusOnSelect: true,
              useTransform: false
            }).css('opacity', '1');
          }

          // Build options
          var firstAvailable = null;
          $('.qv-product-options').empty();
          options.forEach(function (opt, i) {
            var optName = (opt && opt.name) ? String(opt.name) : ('Option ' + (i + 1));
            var optKey = optName.toLowerCase().replace(/\s+/g, '-');
            var selHtml = '' +
              '<div class="option-selection-' + optKey + '">' +
                '<span class="option">' + optName + '</span>' +
                '<select class="option-' + i + ' option ' + optKey + '"></select>' +
              '</div>';
            $('.qv-product-options').append(selHtml);

            (opt.values || []).forEach(function (value) {
              $('.option-' + i).append('<option value="' + value + '">' + value + '</option>');
            });
          });

          // Set price/availability to the first in-stock variant
          variants.some(function (v) {
            if (!v) return false;
            var inStock = (v.inventory_management == null) || (v.inventory_quantity > 0);
            if (inStock) {
              var price = (v.price / 100).toFixed(2);
              var original = v.compare_at_price ? (v.compare_at_price / 100).toFixed(2) : 0;

              $('.qv-product-price').text('$' + price);
              if (original > 0) {
                $('.qv-product-original-price').text('$' + original).show();
              } else {
                $('.qv-product-original-price').hide();
              }

              // Preselect options
              if (v.option1) $('.option-0').val(v.option1);
              if (v.option2) $('.option-1').val(v.option2);
              if (v.option3) $('.option-2').val(v.option3);

              firstAvailable = v;
              return true;
            }
            return false;
          });

          // If nothing in stock, reflect that
          if (!firstAvailable) {
            $('.qv-add-button').prop('disabled', true).text('Sold Out');
            $('.qv-add-to-cart').hide();
            $('.qv-product-price').text('Sold Out').show();
          } else {
            $('.qv-add-button').prop('disabled', false).text('Add to Cart');
          }

          // Variant change handler — keep product_handle in closure
          $(document).off('change.qv', '#quick-view select');
          $(document).on('change.qv', '#quick-view select', function () {
            var selectedOptions = [];
            $('#quick-view select').each(function () {
              selectedOptions.push($(this).val());
            });
            var selectedTitle = selectedOptions.join(' / ');

            $.getJSON('/products/' + product_handle + '.js').done(function (p) {
              (p.variants || []).forEach(function (v) {
                if (v.title === selectedTitle) {
                  var price = (v.price / 100).toFixed(2);
                  var original = v.compare_at_price ? (v.compare_at_price / 100).toFixed(2) : 0;
                  $('.qv-product-price').text('$' + price);
                  if (original > 0) {
                    $('.qv-product-original-price').text('$' + original).show();
                  } else {
                    $('.qv-product-original-price').hide();
                  }

                  var inStock = (v.inventory_management == null) || (v.inventory_quantity > 0);
                  if (inStock) {
                    $('.qv-add-button').prop('disabled', false).text('Add to Cart');
                  } else {
                    $('.qv-add-button').prop('disabled', true).text('Sold Out');
                  }
                }
              });
            });
          });

          // Fancybox open (if present); otherwise, just show inline
          if ($.fancybox) {
            $.fancybox({
              href: '#quick-view',
              maxWidth: 1040,
              maxHeight: 700,
              fitToView: true,
              width: '70%',
              height: '70%',
              autoSize: false,
              closeClick: false,
              openEffect: 'none',
              closeEffect: 'none',
              beforeLoad: function () {
                // keep for future hooks; product_handle is accessible via closure
                $('.fancybox-wrap').css('overflow', 'hidden !important');
              },
              afterShow: function () {
                // ensure slick recalculates
                if ($.fn.slick) {
                  $for.slick('setPosition');
                  $nav.slick('setPosition');
                }
              },
              afterClose: function () {
                $qv.removeClass().empty();
                $(document).off('change.qv', '#quick-view select');
              }
            });
          } else {
            // Fallback: show inline if Fancybox unavailable
            $qv.show();
          }
        })
        .fail(function () {
          console.warn('[QuickView] Could not fetch product JSON for handle:', product_handle);
        });
    });

    // Reposition slick on resize if modal visible
    $(window).on('resize.quickview', function () {
      var $qv = $('#quick-view');
      if ($qv.is(':visible') && $.fn.slick) {
        $('.product-images-for').slick('setPosition');
        $('.product-images-nav').slick('setPosition');
      }
    });
  }
})(jQuery, window, document);
