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

wpj.domUtils.initOpeners();

if ($('#related-products').length) {
   import('@assets/js/wpj.carousel.js').then(wpjCarousel => {
      $('#related-products').wpjCarousel({
         itemsOnPage: 1.2,
         gutter: 10,
         controlsText: ['<i class="fc icons_arrow-left"></i>', '<i class="fc icons_arrow-right"></i>'],
         responsive: {
            420: {
               itemsOnPage: 1.8,
               gutter: 10
            },
            544: {
               itemsOnPage: 2.2,
               gutter: 10
            },
            768: {
               itemsOnPage: 2.6,
               gutter: 10
            },
            992: {
               itemsOnPage: 3,
               gutter: 10
            },
            1260: {
               itemsOnPage: 4,
               gutter: 10
            },
            1420: {
               itemsOnPage: 5,
               gutter: 10
            }
         }
      });
   });
}

$('body').on('click', '[data-product-tabs-target]', function(e) {
   if ($(window).width() < 768) {
      e.preventDefault();
      const $this = $(this);
      const $target = $('[data-product-tabs-content="' + $this.data('product-tabs-target') + '"]');

      $this.toggleClass('opened');
      $target.slideToggle();
   }
}).on('click', '[data-toggle-quantity-discounts]', function (e) {
   e.preventDefault();

   const $this = $(this);
   const $target = $('[data-toggle-quantity-discounts-target]');

   $this.toggleClass('is-active');
   $target.toggleClass('is-active');
});

// sticky info
const $window = $(window),
   $header = $('.header'),
   $product_sticky_info = $('.product-sticky-info-wrapper'),
   product_sticky_start = $('.product-sticky-info-helper').offset().top - $header.height();
$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');
      }
   }
});

const element = document.querySelector('#delivery-country');
if (element) {
   element.addEventListener('change', () => {
      window.location.href = '/zeme/' + element.value + "#deliveries";
   });
}

var hash = wpj.domUtils.getHash();
if (hash === 'deliveries') {
   $('[data-focus-opener="product-delivery"]').trigger('click');
   window.location.hash = '';
}

$('.review-stars-wrapper').on({
   mouseenter: function() {
      const self = this;
      setTimeout(function() {
         if ($(self).is(':hover')) {
            $(self).addClass('hover');
         }
      }, 400);
   },
   mouseleave: function() {
      const self = this;
      setTimeout(function() {
         if (!$(self).is(':hover')) {
            $(self).removeClass('hover');
         }
      }, 200);
   }
});

if (MODULES.REVIEWS) {
   $('[data-more-reviews]').on('opened', function() {
      const $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);
      }
   });

   $('body').on('click', '[data-more-reviews-button]', function(e) {
      e.preventDefault();
      const $btn = $(this),
         more = $btn.data('text-show'),
         less = $btn.data('text-hide');

      $.ajax({
         type: 'GET',
         url: $btn.attr('href'),
         success: function(data) {
            $('[data-more-reviews-container]').html(data);
            $btn.removeAttr('data-more-reviews-button').text(less);
         }
      });
   });

   // Pokud se přihlašuju v rámci přidávání recenze, přesměruj po přihlášení na #write-review
   $('body').on('click', '.review-form .cart-signin', function() {
      $('#login').login('setNextUrl', window.location + '#write-review');
   });

   // Otevři dialog na přidávání recenzí, pokud mám #write-review
   if (wpj.domUtils.getHash() === 'write-review') {
      $('[data-wpj-focus-ajax][data-wpj-focus*="/reviews/"]').trigger('click');
   }
}

if (MODULES.QUANTITY_DISCOUNT) {
   const discountMap = new Map();

   $('body').on('change', '[data-buy-form] [data-pieces-b2b] ', function(e) {
      if (!wpj.quantityDiscounts) {
         return;
      }

      const dataBuyForm = $(this).closest('tr');
      const variationId = dataBuyForm.find('td.q-discount').data('variation');

      const targetDiscount = wpj.quantityDiscounts[variationId].find((discount) => {
         return discount.piecesFrom <= this.value && (isNaN(discount.piecesTo) || this.value <= discount.piecesTo);
      });

      const memoDiscount = discountMap.get(variationId);

      if (!targetDiscount || (memoDiscount && memoDiscount.index === targetDiscount.index)) {
         return;
      }

      dataBuyForm.find('.b2b-price > strong').text(targetDiscount.priceNoVat);
      dataBuyForm.find('td.q-discount .quantity-variation-discounts-item.active').removeClass('active');

      const targetDiscountIndex = targetDiscount.index === (wpj.quantityDiscounts[variationId].length - 1) ? targetDiscount.index : (targetDiscount.index + 1);

      dataBuyForm.find('td.q-discount .quantity-variation-discounts-item[data-discount-index=' + targetDiscountIndex + ']').addClass('active');

      discountMap.set(variationId, targetDiscount);
   });
}
