import { idCard } from 'ionicons/icons';
import { QuizCRQuestion } from '../types/QuizCRQuestion';
import { usePocketBase } from './usePocketBase';
import { QueryClient } from '@tanstack/react-query';
import PocketBase from 'pocketbase';

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: Infinity,
    },
  },
});

const fetchCRQuestions = async (cat_id: string, pb: PocketBase) => {
  const response = await queryClient.fetchQuery({
    queryKey: ['fetchData', cat_id],
    staleTime: 60 * 1000,
    queryFn: async () => {
      return await pb.collection('QuizCRQuestions').getList<QuizCRQuestion>(1, 9999, {
        filter: `cat_id = "${cat_id}"`,
        $autoCancel: false,
      });
    },
  });

  return response;
};

export default fetchCRQuestions;
