import { initBuyForm, initBuyFormJsShop } from '@assets/js/wpj.product.add-to-cart.js';

if (MODULES.PRODUCTS_VARIATIONS) {
   const { WpjProductVariations } = require('@assets/js/wpj.product.variations.class.js');
   let $allVariations = $('[data-variations="variations"]');

   if (!$allVariations.length) {
      $allVariations = $('[data-variations="variations-ajax"]');
   }

   const variations = new WpjProductVariations($allVariations);

   if (MODULES.JS_SHOP) {
      initBuyFormJsShop(true, variations);
   } else {
      initBuyForm(true, variations);
   }
} else {
   if (MODULES.JS_SHOP) {
      initBuyFormJsShop(false);
   } else {
      initBuyForm(false);
   }
}

// todo if module
/*import ProductSets from '@assets[shared]/js/wpj.productSets.js';
if ($('[data-sets="wrapper"]').length) {
   new ProductSets();
}*/

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

      if (MODULES.B2B) {
         wpjWatchdog.ajaxWatchdog();
      }
   });
}

if (MODULES.COMMENTS) {
   const WpjComments = require('@assets/js/wpj.comments.js');
   new WpjComments();
}

if (typeof initProductGallerySlider === 'undefined' && $('#product-gallery').length) {
   import('@assets/js/wpj.product.helpers.js').then(productHelpers => {
      if (typeof initProductGallerySlider === 'undefined') {
         productHelpers.initProductGallerySlider();
      }
   });
}

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();
      console.log('dd');
      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');
   }
}
