/**
 * Resolves an image source to a full URL.
 * - If src starts with "http" or "https", returns it as-is.
 * - Otherwise, prepends the API base URL from environment variables.
 * - If src is null/undefined, returns the placeholder.
 */
import { IMAGE_BASE_URL } from "./apiHelper";

export const PLACEHOLDER_IMAGE =
  "https://static.wixstatic.com/media/703249_3a76a53b765e46d4b3a5e4c3ec6ddd7e~mv2.png/v1/fill/w_800,h_800,al_c,q_90/placeholder.png";

export function resolveImage(src: string | null | undefined): string {
  if (!src) return PLACEHOLDER_IMAGE;
  if (src.startsWith("http://") || src.startsWith("https://") || src.startsWith("//")) {
    return src;
  }
  let base = IMAGE_BASE_URL;
  if (base.endsWith('/')) {
    base = base.slice(0, -1);
  }
  return `${base}${src.startsWith("/") ? "" : "/"}${src}`;
}
