/**
 * Trouve-KA — favicon d'un domaine (icônes DuckDuckGo, repli neutre)
 * Author: Simon-Pierre Boucher
 * Contact: contact@spboucher.ai
 */

"use client";

import { useState } from "react";

interface FaviconProps {
  domain: string;
}

/** Petit favicon décoratif d'un domaine; repli sur une pastille neutre. */
export function Favicon({ domain }: FaviconProps) {
  const [failed, setFailed] = useState(false);

  if (failed || !domain) {
    return (
      <span
        aria-hidden="true"
        className="inline-block h-4 w-4 shrink-0 rounded-sm border border-line bg-surface-2"
      />
    );
  }

  return (
    // eslint-disable-next-line @next/next/no-img-element
    <img
      src={`https://icons.duckduckgo.com/ip3/${encodeURIComponent(domain)}.ico`}
      alt=""
      aria-hidden="true"
      width={16}
      height={16}
      loading="lazy"
      className="h-4 w-4 shrink-0 rounded-sm"
      onError={() => setFailed(true)}
    />
  );
}
