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

// The officer-slskd sidecar starts its slskd reverse-proxy on a random loopback port and reports it here
// on connect. We remember it so `/api/slskd/*` always forwards to the current sidecar. The platform holds
// NO knowledge of slskd itself (URL, API key) — only where the sidecar is listening.

let serverPort: number | null = null;

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

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