import _ from 'lodash';

export const languages = [
	{ name: 'English', code: 'en', locale: 'en_US' },
	{ name: 'Deutsch', code: 'de', locale: 'de_DE' },
	{ name: '日本語', code: 'ja', locale: 'ja_JP' },
	{ name: 'Français', code: 'fr', locale: 'fr_FR' },
	{ name: 'Español', code: 'es', locale: 'es_ES' },
	{ name: 'Italiano', code: 'it', locale: 'it_IT' },
];

export const localeToLanguageCode = (locale) => {
	const lang = _.find(languages, (l) => l.locale === locale || l.code === locale || _.startsWith(locale, l.code)) || { code: 'en' };
	return lang.code;
};

export const languageCodeToLocale = (code) => {
	const lang = _.find(languages, (l) => l.locale === code || l.code === code) || { locale: 'en_US' };
	return lang.locale;
};

export const attachLocalizedFrameName = (frame, framePairing, locale) => {
	const translatedName = _.get(framePairing, `${frame.model}.locales[${locale}]`, null);
	return translatedName ? { ...frame, modelName: translatedName } : frame;
};
