import { useEffect, useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { DealSchema } from "@/client";
import { getDealsOptions } from "@/client/@tanstack/react-query.gen";

type Props = {
    boardId?: number;
};

const useDealsList = ({ boardId }: Props) => {
    const [deals, setDeals] = useState<DealSchema[]>([]);

    const { data, refetch, isLoading } = useQuery({
        ...getDealsOptions({ path: { boardId: boardId! } }),
        enabled: boardId !== undefined,
    });

    useEffect(() => {
        if (boardId === undefined) {
            setDeals([]);
            return;
        }
        if (data?.deals) {
            setDeals(data.deals);
        }
    }, [data?.deals, boardId]);

    return { deals, setDeals, refetch, isLoading };
};

export default useDealsList;
