import loadI18n from "./loadI18n";
// eslint-disable-next-line import/no-unresolved
import {I18n} from "intl-tel-input/i18n/types";

const cache = new Map();

async function internationalisation(lang: string) {
	const locale = lang || 'en';
	const i18n = loadI18n[locale];
	return (await i18n()).default;
}

function fetchTranslation(lang: string): Promise<I18n> {
	if (!cache.has(lang)) {
		cache.set(lang, internationalisation(lang));
	}
	return cache.get(lang);
}

export default fetchTranslation
