import { useCallback } from 'react';
import { type NavigateOptions, useNavigate } from 'react-router-dom';

import { huLibrariesRoutes } from '../routes';

import { useArticleId } from './useArticleId';

export const useNavigateToCurrentArticle = () => {
  const navigate = useNavigate();
  const { rootId, childId } = useArticleId();

  return useCallback(
    (options?: NavigateOptions) => {
      if (!rootId) {
        navigate(huLibrariesRoutes.base(), options);
        return;
      }
      navigate(huLibrariesRoutes.article(rootId, childId), options);
    },
    [navigate, rootId, childId],
  );
};
