import React from 'react';
import { useNavigate, useLocation } from 'react-router-dom';
import { useEventContext } from '../../context/EventContext';
import { NavKey } from '../components/BottomNav';
import { resolveTabTarget, resolveDefaultTarget } from '../lib/tabHistory';
import { adminPath } from '../../constants';

export function useMobileNav(currentSlug?: string | null, activeTab?: NavKey) {
  const navigate = useNavigate();
  const location = useLocation();
  const { activeEvent } = useEventContext();
  const slug = currentSlug ?? activeEvent?.slug ?? null;

  const go = React.useCallback(
    (key: NavKey) => {
      // Tap-to-reset: If the user taps the tab they are already on, reset to root.
      if (key === activeTab) {
        navigate(resolveDefaultTarget(key, slug));
        return;
      }

      const target = resolveTabTarget(key, slug);
      navigate(target);
    },
    [navigate, activeTab, slug]
  );

  return { go, slug };
}
