import API_URL from "../config/api";
import type { ApiErrorResponse } from "../types";

export function useGetWorkshopsLength() {
    async function getWorkshopsLength() {
        try {
            const response = await fetch(`${API_URL}/api/workshops-length`, {
                method: "GET",
                headers: {
                    Accept: "application/json",
                    "Content-Type": "application/json",
                    Authorization: `Bearer ${localStorage.getItem("token")}`
                },
            });

            const data = await response.json();

            if (response.ok) {
                return {
                    workshops: data.data.workshops,
                    workshopsInscribed: data.data.workshopsInscribed,
                };
            }

            return data as ApiErrorResponse;

        } catch (error) {
            return error as ApiErrorResponse;
        }
    }

    return { getWorkshopsLength };
}