export function resolvePublicPageUrl(publicUrl: string): string {
  const value = String(publicUrl ?? '').trim();
  if (!value) return value;
  if (/^https?:\/\//i.test(value)) return value;
  if (value.startsWith('//')) return `${window.location.protocol}${value}`;
  if (value.startsWith('/')) return `${window.location.origin}${value}`;
  return `${window.location.origin}/${value.replace(/^\/+/, '')}`;
}

export function resolvePlazaPostUrl(postId: string): string {
  const configured = String(import.meta.env.VITE_PLAZA_BASE ?? '').trim();
  if (configured) {
    return `${configured.replace(/\/$/, '')}/plaza/p/${encodeURIComponent(postId)}`;
  }
  if (import.meta.env.DEV) {
    return `http://127.0.0.1:3001/plaza/p/${encodeURIComponent(postId)}`;
  }
  return `${window.location.origin}/plaza/p/${encodeURIComponent(postId)}`;
}

export function resolvePlazaHomeUrl(): string {
  const configured = String(import.meta.env.VITE_PLAZA_BASE ?? '').trim();
  if (configured) {
    return `${configured.replace(/\/$/, '')}/plaza`;
  }
  if (import.meta.env.DEV) {
    return 'http://127.0.0.1:3001/plaza';
  }
  return `${window.location.origin}/plaza`;
}

export function resolveMindSpaceHomeUrl(): string {
  const configured = String(import.meta.env.VITE_MINDSPACE_BASE ?? '').trim();
  if (configured) {
    return `${configured.replace(/\/$/, '')}/space`;
  }
  return `${window.location.origin}/space`;
}
