declare global {
  interface Window {
    __SALUDUT_CONFIG__?: {
      apiBaseUrl?: string;
    };
  }
}

const getDefaultApiBaseUrl = (): string => {
  const host = window.location.hostname;
  const port = window.location.port;
  const isLocalhost = host === 'localhost' || host === '127.0.0.1';

  if (isLocalhost && port === '4200') {
    return 'http://localhost:3000/api';
  }

  return '/api';
};

const rawApiBaseUrl = window.__SALUDUT_CONFIG__?.apiBaseUrl || getDefaultApiBaseUrl();
export const API_BASE_URL = rawApiBaseUrl.replace(/\/+$/, '');
