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

export function useGetWorkshopsSearch() {
    async function getWorkshopsSearch(searchQuery: string = "") {
        const response = await fetch(
            `${API_URL}/api/workshops-search?search=${encodeURIComponent(searchQuery)}`,
            {
                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 as Workshop[],
                meta: data.meta
            };
        } else {
            return data as ApiErrorResponse;
        }
    }

    return { getWorkshopsSearch };
}