import type { Lang } from './ui';

const esToEnOverrides: Record<string, string | null> = {
  '/nosotros/': '/en/about/',
  '/contacto/': '/en/contact/',
  '/planes/': '/en/plans/',
  '/instructivos/': '/en/tutorials/',
  '/legal/privacidad/': '/en/legal/privacy/',
  '/legal/terminos/': '/en/legal/terms/',
  '/preguntas/': null,
};

const enToEsMap: Record<string, string> = {};
for (const [esPath, enPath] of Object.entries(esToEnOverrides)) {
  if (enPath) enToEsMap[enPath] = esPath;
}

const SITE_URL = 'https://hostingdelsur.net';

export function getHreflangUrl(
  pathname: string,
  currentLang: Lang,
  targetLang: Lang
): string | null {
  if (currentLang === targetLang) {
    return `${SITE_URL}${pathname}`;
  }

  if (currentLang === 'es' && targetLang === 'en') {
    const override = esToEnOverrides[pathname];
    if (override === null) return null;
    if (override) return `${SITE_URL}${override}`;
    return `${SITE_URL}/en${pathname}`;
  }

  if (currentLang === 'en' && targetLang === 'es') {
    const override = enToEsMap[pathname];
    if (override) return `${SITE_URL}${override}`;
    const esPath = pathname.replace(/^\/en/, '') || '/';
    return `${SITE_URL}${esPath}`;
  }

  return null;
}
