import useSWR from "swr";
import { isAxiosError } from "axios";
import { devlog } from "@frontend/kitui";
import { enqueueSnackbar } from "notistack";
import { resultApi } from "@api/result";
import { useResultStore } from "@root/results/store";

export function useResults(quizId: string) {
  const { isLoading, error, isValidating } = useSWR(
    "results",
    () => resultApi.getList(quizId),
    {
      onError: (error: unknown) => {
        const message = isAxiosError<string>(error)
          ? error.response?.data ?? ""
          : "";

        devlog("Error getting results list", error);
        enqueueSnackbar("Не удалось получить результаты");
      },
    },
  );
  const results = useResultStore((state) => state.results);

  return { results, isLoading, error, isValidating };
}
