import { useCallback } from "react";
import { useLocation, useNavigate } from "react-router-dom";
import { stringifyFilters } from "@/contexts/MemoFilterContext";

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

  const navigateToDateFilter = useCallback(
    (date: string) => {
      const filterQuery = stringifyFilters([{ factor: "displayTime", value: date }]);
      const searchParams = new URLSearchParams(location.search);
      searchParams.set("filter", filterQuery);
      // Explicitly preserve the current pathname when navigating
      navigate({ pathname: location.pathname, search: searchParams.toString() }, { replace: true });
    },
    [navigate, location],
  );

  return navigateToDateFilter;
};
