if (MODULES.WATCHDOG) {
   import('@assets/js/wpj.watchdog.js').then(wpjWatchdog => {
      wpjWatchdog.initWatchdog();
   });
}

const WpjTabs = require('@assets/js/wpj.tabs.js');
new WpjTabs('[data-tabs="shoes-size"]');

const $collection_color_label = $('[data-product-color-selected]');
$(document).on('mouseenter', '.product-collection-item', function() {
   $collection_color_label.html($(this).data('product-color'));
}).on('mouseleave', '.product-collection-item', function() {
   $collection_color_label.html($collection_color_label.data('product-color-selected'));
});

const $sizeConvertionWrapper = $('[data-variation-size-convertion-wrapper]');
$(document).on('click', '[data-variation-size-convertion-trigger]', function(e) {
   $sizeConvertionWrapper.toggleClass('active');
}).on('click', '.product-variation-size-convertion', function(e) {
   $sizeConvertionWrapper.toggleClass('active');
}).on('click', function(e) {
   if (!$(e.target).closest('[data-variation-size-convertion-wrapper]').length && $sizeConvertionWrapper.hasClass('active')) {
      $sizeConvertionWrapper.removeClass('active');
   }
});

// sticky info
const $window = $(window),
   $header = $('.header'),
   $product_sticky_info = $('.product-sticky-info-wrapper'),
   product_sticky_start = $('.product-bottom').offset().top;
if ($window.width() > 991) {
   $window.scroll(function() {
      const scrollTop = $window.scrollTop();

      if (product_sticky_start) {
         if (scrollTop > product_sticky_start && !$product_sticky_info.hasClass('is-stuck')) {
            $product_sticky_info.addClass('is-stuck');
            $header.addClass('active-sticky-info');
         } else if (scrollTop <= product_sticky_start) {
            $product_sticky_info.removeClass('is-stuck');
            $header.removeClass('active-sticky-info');
         }
      }
   });
}

wpj.domUtils.initOpeners();

$('[data-more-reviews]').on('opened', function() {
   var $this = $(this),
      $btn = $('[data-opener="[data-more-reviews]"]'),
      $more = $btn.data('text-show'),
      $less = $btn.data('text-hide');

   if (!$this.is('.active')) {
      $btn.text($more);
   } else {
      $btn.text($less);
   }
});

if ($('#related-products').length) {
   import('@assets/js/wpj.carousel.js').then(wpjCarousel => {
      if ($('#related-products').length) {
         ($('#related-products')).wpjCarousel({
            itemsOnPage: 1.8,
            gutter: 10,
            controlsText: ['<i class="fc icons_arrow-left"></i>', '<i class="fc icons_arrow-right"></i>'],
            responsive: {
               630: {
                  itemsOnPage: 2.5,
                  gutter: 10
               },
               992: {
                  itemsOnPage: 3.5,
                  gutter: 10
               },
               1420: {
                  itemsOnPage: 5,
                  gutter: 10
               }
            }
         });
      }
   });
}

$('body').on('click', '.btn-insert .non-variation', function(e) {
   const $anchor = $('[data-variations="variations-ajax"]');
   const offset = $anchor.offset();

   $('html, body')
      .stop()
      .animate(
         {
            scrollTop: offset.top - $(window).height() + 205
         },
         1000
      );

   return false
});
