/**
 * 国际化配置
 *
 * 基于 vue-i18n 实现的多语言国际化解决方案。
 * 支持中文和英文切换，自动从本地存储恢复用户的语言偏好。
 *
 * ## 主要功能
 *
 * - 多语言支持 - 支持中文（简体）和英文两种语言
 * - 语言切换 - 运行时动态切换语言，无需刷新页面
 * - 持久化存储 - 自动保存和恢复用户的语言偏好
 * - 全局注入 - 在任何组件中都可以使用 $t 函数进行翻译
 * - 类型安全 - 提供 TypeScript 类型支持
 *
 * ## 支持的语言
 *
 * - zh: 简体中文
 * - en: English
 *
 * @module locales
 * @author Art Design Pro Team
 */

import { createI18n } from 'vue-i18n'
import type { I18n, I18nOptions } from 'vue-i18n'
import { LanguageEnum } from '@/enums/appEnum'
import { getSystemStorage } from '@/utils/storage'
import { StorageKeyManager } from '@/utils/storage/storage-key-manager'

// 同步导入语言文件
import enMessages from './langs/en.json'
import zhMessages from './langs/zh.json'

// 业务模块语言包（按模块拆分，避免主语言文件过大）
import enBillingMessages from './en-US/billing.json'
import zhBillingMessages from './zh-CN/billing.json'
import enDictionaryMessages from './en/dictionary.json'
import zhDictionaryMessages from './zh-CN/dictionary.json'
import enTenantMessages from './en/tenant'
import zhTenantMessages from './zh-CN/tenant'
import enAnnouncementMessages from './en/announcement'
import zhAnnouncementMessages from './zh-CN/announcement'
import enMerchantMessages from './en/merchant'
import zhMerchantMessages from './zh-CN/merchant'
import enStoreMessages from './en/store'
import zhStoreMessages from './zh-CN/store'
import enTenantPackageMessages from './en/tenantPackage'
import zhTenantPackageMessages from './zh-CN/tenantPackage'

/**
 * 存储键管理器实例
 */
const storageKeyManager = new StorageKeyManager()

/**
 * 语言消息对象
 */
type JsonObject = Record<string, unknown>

const isPlainObject = (value: unknown): value is JsonObject => {
  return typeof value === 'object' && value !== null && !Array.isArray(value)
}

/**
 * 深度合并语言包（用于按模块拆分语言文件）
 * - 数组：直接覆盖
 * - 对象：递归合并
 * - 其他：直接覆盖
 */
const deepMerge = <T extends JsonObject, U extends JsonObject>(target: T, source: U): T & U => {
  const result: JsonObject = { ...target }

  Object.keys(source).forEach((key) => {
    const sourceValue = source[key]
    const targetValue = result[key]

    if (isPlainObject(targetValue) && isPlainObject(sourceValue)) {
      result[key] = deepMerge(targetValue, sourceValue)
      return
    }

    result[key] = sourceValue
  })

  return result as T & U
}

const messages = {
  [LanguageEnum.EN]: deepMerge(
    deepMerge(
      deepMerge(enMessages as unknown as JsonObject, enBillingMessages as unknown as JsonObject),
      enDictionaryMessages as unknown as JsonObject
    ),
    {
      tenant: enTenantMessages,
      tenantPackage: enTenantPackageMessages,
      announcement: enAnnouncementMessages,
      merchant: enMerchantMessages,
      store: enStoreMessages
    } as unknown as JsonObject
  ),
  [LanguageEnum.ZH]: deepMerge(
    deepMerge(
      deepMerge(zhMessages as unknown as JsonObject, zhBillingMessages as unknown as JsonObject),
      zhDictionaryMessages as unknown as JsonObject
    ),
    {
      tenant: zhTenantMessages,
      tenantPackage: zhTenantPackageMessages,
      announcement: zhAnnouncementMessages,
      merchant: zhMerchantMessages,
      store: zhStoreMessages
    } as unknown as JsonObject
  )
} as unknown as NonNullable<I18nOptions['messages']>

/**
 * 语言选项列表
 * 用于语言切换下拉框
 */
export const languageOptions = [
  { value: LanguageEnum.ZH, label: '简体中文' },
  { value: LanguageEnum.EN, label: 'English' }
]

/**
 * 从存储中获取语言设置
 * @returns 语言设置，如果获取失败则返回默认语言
 */
const getDefaultLanguage = (): LanguageEnum => {
  // 尝试从版本化的存储中获取语言设置
  try {
    const storageKey = storageKeyManager.getStorageKey('user')
    const userStore = localStorage.getItem(storageKey)

    if (userStore) {
      const { language } = JSON.parse(userStore)
      if (language && Object.values(LanguageEnum).includes(language)) {
        return language
      }
    }
  } catch (error) {
    console.warn('[i18n] 从版本化存储获取语言设置失败:', error)
  }

  // 尝试从系统存储中获取语言设置
  try {
    const sys = getSystemStorage()
    if (sys) {
      const { user } = JSON.parse(sys)
      if (user?.language && Object.values(LanguageEnum).includes(user.language)) {
        return user.language
      }
    }
  } catch (error) {
    console.warn('[i18n] 从系统存储获取语言设置失败:', error)
  }

  // 返回默认语言
  console.debug('[i18n] 使用默认语言:', LanguageEnum.ZH)
  return LanguageEnum.ZH
}

/**
 * i18n 配置选项
 */
const i18nOptions: I18nOptions = {
  locale: getDefaultLanguage(),
  legacy: false,
  globalInjection: true,
  fallbackLocale: LanguageEnum.ZH,
  messages
}

/**
 * i18n 实例
 */
const i18n: I18n = createI18n(i18nOptions)

/**
 * 翻译函数类型
 */
interface Translation {
  (key: string): string
}

/**
 * 全局翻译函数
 * 可在任何地方使用，无需导入 useI18n
 */
export const $t = i18n.global.t as Translation

export default i18n
