import React, { ImgHTMLAttributes } from "react";

export function Image(props: ImgHTMLAttributes<HTMLImageElement>) {
  const { src: relativeSrc = "" } = props;

  let absoluteSrc = process.env.NEXT_PUBLIC_BASE_PATH ?? "/";
  if (absoluteSrc.endsWith("/") && relativeSrc.startsWith("/")) {
    absoluteSrc += relativeSrc.slice(1);
  } else if (absoluteSrc.endsWith("/") || relativeSrc.startsWith("/")) {
    absoluteSrc += relativeSrc;
  } else {
    absoluteSrc += "/" + relativeSrc;
  }

  return <img {...props} src={absoluteSrc} />;
}
