import { GetNoteAPI } from "@/type/wwwAPI";

import { wwwAPI } from "@/utilities/wwwAPI";
import { useQuery } from "@tanstack/react-query";
import { useParams } from "next/navigation";
import { useMemo } from "react";
import { useIntParam, useIsPath } from "taehui-lib/fe-utilities";

export default function useGetNote() {
  const { param: page } = useIntParam("page", 1);
  const { param: fit } = useIntParam("fit", 0);
  const { param: src } = useIntParam("src", 0);

  let { want: [want] = [""] } = useParams<{ want: string[] }>();
  want = useMemo(() => decodeURIComponent(want), [want]);

  const isPath = useIsPath();

  return useQuery({
    enabled: isPath("/note"),
    queryKey: ["note", fit, src, want, page],
    queryFn: async () => {
      const { data } = await wwwAPI.get<GetNoteAPI>("/note", {
        params: {
          fit,
          src,
          want,
          page,
        },
      });

      return data;
    },
    initialData: {
      totalCount: 0,
      topCount: 0,
      lastPage: 0,
      notes: [],
    },
  });
}
