import { LanguageVariant } from '@hubspot/cms-components';

// Define interfaces for styled components props

type TranslationsArrayAsObject = Record<string, LanguageVariant>;

type GetLanguageDisplayNameArgs = {
  currentPageLanguage: string;
  translationsArrayAsObject: TranslationsArrayAsObject | null;
};

// Shared utility functions
export const getLanguageDisplayName = (args: GetLanguageDisplayNameArgs) => {
  const { currentPageLanguage, translationsArrayAsObject } = args;

  if (!translationsArrayAsObject || !currentPageLanguage) {
    return '';
  }

  return (
    translationsArrayAsObject[currentPageLanguage]?.languageDisplayName
      .LOCALIZED ?? ''
  );
};

export const createTranslationsArrayAsObject = (
  translations: LanguageVariant[]
): TranslationsArrayAsObject | null => {
  // Early return if no translations
  if (!translations || translations.length <= 1) {
    return null;
  }

  return translations.reduce<TranslationsArrayAsObject>((acc, translation) => {
    acc[translation.languageCode] = translation;
    return acc;
  }, {});
};
