require('@assets[shared]/js/wpj.app.js');
require('@assets[shared]/js/utils/photo-rotator.js');

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

const { WpjProductVariations } = require('@assets/js/wpj.product.variations.class.js');
const wpjVariations = new WpjProductVariations($('[data-variations="variations-ajax"]'));

initBuyFormJsShop(true, wpjVariations);

$('[data-sections="header"] > .has-submenu').on({
   mouseenter: function() {
      var self = this;
      var header = $('.header');
      if ($(self).is(':hover')) {
         $(self).addClass('hover');
         header.addClass('hover');
         $('.overlay-blur').addClass('active');
      }
   },
   mouseleave: function() {
      var self = this;
      var header = $('.header');
      if (!$(self).is(':hover')) {
         $(self).removeClass('hover');
         header.removeClass('hover');
         $('.overlay-blur').removeClass('active');
      }
   }
});

import { cartboxSidebar } from '@assets[shared]/js/wpj.cartbox.js';

cartboxSidebar();

// home products carousel
if ($('#home-products').length) {
   import('@assets/js/wpj.carousel.js').then(wpjCarousel => {
      if ($('#home-products').length) {
         $('#home-products').wpjCarousel({
            itemsOnPage: 1.5,
            gutter: 10,
            controlsText: ['<span class="fc icons_caret-left"></span>', '<span class="fc icons_caret-right"></span>'],
            responsive: {
               544: {
                  itemsOnPage: 2.5
               },
               768: {
                  itemsOnPage: 3.5
               },
               1260: {
                  itemsOnPage: 4
               }
            }
         });
      }
   });
}

if ($('#cart-products').length) {
   import('@assets/js/wpj.carousel.js').then(wpjCarousel => {
      if ($('#cart-products').length) {
         ($('#cart-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.5,
                  gutter: 10
               },
               544: {
                  itemsOnPage: 2.1,
                  gutter: 10
               },
               630: {
                  itemsOnPage: 2.5,
                  gutter: 10
               },
               992: {
                  itemsOnPage: 3.5,
                  gutter: 10
               },
               1420: {
                  itemsOnPage: 5,
                  gutter: 10
               }
            }
         });
      }
   });
}

if ($(window).width() > 767) {
   const headerElm = document.querySelector('.header');
   const headerStickyHelperElm = document.querySelector('.header-sticky-helper');

   const handler = (entries) => {
      if (!entries[0].isIntersecting) {
         headerElm.classList.add('is-stuck');
      } else {
         headerElm.classList.remove('is-stuck');
      }
   };

   if (headerStickyHelperElm) {
      const observer = new window.IntersectionObserver(handler);
      observer.observe(headerStickyHelperElm);
   }
}

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

const $body = $('body');
$body.on('click update', '[data-custom-opener]', function(e) {
   let $this = $(e.currentTarget),
      $target = $($this.data('custom-opener')),
      checked = $this.is('.active');

   if (e.type != 'update')
      checked = !checked;

   $target[checked ? 'slideDown' : 'slideUp']()[checked ? 'addClass' : 'removeClass']('active').trigger('opened');
   $this[checked ? 'addClass' : 'removeClass']('active');

   return !$this.is('a');
}).find('[data-custom-opener]').trigger('update');

// sellers
$body.on('keyup change', '[data-seller-search-input]', function(e) {
   const identifier = $(this).data('seller-search-input');
   console.log(identifier);
   const $sellers_list = $('[data-seller-list="' + identifier + '"]');
   const $seller_not_found = $('[data-seller-not-found="' + identifier + '"]');
   const pattern = $(this).val();

   wpj.domUtils.resetTimer('sellers-search', 200, function() {
      if (pattern) {
         $sellers_list.addClass('searching');
         $sellers_list.find('.seller-row:not(:contains(' + pattern + '))').hide();

         const $foundedSellers = $sellers_list.find('.seller-row:contains(' + pattern + ')').css('display', 'flex');

         if ($foundedSellers.length) {
            $seller_not_found.hide();
         } else {
            $seller_not_found.show();
         }

      } else {
         $sellers_list.removeClass('searching');
         $sellers_list.find('.seller-row').css('display', 'flex');
         $seller_not_found.hide();
      }
   });
});

// footer rwd menu
$('.footer-menu .title-default').on('click', function(e) {
   if ($(window).width() < 768) {
      $(this).closest('.column').toggleClass('is-open');
      $(this).next().slideToggle('is-open');
   }
});

$(document).on('click', '.header-burger', function() {
   $(this).toggleClass('open-menu');
});

const body = $('body');
const header = $('.header');
const search = $('.header-search-inner');


$('[data-toggle="search"]').click(function() {
   if (search.is('.active')) {
      header.addClass('search-active');
      body.addClass('focus-opened');
      $('.header-search-outer').addClass('active');
   } else {
      header.removeClass('search-active');
      body.removeClass('focus-opened');
      $('.header-search-outer').removeClass('active');
   }
});

$('[data-search-input]').on('change input', function() {
   const autocomplete = $('.autocomplete');

   if ((autocomplete).is('.opened')) {
      $('[data-toggle="search"]').addClass('autocomplete-opened');
   } else {
      $('[data-toggle="search"]').removeClass('autocomplete-opened');
   }
});

$body.on('click', '.w-coupon a', function(e) {
   const $this = $(this);
   const text = $(this).html();
   const $element = $this.closest('.w-coupon').find('h4');
   let $temp = $('<input>');

   $('body').append($temp);
   $temp.val($($element).html()).select();
   document.execCommand('copy');
   $temp.remove();

   $this.html('<i class="fc icons_check"></i>');
   wpj.domUtils.resetTimer('removeCouponCheckmark', 1000, function() {
      $this.html(text);
   });

   return false;
});

$('#login').login({
   addBodyPadding: false
});
