import { createI18n } from "vue-i18n";
import zhCN from "./locales/zh-CN";
import enUS from "./locales/en-US";
import jaJP from "./locales/ja-JP";

const messages = {
  "zh-CN": zhCN,
  "en-US": enUS,
  "ja-JP": jaJP,
};

const supportedLocales = Object.keys(messages);

const getInitialLocale = () => {
  if (typeof window !== "undefined") {
    const params = new URLSearchParams(window.location.search);
    const lang = params.get("lang");
    if (lang && supportedLocales.includes(lang)) {
      return lang;
    }
  }
  return "zh-CN";
};

const i18n = createI18n({
  locale: getInitialLocale(),
  fallbackLocale: "zh-CN",
  messages,
  legacy: false,
  globalInjection: true,
});

export default i18n;
