// @ts-nocheck
import React from 'react';
import { useNavigate } from 'react-router-dom';
import { useEventContext } from '../context/EventContext';
import { ADMIN_EVENTS_PATH, ADMIN_EVENT_PHOTOS_PATH } from '../constants';

export default function LiveRedirectPage(): React.ReactElement {
  const navigate = useNavigate();
  const { activeEvent, events, isLoading, refetch } = useEventContext();

  React.useEffect(() => {
    if (!isLoading && !events.length) {
      void refetch();
    }
  }, [isLoading, events.length, refetch]);

  React.useEffect(() => {
    if (isLoading) {
      return;
    }
    const targetEvent = activeEvent ?? events[0] ?? null;
    if (targetEvent) {
      navigate(ADMIN_EVENT_PHOTOS_PATH(targetEvent.slug), { replace: true });
    } else {
      navigate(ADMIN_EVENTS_PATH, { replace: true });
    }
  }, [isLoading, activeEvent, events, navigate]);

  if (isLoading) {
    return (
      <div className="flex min-h-screen items-center justify-center text-sm text-muted-foreground">
        Lade Events ...
      </div>
    );
  }
  return null;
}
