(function() {
	'use strict';

	document.addEventListener('DOMContentLoaded', function() {
		const slider = document.querySelector('.front-top-slider');
		if (!slider) return;

		const intervalMs = parseInt(slider.dataset.interval, 10) * 1000;
		const durationMs = parseInt(slider.dataset.duration, 10);// * 1000;
		const slides = Array.from(slider.querySelectorAll('.front-top-slide'));

		if (slides.length < 2) return;

		let currentIndex = 0;
		let isAnimating = false;

		// Configuração inicial via JS (pode ser movida pro CSS se preferir)
		slides.forEach((slide, i) => {
			slide.style.transition = `opacity ${durationMs}ms ease-in-out`;
			slide.style.opacity = i === 0 ? '1' : '0';
			slide.style.zIndex = i === 0 ? '2' : '1';
		});

		// Respeita preferência de acessibilidade
		const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
		if (prefersReducedMotion) return;

		function advanceSlide() {
			if (isAnimating) return;
			isAnimating = true;

			const current = slides[currentIndex];
			currentIndex = (currentIndex + 1) % slides.length;
			const next = slides[currentIndex];

			// Lógica de z-index + fade solicitada
			next.style.zIndex = 2;      // Sobe a próxima para o topo
			current.style.opacity = '0'; // Fade out da atual
			next.style.opacity = '1';    // Fade in da próxima

			// Libera o lock após a transição
			setTimeout(() => {
				current.style.zIndex = 1; // Volta a atual para baixo
				isAnimating = false;
			}, durationMs);
		}

		setInterval(advanceSlide, intervalMs);
	});
})();