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

import { tns } from '@shop/engine/node_modules/tiny-slider/src/tiny-slider';
import { initBuyFormJsShop } from '@assets/js/wpj.product.add-to-cart.js';
import { initInstafeed } from '@assets/js/wpj.instafeed-init.js';

initInstafeed("cyklospeciality", 4);

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

if (MODULES.PRODUCTS_VARIATIONS) {
   const { WpjProductVariations } = require('@assets/js/wpj.product.variations.class.js');
   const wpjVariations = new WpjProductVariations($('[data-variations="variations-ajax"]'));
   initBuyFormJsShop(true, wpjVariations);
} else {
   initBuyFormJsShop(false);
}


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

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

cartboxSidebar();

if ($('#footer-reviews').length) {
   const footer_reviews = tns({
      container: '#footer-reviews',
      items: 1,
      slideBy: 1,
      nav: false,
      controls: true,
      controlsText: ['', '']
   });
}

// fixed header
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);
}

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

$(document).on('change', '[data-variations-quickbuy] input', function() {
   let $this = $(this);
   let price = $this.data('price');
   let priceCommon = $this.data('price-common');
   let wrapper = $this.closest('[data-quickbuy-wrapper]');
   let img = $this.data('var-image')

   wrapper.find('[data-quickbuy-price]').text(price);

   if (img) {
      wrapper.find('[data-quickbuy-image]').attr('src', img);
   }

   if (priceCommon) {
      wrapper.find('[data-quickbuy-price-common]').text(priceCommon);
   }

}).on('click', '[data-quickbuy-submit]', function() {
   $(this).closest('.focus-content').find('[data-focus="close"]').click();
}).on('submit', '[data-buy-form]', function() {
   if ($('.main-cart').length) {
      $('#cart').cart('reloadCart');
   }
});
