import { useQuery, useQueryClient } from "@tanstack/react-query";
import { DealTagSchema } from "@/lib/client";
import {
    getDealTagsOptions,
    getDealTagsQueryKey,
} from "@/lib/client/@tanstack/react-query.gen";

type Props = {
    projectId: number;
};

const useDealTagsList = ({ projectId }: Props) => {
    const queryClient = useQueryClient();
    const options = {
        path: { projectId },
    };
    const { data, refetch } = useQuery(getDealTagsOptions(options));

    const queryKey = getDealTagsQueryKey(options);

    const setDealTags = (dealTags: DealTagSchema[]) => {
        queryClient.setQueryData(
            queryKey,
            (old: { items: DealTagSchema[] }) => ({
                ...old,
                items: dealTags,
            })
        );
    };

    return {
        dealTags: data?.items ?? [],
        setDealTags,
        refetch,
        queryKey,
    };
};

export default useDealTagsList;
