import { NavigationRoutes } from 'navigation/enum';
import { PATH_PAGES } from 'navigation/web';
import { useCallback } from 'react';
import { useLocation, useNavigate, useParams } from 'react-router-dom';
import { ParamsProps, Routing } from './types';

export const useRouting = (): Routing => {
  const navigate = useNavigate();
  const location = useLocation();
  const params = useParams();

  const pathname = location.pathname;
  const routeSegments = pathname.split('/').slice(1);
  const activeRoute = '/' + routeSegments[0];

  const toLanding = (param: ParamsProps) => {
    navigate(`/${PATH_PAGES.landing}`, { state: param });
  };

  const navigateTo = (nav: any) => {
    navigate(nav);
  };

  const navigateBack = () => {
    navigate(-1);
  };
  const openRoom = useCallback(
    (id) => {
      navigate(`${NavigationRoutes.Rooms}/${id}`);
    },
    [navigate]
  );
  return {
    openRoom,
    toLanding,
    navigateTo,
    params,
    activeRoute,
    routeSegments,
    navigateBack,
  };
};
