---
import { LANGUAGES, DEFAULT_LANG, type Lang } from '@/i18n/utils';

interface Props {
  lang: Lang;
}
const { lang } = Astro.props;
const path = Astro.url.pathname;

function otherLangUrl(): string {
  const cleanPath = path.replace(/^\/(en|es)/, '') || '/';
  if (lang === DEFAULT_LANG) {
    return `/en${cleanPath === '/' ? '' : cleanPath}`;
  }
  return cleanPath;
}

const other = LANGUAGES.find((l) => l.code !== lang);
const isEn = lang === 'en';
---
<a
  href={otherLangUrl()}
  hreflang={other?.code}
  class="text-xs font-semibold tracking-wider px-2.5 py-1.5 rounded border transition-colors"
  style="border-color: var(--hds-line); color: var(--hds-fg-soft);"
  aria-label={isEn ? 'Switch language to Spanish' : 'Cambiar idioma a inglés'}
  onmouseover="this.style.borderColor='var(--color-hds-naranja)';this.style.color='var(--color-hds-naranja)';"
  onmouseout="this.style.borderColor='var(--hds-line)';this.style.color='var(--hds-fg-soft)';"
>
  {other?.label}
</a>
