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

const usePreviousPath = () => {
  const location = useLocation();
  const navigate = useNavigate();
  const [previousPath, setPreviousPath] = useState(null);
    console.log(previousPath);
    
  useEffect(() => {
    // Update the previous path whenever the location changes
    const currentPath = location.pathname as any;
    setPreviousPath(currentPath);

    // Clean up
    return () => {
      setPreviousPath(null);
    };
  }, [location]);

  const navigateToPreviousPath = () => {
    if (previousPath) {
      navigate(previousPath);
    } else {
      navigate(-1); // Fallback to default behavior
    }
  };

  return { navigateToPreviousPath };
};

export default usePreviousPath;
