import { env } from "@ump/shared";

/**
 * Submitted PDFs are stored under `/submitted-initiatives/...` on the API host.
 * When `VITE_API_URL` points at the backend, prefix so links open the correct origin.
 * When API_URL is empty (dev same-origin), Vite proxy serves the path unchanged.
 */
export function resolveApplicationAssetUrl(url: string | undefined | null): string {
  if (url == null || typeof url !== "string") return "";
  const u = url.trim();
  if (!u) return u;
  if (/^https?:\/\//i.test(u)) return u;
  if (u.startsWith("/submitted-initiatives")) {
    const base = env.API_URL.replace(/\/$/, "");
    return base ? `${base}${u}` : u;
  }
  return u;
}
