import type { ExportJob, MicGain, Peaks, PublicConfig, Take, TrackKind, Transcript, Video, VideoSummary } from '../shared/types.ts'; async function request(method: string, url: string, body?: unknown): Promise { const headers: Record = {}; let payload: BodyInit | undefined; if (method !== 'GET') headers['X-Takes'] = '1'; if (body instanceof Blob) { payload = body; headers['Content-Type'] = body.type || 'application/octet-stream'; } else if (body !== undefined) { payload = JSON.stringify(body); headers['Content-Type'] = 'application/json'; } const res = await fetch(url, { method, headers, body: payload }); if (!res.ok) { let msg = `${res.status} ${res.statusText}`; try { msg = (await res.json()).error ?? msg; } catch { // not JSON } throw new Error(msg); } return res.json() as Promise; } async function getOptional(url: string): Promise { const res = await fetch(url, { cache: 'no-cache' }); return res.ok ? ((await res.json()) as T) : null; } const v = (id: string) => `/api/videos/${id}`; export const api = { config: () => request('GET', '/api/config'), saveConfig: (patch: Record) => request<{ ok: true }>('PUT', '/api/config', patch), micGain: (label: string) => request('GET', `/api/mic-gain?label=${encodeURIComponent(label)}`), setMicGain: (label: string, volume: number) => request('PUT', '/api/mic-gain', { label, volume }), videos: () => request('GET', '/api/videos'), setOrder: (order: string[]) => request<{ ok: true }>('PUT', '/api/order', { order }), createVideo: (input: { title: string; description: string; notes: string }) => request