import { useCallback, useEffect, useRef, useState } from 'react' import type { PropertyPhoto } from '../../types/property' import VideoPlayer from './VideoPlayer' interface PhotoCarouselProps { photos: PropertyPhoto[] videoUrl?: string | null } function ChevronLeft() { return ( ) } function ChevronRight() { return ( ) } function NoPhotoPlaceholder() { return (
Sem fotos disponíveis
) } export default function PhotoCarousel({ photos, videoUrl }: PhotoCarouselProps) { const [activeIndex, setActiveIndex] = useState(0) const touchStartX = useRef(null) const containerRef = useRef(null) const hasVideo = Boolean(videoUrl) const totalSlides = photos.length + (hasVideo ? 1 : 0) const prev = useCallback(() => { setActiveIndex((i) => (i === 0 ? totalSlides - 1 : i - 1)) }, [totalSlides]) const next = useCallback(() => { setActiveIndex((i) => (i === totalSlides - 1 ? 0 : i + 1)) }, [totalSlides]) useEffect(() => { const el = containerRef.current if (!el) return const handleKey = (e: KeyboardEvent) => { if (e.key === 'ArrowLeft') prev() else if (e.key === 'ArrowRight') next() } el.addEventListener('keydown', handleKey) return () => el.removeEventListener('keydown', handleKey) }, [prev, next]) if (totalSlides === 0) return const isVideoSlide = hasVideo && activeIndex === 0 const photoIndex = hasVideo ? activeIndex - 1 : activeIndex const active = !isVideoSlide ? photos[photoIndex] : null const single = totalSlides === 1 return (
{/* Main slide */}
{isVideoSlide ? ( ) : (
{active!.alt_text
)} {/* Nav buttons */} {!single && ( <> {/* Counter */}
{activeIndex + 1} / {totalSlides}
)}
{/* Thumbnail strip */} {!single && (
{ touchStartX.current = e.touches[0].clientX }} onTouchEnd={(e) => { if (touchStartX.current === null) return const delta = e.changedTouches[0].clientX - touchStartX.current if (delta < -50) next() else if (delta > 50) prev() touchStartX.current = null }} > {hasVideo && ( )} {photos.map((photo, idx) => { const slideIdx = hasVideo ? idx + 1 : idx return ( ) })}
)}
) }