import { useQuery } from "@tanstack/react-query";
import { useIntParam, useWant } from "taehui-ts/fe-utility";

import { wwwAXIOS } from "src/Www";
import { GetNoteAPI } from "src/wwwAPI";
import { useNoteStore } from "src/Stores";
import { useLocation } from "react-router-dom";

export default function useGetNote() {
  const { viewUnit } = useNoteStore();

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

  const { want } = useWant("/qwilight/note");

  const { pathname } = useLocation();

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

      return data;
    },
    initialData: {
      totalCount: 0,
      highestCount: 0,
      noteCount: 0,
      notes: [],
    },
  });
}
