const DEFAULT_API_BASE_URL = '';

const configuredApiBaseUrl = import.meta.env.VITE_API_BASE_URL;

export const API_BASE_URL = (configuredApiBaseUrl !== undefined ? configuredApiBaseUrl : DEFAULT_API_BASE_URL).replace(/\/+$/, '');

export const buildApiUrl = (path = '') => {
  const normalizedPath = path.startsWith('/') ? path : `/${path}`;
  return API_BASE_URL ? `${API_BASE_URL}${normalizedPath}` : normalizedPath;
};

export const toAbsoluteApiUrl = (path) => {
  if (!path) return null;
  if (/^(https?:|blob:|data:)/i.test(path)) return path;
  return buildApiUrl(path);
};