import { createI18n } from 'vue-i18n';
import { adminMessages, type AdminLocale } from './admin-messages';
import { ensureAdminLocaleLoaded } from './locale-loader';

export const ADMIN_LOCALE_STORAGE_KEY = 'admin_locale';

const saved = (localStorage.getItem(ADMIN_LOCALE_STORAGE_KEY) as AdminLocale) || 'zh-CN';

export async function createAdminI18n() {
  await ensureAdminLocaleLoaded(saved);
  return createI18n({
    legacy: false,
    locale: saved,
    fallbackLocale: ['en-US', 'zh-CN'] as AdminLocale[],
    messages: adminMessages,
  });
}

export function getAdminLocale(): AdminLocale {
  return (localStorage.getItem(ADMIN_LOCALE_STORAGE_KEY) as AdminLocale) || 'zh-CN';
}

export default adminMessages;
