import type { DatasetInfo, EpisodeData, Preset } from "./types"; const BASE = "/api/arena"; const PRESETS_BASE = "/api/presets/arena"; async function fetchJSON(url: string, opts?: RequestInit): Promise { const res = await fetch(`${BASE}${url}`, { headers: { "Content-Type": "application/json" }, ...opts, }); if (!res.ok) { const err = await res.json().catch(() => ({ error: res.statusText })); throw new Error(err.error || res.statusText); } return res.json(); } async function fetchPresetsJSON(url: string, opts?: RequestInit): Promise { const res = await fetch(`${PRESETS_BASE}${url}`, { headers: { "Content-Type": "application/json" }, ...opts, }); if (!res.ok) { const err = await res.json().catch(() => ({ error: res.statusText })); throw new Error(err.error || res.statusText); } return res.json(); } export const api = { loadDataset(repo: string, split?: string) { return fetchJSON }>("/datasets/load", { method: "POST", body: JSON.stringify({ repo, split }), }); }, listDatasets() { return fetchJSON("/datasets/"); }, getEpisode(dsId: string, envId: string, idx: number) { return fetchJSON(`/datasets/${dsId}/episode/${encodeURIComponent(envId)}/${idx}`); }, unloadDataset(dsId: string) { return fetchJSON<{ status: string }>(`/datasets/${dsId}`, { method: "DELETE" }); }, listPresets() { return fetchPresetsJSON(""); }, createPreset(name: string, repo: string, split?: string) { return fetchPresetsJSON("", { method: "POST", body: JSON.stringify({ name, repo, split }), }); }, updatePreset(id: string, updates: { name?: string; split?: string }) { return fetchPresetsJSON(`/${id}`, { method: "PUT", body: JSON.stringify(updates), }); }, deletePreset(id: string) { return fetchPresetsJSON<{ status: string }>(`/${id}`, { method: "DELETE" }); }, };