import {useLocation} from 'react-router-dom';
import {useEffect, useRef, useState} from 'react';
import {toast} from "react-toastify";
import {ROUTE_PATH} from "@constants/routes";
const usePreviousRoute = () => {
    const location = useLocation();
    const [preLocation, setPreLocation] = useState({});
    // const prevLocation = useRef(null);
    // const currentLocation = useRef(null);
    const [currentLocation, setCurrentLocation] = useState({});
    const [gameID, setGameID] = useState(0);
    const parseGameID = ()=>{
        let pathname=location.pathname;
        let gid=0;
        if (pathname.startsWith(ROUTE_PATH.GAME)){
            let path="";
            let temp=pathname.split("?");
            path=temp.shift();
            temp=path.split("/");
            path=temp.pop();
            gid=Number(path);
        }
        setGameID(gid);
    }
    useEffect(() => {
            setPreLocation({...currentLocation})
            setCurrentLocation({...location});
            parseGameID();
        }, [location.pathname]);
    return {
        preLocation,
        gameID
    };
}

export default usePreviousRoute
