import { useEffect, useState } from "react";
import { useQuery } 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 [statuses, setStatuses] = useState<StatusSchema[]>([]);

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

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

    return { statuses, setStatuses, refetch, isLoading };
};

export default useStatusesList;
