import i18next from "i18next"
import { initReactI18next } from "react-i18next"
import { costrictTranslations, mergeLanguageResources } from "./costrict-i18n/setup"
import { COSTRICT_LANGUAGES } from "@roo/language"

// Build translations object
const translations: Record<string, Record<string, any>> = {}

// Dynamically load locale files
const localeFiles = import.meta.glob("./locales/**/*.json", { eager: true })
const ALLOW_LANGUAGES = Object.keys(COSTRICT_LANGUAGES)

// Process all locale files
Object.entries(localeFiles).forEach(([path, module]) => {
	// Extract language and namespace from path
	// Example path: './locales/en/common.json' -> language: 'en', namespace: 'common'
	const match = path.match(/\.\/locales\/([^/]+)\/([^/]+)\.json/)

	if (match) {
		const [, language, namespace] = match
		if (!ALLOW_LANGUAGES.includes(language)) return

		// Initialize language object if it doesn't exist
		if (!translations[language]) {
			translations[language] = {}
		}

		// Add namespace resources to language
		translations[language][namespace] = (module as any).default || module
	}
})

// console.log("Dynamically loaded translations:", Object.keys(translations))
// Merge CoStrict translations
const mergedTranslations = mergeLanguageResources(translations, costrictTranslations)
// console.log(`Merged webview-ui translations:`, mergedTranslations)

// Initialize i18next for React
// This will be initialized with the VSCode language in TranslationProvider
i18next.use(initReactI18next).init({
	lng: (window as any).defaultLanguage || "en", // Default language (will be overridden)
	fallbackLng: (window as any).defaultLanguage || "en",
	debug: false,
	resources: mergedTranslations,
	interpolation: {
		escapeValue: false, // React already escapes by default
	},
})

export function loadTranslations() {
	Object.entries(mergedTranslations).forEach(([lang, namespaces]) => {
		try {
			Object.entries(namespaces).forEach(([namespace, resources]) => {
				i18next.addResourceBundle(lang, namespace, resources, true, true)
			})
		} catch (error) {
			console.warn(`Could not load ${lang} translations:`, error)
		}
	})
}

export default i18next
