import { useNavigate, useLocation } from 'react-router-dom';

export const useGoBack = () => {
  const navigate = useNavigate();
  const location = useLocation();

  const goBack = () => {
    // navigate(-1);


    const pathParts = location.pathname.split('/').filter(part => part !== '');
    const lastSegment = pathParts[pathParts.length - 1];
    let newPath = "";
    if (lastSegment === "material" || lastSegment === "exercise" || lastSegment === "result") {
      pathParts.pop();
      pathParts.pop();
    } else {
      pathParts.pop();
    }
    newPath = `/${pathParts.join('/')}`;
    
    navigate(newPath);
  };

  return goBack;
};
