// Shared utility: Fetch with timeout
// Prevents edge functions from hanging indefinitely

export const fetchWithTimeout = async (
  url: string,
  options: RequestInit,
  timeout = 30000
): Promise<Response> => {
  const controller = new AbortController();
  const timeoutId = setTimeout(() => controller.abort(), timeout);

  try {
    const response = await fetch(url, {
      ...options,
      signal: controller.signal,
    });
    clearTimeout(timeoutId);
    return response;
  } catch (error: any) {
    clearTimeout(timeoutId);
    if (error.name === 'AbortError') {
      throw new Error(`İstek zaman aşımına uğradı (${timeout / 1000}s)`);
    }
    throw error;
  }
};
