import React from 'react';
import { useNavigate } from 'react-router-dom';
import { ADMIN_HOME_PATH } from '../../constants';

export function useBackNavigation(fallback?: string) {
  const navigate = useNavigate();
  const fallbackTarget = fallback ?? ADMIN_HOME_PATH;

  return React.useCallback(() => {
    if (typeof window !== 'undefined' && window.history.length > 1) {
      navigate(-1);
      return;
    }

    navigate(fallbackTarget, { replace: true });
  }, [fallbackTarget, navigate]);
}
