import type { CallSummary, DeviceStatus, GatewayEvent, GatewayStatus, HealthStatus } from './types'; async function get(path: string): Promise { const res = await fetch(path); if (!res.ok) throw new Error(`${res.status} ${res.statusText}`); return res.json() as Promise; } export async function fetchGatewayStatus(): Promise { return get('/'); } export async function fetchHealth(): Promise { return get('/health'); } export async function fetchActiveCalls(): Promise { return get('/api/calls/active'); } export async function fetchDevices(): Promise { return get('/api/devices'); } export async function hangupCall(callId: string): Promise { const res = await fetch(`/api/calls/${callId}/hangup`, { method: 'POST' }); if (!res.ok) throw new Error(`${res.status} ${res.statusText}`); } export function connectEventStream( onEvent: (e: GatewayEvent) => void, onClose: () => void, ): () => void { const proto = location.protocol === 'https:' ? 'wss:' : 'ws:'; const ws = new WebSocket(`${proto}//${location.host}/ws/events`); ws.onmessage = (msg) => { try { const event = JSON.parse(msg.data as string) as GatewayEvent; onEvent(event); } catch { // malformed frame — ignore } }; ws.onclose = () => onClose(); ws.onerror = () => onClose(); return () => ws.close(); }