/**
 * Derive API base URL from the WS URL.
 * wss://assay.loop42.de/ws → https://assay.loop42.de
 * ws://localhost:8000/ws   → http://localhost:8000
 * No env var needed — single source of truth from VITE_WS_URL.
 * Falls back to same-origin (empty string) for dev.
 */
let _cached: string | null = null;

export function getApiBase(): string {
    if (_cached !== null) return _cached;
    const wsUrl = import.meta.env.VITE_WS_URL as string | undefined;
    if (!wsUrl) { _cached = ''; return ''; }
    try {
        const url = new URL(wsUrl);
        const proto = url.protocol === 'wss:' ? 'https:' : 'http:';
        _cached = `${proto}//${url.host}`;
    } catch {
        _cached = '';
    }
    return _cached;
}
