import Image from "next/image";

import { cn } from "@/lib/utils";

type AppLogoProps = {
  className?: string;
};

export function AppLogo({ className }: AppLogoProps) {
  const src = process.env.NEXT_PUBLIC_APP_LOGO?.trim();

  if (!src) {
    return (
      <span
        className={cn(
          "flex h-20 w-20 shrink-0 items-center justify-center rounded-lg bg-primary text-sm font-medium text-primary-foreground",
          className
        )}
      >
        SP
      </span>
    );
  }

  const isRemote = /^https?:\/\//i.test(src);

  if (isRemote) {
    return (
      // URL absolue : évite d’imposer remotePatterns pour chaque domaine.
      // eslint-disable-next-line @next/next/no-img-element
      <img
        src={src}
        alt=""
        className={cn(
          "h-9 w-auto max-w-[200px] shrink-0 object-contain object-left",
          className
        )}
        loading="eager"
        decoding="async"
      />
    );
  }

  return (
    <Image
      src={src}
      alt=""
      width={200}
      height={200}
      className={cn(
        " m-2h-20 w-auto max-w-[200px] shrink-0 object-contain object-left",
        className
      )}
      priority
    />
  );
}
