import type { BayesResult, SampleInfo } from "./types"; const BASE = import.meta.env.VITE_API_BASE_URL || ""; async function jsonFetch(path: string, init?: RequestInit): Promise { const r = await fetch(BASE + path, { headers: { "Content-Type": "application/json" }, ...init, }); if (!r.ok) { const text = await r.text().catch(() => ""); throw new Error(`${r.status} ${r.statusText}: ${text || path}`); } return (await r.json()) as T; } export const api = { health: () => jsonFetch<{ status: string; service: string; version: string; anthropic_key_present: boolean; anthropic_model: string; }>("/api/health"), listSamples: () => jsonFetch("/api/data/samples"), getSample: (id: string) => jsonFetch(`/api/data/samples/${id}`), bayesianCompute: (body: { data: number[]; judgments: number[]; R: number; scenario_name?: string; reference_case?: string; }) => jsonFetch("/api/bayesian/compute", { method: "POST", body: JSON.stringify(body), }), bayesianKDE: (body: { data: number[]; judgments: number[]; R: number }) => jsonFetch<{ points: Array<{ x: number; y: number }> }>( "/api/bayesian/kde", { method: "POST", body: JSON.stringify(body) } ), bayesianReport: (body: { data: number[]; judgments: number[]; R: number; scenario_name?: string; reference_case?: string; }) => jsonFetch<{ markdown: string; result: BayesResult }>( "/api/bayesian/report", { method: "POST", body: JSON.stringify(body) } ), };