/** Valide une URL d’image publique (chemin local ou https). Pas de http: sauf localhost en dev (évite le contenu mixte). */
export function normalizeImageUrl(raw: string | null | undefined): string | null {
  if (raw == null) return null;
  const s = String(raw).trim();
  if (s === "") return null;
  if (s.startsWith("/") && !s.includes("..") && !s.includes("//")) {
    return s;
  }
  try {
    const u = new URL(s);
    if (u.protocol === "https:") {
      return u.href;
    }
    if (u.protocol === "http:") {
      const isLocal =
        u.hostname === "localhost" ||
        u.hostname === "127.0.0.1" ||
        u.hostname === "[::1]";
      if (process.env.NODE_ENV !== "production" && isLocal) {
        return u.href;
      }
    }
  } catch {
    /* ignore */
  }
  return null;
}

/** Chemins générés par l’upload admin (`/uploads/…`). */
export function isUploadAssetPath(src: string): boolean {
  return src.startsWith("/uploads/");
}
