"use client";

import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import {
  buildI18nResources,
  LOCALE_NAMESPACES,
} from "@/lib/i18n/resources";
import {
  DEFAULT_LANGUAGE,
  ENABLED_LANGUAGES,
} from "@/lib/i18n/registry";
import { resolveClientLanguage } from "@/lib/i18n/clientLanguage";

if (!i18n.isInitialized) {
  i18n.use(initReactI18next).init({
    lng: typeof window === "undefined" ? DEFAULT_LANGUAGE : resolveClientLanguage(),
    fallbackLng: DEFAULT_LANGUAGE,
    supportedLngs: [...ENABLED_LANGUAGES],
    defaultNS: "common",
    ns: [...LOCALE_NAMESPACES],
    resources: buildI18nResources(),
    interpolation: { escapeValue: false },
    react: { useSuspense: false },
  });
}

export default i18n;
