// API Configuration
// Use localhost when accessing from the same machine
// Use the server's IP when accessing from other devices on the network
export const API_CONFIG = {
  // Automatically detect if we're on localhost or network access
  // If accessed via IP address, use that IP for backend too
  // For localhost, always use HTTP to avoid SSL certificate issues with self-signed certs
  getBaseUrl(): string {
    const hostname = window.location.hostname;
    // Always use HTTP for localhost to avoid SSL certificate validation issues
    if (hostname === 'localhost' || hostname === '127.0.0.1') {
      return 'http://localhost:8080';
    }
    // For network access, use same protocol as frontend
    const protocol = window.location.protocol;
    return `${protocol}//${hostname}:8080`;
  },
  
  getWsUrl(): string {
    const hostname = window.location.hostname;
    // SockJS uses HTTP/HTTPS, not WS/WSS directly
    // It will upgrade to WebSocket automatically
    // Always use HTTP for localhost to avoid SSL certificate validation issues
    if (hostname === 'localhost' || hostname === '127.0.0.1') {
      return 'http://localhost:8080/ws';
    }
    // For network access, use same protocol as frontend
    const protocol = window.location.protocol;
    return `${protocol}//${hostname}:8080/ws`;
  }
};

// Export functions that compute URLs dynamically at call time
// This ensures the URL is always computed based on current window.location
export const getBaseUrl = () => API_CONFIG.getBaseUrl();
export const getWsUrl = () => API_CONFIG.getWsUrl();

// For backward compatibility, export as constants (but they'll be computed at runtime)
export const BASE_URL = getBaseUrl();
export const WS_URL = getWsUrl();

