import { useQuery, useQueryClient } from "@tanstack/react-query";
import { ServicesKitSchema } from "@/lib/client";
import {
    getServicesKitsOptions,
    getServicesKitsQueryKey,
} from "@/lib/client/@tanstack/react-query.gen";

export type ServicesKitsList = {
    servicesKits: ServicesKitSchema[];
    setServicesKits: (servicesKits: ServicesKitSchema[]) => void;
    refetch: () => void;
    queryKey: any[];
    isLoading: boolean;
};

const useServicesKitsList = (): ServicesKitsList => {
    const queryClient = useQueryClient();
    const { data, refetch, isLoading } = useQuery(getServicesKitsOptions());

    const queryKey = getServicesKitsQueryKey();

    const setServicesKits = (servicesKits: ServicesKitSchema[]) => {
        queryClient.setQueryData(
            queryKey,
            (old: { items: ServicesKitSchema[] }) => ({
                ...old,
                items: servicesKits,
            })
        );
    };

    return {
        servicesKits: data?.items ?? [],
        setServicesKits,
        refetch,
        queryKey,
        isLoading,
    };
};

export default useServicesKitsList;
