export const getBaseUrl = (): string => {
  const url = process.env.UMI_APP_API_BASE_URL || '';
  return url.replace(/\/+$/, '');
};

const isAbsoluteUrl = (u: string): boolean => {
  if (!u) return false;
  const lower = u.toLowerCase();
  return (
    lower.startsWith('http://') ||
    lower.startsWith('https://') ||
    lower.startsWith('data:') ||
    lower.startsWith('blob:')
  );
};

export const withBaseUrl = (input: string): string => {
  if (!input) return input;
  if (isAbsoluteUrl(input)) return input;
  const base = getBaseUrl();
  if (!base) return input;
  if (input.startsWith('/')) return `${base}${input}`;
  return `${base}/${input}`;
};