// biome-ignore lint/suspicious/noExplicitAny: <explanation>
export function bungeeApiFetcher<T = any>(
	query: string,
	apiKey?: string,
	body?: string,
	method: "GET" | "POST" = "GET", // Default method is GET
) {
	if (!apiKey) {
		throw new Error("API Key is required for bungeeApiFetcher");
	}

	let headers = {};
	if (body && method === "POST") {
		headers = {
			"x-api-key": apiKey,
			"Content-Type": "application/json",
			"API-KEY": apiKey,
		};
	} else {
		headers = {
			"x-api-key": apiKey,
			"API-KEY": apiKey,
		};
	}

	return fetch(query, { headers, body, method }).then((res) => {
		if (!res.ok) {
			return res.json().then((errorData) => {
				console.log("error data: ", errorData);
				const errorMessage = errorData.message || "An error occurred";
				// biome-ignore format:
				throw new Error(`${res.status}: ${errorMessage}`);
			});
		}
		return res.json() as Promise<T>;
	});
}
