import { camelCase } from 'lodash';

type countries = {
  [countryCode: string]: string;
};

type flags = {
  [countryName: string]: string;
};

export const getCountriesToContinents = async () => {
  return import('../assets/json/country_continent.json');
};

export const getFlagsAsync = async () => {
  return import('../assets/flags');
};

export const getCountriesListAsync = async () => {
  const { default: countriesList }: { default: countries } = await import(
    '../assets/json/country.json'
  );
  return countriesList;
};

export const getCountriesWithFlagAsync = async () => {
  const countriesList = await getCountriesListAsync();
  const flags: flags = await getFlagsAsync();

  const countriesWithFlag: countries = Object.keys(countriesList).reduce(
    (acc: countries, countryCode) => {
      if ((flags as flags)[camelCase(countriesList[countryCode])]) {
        acc[countryCode] = countriesList[countryCode];
      }
      return acc;
    },
    {}
  );

  return countriesWithFlag;
};
