import * as sidecar from '@@/sidecar-registry';

// The officer-music sidecar starts its audio-streaming HTTP server on a random port and reports it here
// on connect. We remember it so `/api/music/*` always proxies to the current server.

let serverPort: number | null = null;

sidecar.on('music:server', (msg) => {
  const port = (msg as { port?: number }).port;
  if (typeof port !== 'number') return;
  serverPort = port;
  console.log(`[music] sidecar audio server registered on port ${port}`);
});

/** Base URL of the sidecar's audio server, or null if the sidecar hasn't reported in yet. */
export function getMusicServerUrl(): string | null {
  return serverPort ? `http://127.0.0.1:${serverPort}` : null;
}
