import { useEffect, useState } from "react";
import { useQueryClient } from "@tanstack/react-query";
import { StatusSchema } from "@/lib/client";
import { getStatusesOptions } from "@/lib/client/@tanstack/react-query.gen";

type Props = {
    boardId?: number;
};

const useStatusesList = ({ boardId }: Props) => {
    const queryClient = useQueryClient();

    const [statuses, setStatuses] = useState<StatusSchema[]>([]);

    const fetchStatuses = () => {
        if (!boardId) {
            setStatuses([]);
            return;
        }
        
        queryClient
            .fetchQuery({
                ...getStatusesOptions({ path: { boardId } }),
            })
            .then(data => {
                setStatuses(data.statuses);
            })
            .catch(err => console.error(err));
    };

    useEffect(() => {
        fetchStatuses();
    }, [boardId]);

    return { statuses, setStatuses, refetch: fetchStatuses };
};

export default useStatusesList;
