export function useChatApi<T>(path: string, options: Parameters<typeof $fetch<T>>[1] = {}) {
  const config = useRuntimeConfig();
  const { token, user } = useAuth();
  const headers = new Headers(options.headers as HeadersInit | undefined);

  if (!headers.has("Content-Type") && options.method && options.method !== "GET") {
    headers.set("Content-Type", "application/json");
  }

  if (token.value) {
    headers.set("Authorization", `Bearer ${token.value}`);
  }

  if (user.value?.email) {
    headers.set("x-user-email", user.value.email);
  }

  return $fetch<T>(`${config.public.chatApiBase}${path}`, {
    ...options,
    headers,
    credentials: "include"
  }).catch((error: Error) => {
    throw new Error(error.message || "Не удалось выполнить запрос к чату");
  });
}
