import { changeLocale, SUPPORTED_LOCALES, type SupportedLocale } from '@chat-app/shared/i18n';
import { useTranslation } from 'react-i18next';

const LABELS: Record<SupportedLocale, string> = {
  en: 'EN',
  de: 'DE',
};

export function LanguageSwitcher({ compact = false }: { compact?: boolean }) {
  const { i18n } = useTranslation();
  const current = (i18n.resolvedLanguage ?? i18n.language) as SupportedLocale;

  return (
    <div
      role="group"
      aria-label="Language"
      className={
        'inline-flex items-center rounded-full border border-white/10 bg-white/5 p-0.5 text-[11px] font-medium ' +
        (compact ? '' : 'backdrop-blur')
      }
    >
      {SUPPORTED_LOCALES.map((locale) => {
        const active = locale === current;
        return (
          <button
            key={locale}
            type="button"
            aria-pressed={active}
            onClick={() => {
              if (!active) void changeLocale(locale);
            }}
            className={
              'cursor-pointer rounded-full px-2.5 py-1 transition focus:outline-none focus:ring-2 focus:ring-brand-400/40 ' +
              (active
                ? 'bg-brand-500/25 text-white ring-1 ring-brand-400/40'
                : 'text-neutral-400 hover:text-neutral-200')
            }
          >
            {LABELS[locale]}
          </button>
        );
      })}
    </div>
  );
}
