import styles from './LanguageOptions.module.scss';
import cx from '../utils/classname.js';
import type { LanguageOptionsProps } from './types.js';
import List, { ListItem } from '../List/index.js';
import Link from '../Link/index.js';

const LanguageOptions = ({
  translations,
  menuBackgroundColor,
  menuBackgroundColorHover,
  color,
  colorHover,
}: LanguageOptionsProps) => {
  if (!translations || translations.length <= 1) {
    return null;
  }

  const inlineStyles: React.CSSProperties & Record<`--hs-${string}`, string> =
    {
      ...(color?.rgba && {
        '--hs-languageSwitcher-color': color.rgba,
      }),
      ...(menuBackgroundColorHover?.rgba && {
        '--hs-languageSwitcher-backgroundColor-hover':
          menuBackgroundColorHover.rgba,
      }),
      ...(colorHover?.rgba && {
        '--hs-languageSwitcher-color-hover': colorHover.rgba,
      }),
    };

  return (
    <List
      style={inlineStyles}
      role="menu"
      className={styles.languageList}
      gap="0px"
      showMarker={false}
    >
      {translations.map(translation => (
        <ListItem
          key={translation.languageCode}
          role="menuitem"
          className={cx(styles.languageItem, {
            [styles['languageItem--active']]: translation.isActive,
          })}
        >
          <Link
            href={translation.localizedUrl}
            lang={translation.languageCode}
            // hrefLang={translation.languageCode} // this was in the Elevate implementation, so leaving here for now
            className={styles.languageLink}
          >
            {translation.languageDisplayName.LOCALIZED}
          </Link>
        </ListItem>
      ))}
    </List>
  );
};

export default LanguageOptions;
