/**
 * Resolves media URL returned from an API.
 * - Returns null (not empty string) when URL cannot be constructed.
 * - Accepts absolute URLs and relative paths.
 */
export function resolveMediaUrl(
  input: string | null | undefined,
  apiBaseUrl: string | null | undefined
): string | null {
  const raw = (input ?? "").trim();
  if (!raw) return null;

  if (/^https?:\/\//i.test(raw)) return raw;

  const base = (apiBaseUrl ?? "").trim();
  if (!base) return null;

  const baseNoSlash = base.replace(/\/+$/, "");
  const pathWithSlash = raw.startsWith("/") ? raw : `/${raw}`;
  return `${baseNoSlash}${pathWithSlash}`;
}
