import type { BuiltinWidgetKindInfo, WidgetDataResponse, WidgetInstance, WidgetInstanceInput, } from "../types"; const API_BASE = "/api"; export async function fetchBuiltinWidgetKinds(): Promise< BuiltinWidgetKindInfo[] > { const res = await fetch(`${API_BASE}/widgets/builtin`); if (!res.ok) throw new Error("Failed to fetch built-in widget kinds"); return res.json(); } export async function fetchWidgetInstances(): Promise { const res = await fetch(`${API_BASE}/widgets/instances`); if (!res.ok) throw new Error("Failed to fetch widget instances"); return res.json(); } export async function createWidgetInstance( input: WidgetInstanceInput, ): Promise { const res = await fetch(`${API_BASE}/widgets/instances`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(input), }); if (!res.ok) throw new Error("Failed to create widget instance"); return res.json(); } export async function updateWidgetInstance( input: WidgetInstanceInput, ): Promise { if (!input.id) throw new Error("Widget ID is required for update"); const res = await fetch(`${API_BASE}/widgets/instances/${input.id}`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(input), }); if (!res.ok) throw new Error("Failed to update widget instance"); return res.json(); } export async function deleteWidgetInstance( widgetId: string, ): Promise<{ status: string }> { const res = await fetch(`${API_BASE}/widgets/instances/${widgetId}`, { method: "DELETE", }); if (!res.ok) throw new Error("Failed to delete widget instance"); return res.json(); } export async function fetchWidgetData( widgetId: string, ): Promise { const res = await fetch(`${API_BASE}/widgets/instances/${widgetId}/data`); if (!res.ok) throw new Error("Failed to fetch widget data"); return res.json(); }