"use client";

import {
  responsive,
  type EventCardProps,
} from "./eventCardTypes";
import { useEffect, useRef, useState } from "react";
import { useResponsiveValue } from "@/hooks/useViewportWidth";
import Carousel from "react-multi-carousel";
import "react-multi-carousel/lib/styles.css";
import { useRouter } from "next/navigation";
import { useLanguageStore } from "@/store/languageStore";
import { useIsMounted } from "@/hooks/useIsMounted";

/**
 * The home events carousel's slide bookkeeping.
 *
 * `onNavigationVisibilityChange` lets the page hide its own arrows when the
 * list is short enough not to scroll.
 */
export function useEventCard({
  events,
  onNavigationVisibilityChange,
}: Pick<
  EventCardProps,
  "events" | "onNavigationVisibilityChange" | "currentUser"
>) {
  const selectedLanguage = useLanguageStore((s) => s.language);
  const [currentSlide, setCurrentSlide] = useState(0);
  /*
   * Derived, not stored. `totalItems` was `useState(0)` written by a mount /
   * data effect, so on the first render after new data it was still the
   * previous length — which is what `goToNext` and the arrow-disabled checks
   * read, briefly disabling "next" on a list that had just grown. It is
   * `Array.isArray(events) ? events.length : 0` and nothing else, so it is computed here.
   */
  const totalItems = Array.isArray(events) ? events.length : 0;
  /*
   * `maxVisibleItems` is a measurement of the viewport, not state this
   * component owns. It was `useState` seeded with a guess and overwritten by a
   * mount effect plus a `resize` listener — the shape
   * `react-hooks/set-state-in-effect` flags, and the one `useViewportWidth`
   * exists for. `useResponsiveValue` reads the same three breakpoints through
   * `useSyncExternalStore`, which has a separate server snapshot and so needs
   * no post-mount write.
   *
   * Before measurement the value is the narrowest entry rather than the old
   * hard-coded 3. Only the arrow/dot visibility reads it — the carousel
   * itself takes `responsive` directly — so this affects one frame on first
   * paint, mobile-first, which is the direction the effect corrected to
   * anyway.
   */
  const maxVisibleItems = useResponsiveValue(
    [
      { minWidth: 1200, value: responsive.superLargeDesktop.items },
      { minWidth: 991, value: responsive.desktop.items },
      { minWidth: 768, value: responsive.smallDesktop.items },
      { minWidth: 0, value: responsive.tablet.items },
    ],
    responsive.tablet.items
  );
  const carouselRef = useRef<Carousel>(null);
  const router = useRouter();

  const isMounted = useIsMounted();

  useEffect(() => {
    const isNavigationVisible = Array.isArray(events) && events.length > maxVisibleItems;
    onNavigationVisibilityChange?.(isNavigationVisible);
  }, [events, maxVisibleItems, onNavigationVisibilityChange]);

  const goToPrevious = () => {
    if (carouselRef.current && currentSlide > 0) {
      carouselRef.current.previous(1);
      setCurrentSlide((prev) => Math.max(prev - 1, 0));
    }
  };

  const goToNext = () => {
    const maxSlideIndex = Math.max(0, totalItems - maxVisibleItems);
    if (carouselRef.current && currentSlide < maxSlideIndex) {
      carouselRef.current.next(1);
      setCurrentSlide((prev) => Math.min(prev + 1, maxSlideIndex));
    }
  };

  return {
    selectedLanguage,
    currentSlide,
    setCurrentSlide,
    totalItems,
    maxVisibleItems,
    carouselRef,
    router,
    isMounted,
    goToPrevious,
    goToNext,
  };
}
