const SHARE_BASE_ENV_KEYS = [
  'MADARX_PUBLIC_SHARE_BASE_URL',
  'NEXT_PUBLIC_SHARE_BASE_URL',
  'NEXT_PUBLIC_SITE_URL',
];

function normalizedBaseUrl() {
  for (const key of SHARE_BASE_ENV_KEYS) {
    const raw = process.env[key];
    if (!raw || !String(raw).trim()) continue;
    const value = String(raw).trim().replace(/\/+$/, '');
    if (/^https?:\/\//i.test(value)) return value;
  }
  return '';
}

export function publicShareUrl(path) {
  const normalizedPath = String(path || '').startsWith('/') ? String(path || '') : `/${path || ''}`;
  const base = normalizedBaseUrl();
  return base ? `${base}${normalizedPath}` : normalizedPath;
}
