/**
 * Advance WP Slider — Swiper bootstrap.
 */
(function () {
	'use strict';

	function parseSettings(el) {
		try {
			return JSON.parse(el.getAttribute('data-awp-settings') || '{}');
		} catch (e) {
			return {};
		}
	}

	function initSlider(root) {
		if (!root || root.dataset.awpReady === '1' || typeof Swiper === 'undefined') {
			return;
		}

		var swiperEl = root.querySelector('.awp-swiper');
		if (!swiperEl) {
			return;
		}

		var cfg = parseSettings(root);
		var options = {
			speed: cfg.speed || 600,
			loop: !!cfg.loop,
			effect: cfg.effect || 'slide',
			grabCursor: !!cfg.grabCursor,
			centeredSlides: !!cfg.centeredSlides,
			spaceBetween: cfg.spaceBetween || 0,
			slidesPerView: cfg.slidesPerView || 1,
			watchOverflow: true,
			observer: true,
			observeParents: true,
			breakpoints: {
				0: { slidesPerView: cfg.slidesPerViewMobile || 1 },
				768: { slidesPerView: cfg.slidesPerViewTablet || 1 },
				1025: { slidesPerView: cfg.slidesPerView || 1 }
			}
		};

		if (cfg.effect === 'fade' || cfg.effect === 'cube' || cfg.effect === 'flip') {
			options.slidesPerView = 1;
			options.breakpoints = {
				0: { slidesPerView: 1 },
				768: { slidesPerView: 1 },
				1025: { slidesPerView: 1 }
			};
		}

		if (cfg.effect === 'coverflow') {
			options.coverflowEffect = {
				rotate: 30,
				stretch: 0,
				depth: 100,
				modifier: 1,
				slideShadows: true
			};
		}

		if (cfg.keyboard) {
			options.keyboard = { enabled: true };
		}

		if (cfg.autoplay) {
			options.autoplay = {
				delay: cfg.autoplayDelay || 4000,
				disableOnInteraction: false,
				pauseOnMouseEnter: !!cfg.pauseOnHover
			};
		}

		if (cfg.showPagination) {
			var paginationEl = root.querySelector('.awp-pagination');
			if (paginationEl) {
				options.pagination = {
					el: paginationEl,
					clickable: true,
					type: cfg.paginationType || 'bullets'
				};
			}
		}

		if (cfg.showArrows) {
			var nextEl = root.querySelector('.awp-nav--next');
			var prevEl = root.querySelector('.awp-nav--prev');
			if (nextEl && prevEl) {
				options.navigation = {
					nextEl: nextEl,
					prevEl: prevEl
				};
			}
		}

		new Swiper(swiperEl, options);
		root.dataset.awpReady = '1';
	}

	function initAll(context) {
		var scope = context && context.querySelectorAll ? context : document;
		scope.querySelectorAll('.awp-slider').forEach(initSlider);
	}

	if (document.readyState === 'loading') {
		document.addEventListener('DOMContentLoaded', function () {
			initAll(document);
		});
	} else {
		initAll(document);
	}

	function bindElementor() {
		if (!window.elementorFrontend || !elementorFrontend.hooks) {
			return;
		}
		elementorFrontend.hooks.addAction('frontend/element_ready/awp_slider.default', function ($scope) {
			var el = $scope && $scope[0] ? $scope[0] : $scope;
			initAll(el);
		});
	}

	if (window.jQuery) {
		jQuery(window).on('elementor/frontend/init', bindElementor);
	} else {
		window.addEventListener('elementor/frontend/init', bindElementor);
	}

	document.addEventListener('awp-slider:init', function (e) {
		initAll(e.detail || document);
	});

	window.AWPSliderInit = initAll;
})();
