import { API_URL } from '../api';

// Resolve a backend-served static path (e.g. /cache/prefetch/*.json) against the API origin
function resolveBackend(path: string): string {
  try {
    if (/^https?:\/\//i.test(path)) return path;
    const base = process.env.REACT_APP_API_BASE_URL || process.env.REACT_APP_API_URL || 'http://localhost:8080/api/v1';
    const u = new URL(base);
    u.pathname = path.startsWith('/') ? path : `/${path}`;
    return u.toString();
  } catch {
    return path;
  }
}

export async function getFacrClubInfoCache(): Promise<any | null> {
  const url = resolveBackend('/cache/prefetch/facr_club_info.json');
  const res = await fetch(url, { cache: 'no-cache' });
  if (!res.ok) return null;
  return res.json();
}

export async function getFacrTablesCache(): Promise<any | null> {
  const url = resolveBackend('/cache/prefetch/facr_tables.json');
  const res = await fetch(url, { cache: 'no-cache' });
  if (!res.ok) return null;
  return res.json();
}
