import type { ReactNode } from 'react';
import { Logo } from './Logo';

export interface NavLink {
  href: string;
  label: string;
  current?: boolean;
}

export interface HeaderProps {
  institution: string;
  motto?: string;
  homeHref: string;
  /** Nom de la navigation, par exemple « Navigation principale ». */
  navLabel: string;
  links: readonly NavLink[];
  /** Texte du lien d'évitement, par exemple « Aller au contenu » (WCAG 2.4.1). */
  skipLabel: string;
  /** Identifiant de la zone principale visée par le lien d'évitement. */
  mainId?: string;
  /** Emplacement libre à droite : sélecteur de langue, connexion… */
  actions?: ReactNode;
}

const LIEN_SUR_BANCO =
  'inline-flex min-h-11 items-center rounded-md px-3 text-sur-banco no-underline hover:underline focus-visible:outline-anneau-sur-banco';

export function Header({
  institution,
  motto,
  homeHref,
  navLabel,
  links,
  skipLabel,
  mainId = 'contenu',
  actions,
}: HeaderProps) {
  return (
    <>
      <a
        href={`#${mainId}`}
        className="sr-only z-50 bg-surface px-4 py-3 font-semibold text-lien focus:not-sr-only focus:fixed focus:left-4 focus:top-4"
      >
        {skipLabel}
      </a>
      <header className="border-b-4 border-accent bg-banco text-sur-banco">
        <div className="mx-auto flex max-w-6xl flex-wrap items-center gap-x-6 gap-y-2 px-4 py-3">
          <a href={homeHref} className={`${LIEN_SUR_BANCO} gap-3 px-0`}>
            <Logo size={56} />
            <span className="flex flex-col leading-tight">
              <span className="font-titre text-lg font-semibold">{institution}</span>
              {motto ? <span className="text-sm">{motto}</span> : null}
            </span>
          </a>
          <nav aria-label={navLabel} className="ms-auto">
            <ul className="flex flex-wrap gap-1">
              {links.map((lien) => (
                <li key={lien.href}>
                  <a
                    href={lien.href}
                    aria-current={lien.current ? 'page' : undefined}
                    className={`${LIEN_SUR_BANCO} aria-[current=page]:underline aria-[current=page]:decoration-accent aria-[current=page]:decoration-2`}
                  >
                    {lien.label}
                  </a>
                </li>
              ))}
            </ul>
          </nav>
          {actions}
        </div>
      </header>
    </>
  );
}
