// frontend/lib/automation/api.ts import type { AutomationPage, ParsePreview, RuleCreate, RuleOut } from "@/lib/types"; const J = { "Content-Type": "application/json" }; async function ok(r: Response): Promise { if (!r.ok) throw new Error(`${r.status} ${r.statusText}`); return r.json() as Promise; } export const automationApi = { page: () => fetch("/api/automation", { cache: "no-store" }).then((r) => ok(r)), parse: (text: string) => fetch("/api/automation/parse", { method: "POST", headers: J, body: JSON.stringify({ text }) }).then( (r) => ok(r), ), createRule: (b: RuleCreate) => fetch("/api/automation/rules", { method: "POST", headers: J, body: JSON.stringify(b) }).then((r) => ok(r), ), toggle: (id: string) => fetch(`/api/automation/rules/${id}/toggle`, { method: "POST" }).then((r) => ok(r)), remove: (id: string) => fetch(`/api/automation/rules/${id}`, { method: "DELETE" }).then((r) => ok<{ deleted: string }>(r)), accept: (id: string) => fetch(`/api/automation/suggestions/${id}/accept`, { method: "POST" }).then((r) => ok(r)), dismiss: (id: string) => fetch(`/api/automation/suggestions/${id}/dismiss`, { method: "POST" }).then((r) => ok<{ dismissed: string }>(r), ), };