import { useState, useRef, useEffect } from "react"; import styles from "./ImageCarousel.module.scss"; const ImageCarousel = ({ images, altText, showThumbnails = false, isDetailView = false, // Prop to differentiate between card and detail view }) => { const [currentIndex, setCurrentIndex] = useState(0); const touchStartX = useRef(0); const touchEndX = useRef(0); const carouselRef = useRef(null); // Check if there are multiple images const hasMultipleImages = Array.isArray(images) && images.length > 1; // Get current image URL const currentImage = hasMultipleImages && images[currentIndex] ? images[currentIndex].images_400x400 : images[0]?.images_400x400 || ""; // Auto-slide functionality - every 9 seconds useEffect(() => { if (!hasMultipleImages) return; const interval = setInterval(() => { setCurrentIndex((prev) => (prev === images.length - 1 ? 0 : prev + 1)); }, 9000); return () => clearInterval(interval); }, [hasMultipleImages, images]); // Navigate to previous image const handlePrev = (e) => { if (e) e.stopPropagation(); if (!hasMultipleImages) return; setCurrentIndex((prev) => (prev === 0 ? images.length - 1 : prev - 1)); }; // Navigate to next image const handleNext = (e) => { if (e) e.stopPropagation(); if (!hasMultipleImages) return; setCurrentIndex((prev) => (prev === images.length - 1 ? 0 : prev + 1)); }; // Handle thumbnail click const handleThumbnailClick = (index, e) => { if (e) e.stopPropagation(); setCurrentIndex(index); }; // Touch event handlers const handleTouchStart = (e) => { touchStartX.current = e.touches[0].clientX; }; const handleTouchMove = (e) => { touchEndX.current = e.touches[0].clientX; }; const handleTouchEnd = () => { if (!hasMultipleImages) return; const touchDiff = touchStartX.current - touchEndX.current; // Swipe threshold - only respond to intentional swipes if (Math.abs(touchDiff) > 50) { if (touchDiff > 0) { // Swipe left -> Next image handleNext(); } else { // Swipe right -> Previous image handlePrev(); } } }; // Apply transition effect using CSS useEffect(() => { if (carouselRef.current) { carouselRef.current.classList.add(styles.transitioning); const timer = setTimeout(() => { if (carouselRef.current) { carouselRef.current.classList.remove(styles.transitioning); } }, 900); // Match this timing with CSS transition duration return () => clearTimeout(timer); } }, [currentIndex]); // If there's only one image, just show it - applying different classes based on view if (!hasMultipleImages) { return (