import { Lang, Langs, languages, popularLangKeys } from "localization/i18n";
import React from "react";
import { useTranslation } from "react-i18next";

const useLangugeViewModel = () => {
  const { i18n } = useTranslation();
  const switchLang = React.useCallback(
    (lang: string) => i18n.changeLanguage(lang),
    [i18n]
  );

  const popularLanguages = Object.keys(languages)
    .filter((k) => popularLangKeys.includes(k))
    .reduce((acc, k) => {
      acc.set(k, languages[k as Langs]);
      return acc;
    }, new Map<string, Lang>());

  return {
    selected: i18n.resolvedLanguage as Langs,
    languages: languages,
    popularLanguages,
    switchLang,
  };
};

export { useLangugeViewModel };
