import { createInstance } from 'i18next';
import { initReactI18next } from 'react-i18next/initReactI18next';
import resourcesToBackend from 'i18next-resources-to-backend';
import i18nConfig from '../i18nConfig';

import 'dayjs/locale/en';
import 'dayjs/locale/es';
import 'dayjs/locale/ar';
import 'dayjs/locale/tr';

export default async function initTranslations(locale: string, namespaces: string[]) {
	const i18nInstance = createInstance();

	await i18nInstance
		.use(initReactI18next)
		.use(
			resourcesToBackend(
				(language: string, namespace: string) =>
					import(`@/locales/${language}/${namespace}.json`),
			),
		)
		.init({
			lng: locale,
			fallbackLng: i18nConfig.defaultLocale,
			supportedLngs: i18nConfig.locales,
			defaultNS: namespaces[0],
			fallbackNS: namespaces[0],
			ns: namespaces,
			preload: typeof window === 'undefined' ? i18nConfig.locales : [],
		});

	return i18nInstance;
}
