export const useLanguageSwitch = () => {
  const { locale, setLocale } = useI18n()
  const { getLocalePath } = useI18nUtils()
  
  // 切换语言
  const switchLanguage = async (newLocale, router) => {
    if (!newLocale || newLocale === locale.value) {
      console.log('语言切换跳过:', { newLocale, current: locale.value })
      return Promise.resolve()
    }
    
    console.log('开始切换语言:', { from: locale.value, to: newLocale })
    
    try {
      // 更新 i18n locale
      await setLocale(newLocale)
      console.log('I18n locale 已更新:', newLocale)
      
      // 获取当前路由信息
      const currentRoute = router.currentRoute.value
      const currentPath = currentRoute.path
      console.log('当前路径:', currentPath)
      
      // 获取新语言的路径
      const newPath = getLocalePath(newLocale, currentPath)
      console.log('新路径:', newPath)
      
      // 如果路径需要改变，则进行导航
      if (newPath !== currentPath) {
        try {
          await router.push(newPath)
          console.log('语言切换导航完成')
        } catch (error) {
          console.warn('语言切换导航失败:', error)
          // 导航失败不影响语言切换
        }
      } else {
        console.log('无需导航')
      }
      
      console.log('语言切换成功完成')
      return Promise.resolve()
    } catch (error) {
      console.error('语言切换失败:', error)
      // 出错时回退到默认语言
      try {
        await setLocale('en')
        console.log('已回退到默认语言: en')
      } catch (fallbackError) {
        console.error('回退到默认语言失败:', fallbackError)
      }
      return Promise.reject(error)
    }
  }
  
  // 获取当前语言显示名称
  const getCurrentLanguageName = () => {
    try {
      const availableLocales = [
        { code: 'en', name: 'English' },
        { code: 'cn', name: '简体中文' }
      ]
      const currentLang = availableLocales.find(
        lang => lang.code === locale.value
      )
      return currentLang ? currentLang.name : 'English'
    } catch (error) {
      console.warn('获取语言名称失败:', error)
      return 'English'
    }
  }
  
  // 获取目标语言的显示名称
  const getLanguageName = (code) => {
    try {
      const availableLocales = [
        { code: 'en', name: 'English' },
        { code: 'cn', name: '简体中文' }
      ]
      const targetLang = availableLocales.find(lang => lang.code === code)
      return targetLang ? targetLang.name : code
    } catch (error) {
      console.warn('获取目标语言名称失败:', error)
      return code
    }
  }
  
  // 初始化语言
  const initLanguage = async () => {
    console.log('开始初始化语言')
    try {
      // 从 cookie 中获取之前选择的语言
      const savedLocale = useCookie('i18n_redirected')
      
      if (savedLocale.value && savedLocale.value !== locale.value) {
        console.log('使用保存的语言:', savedLocale.value)
        await setLocale(savedLocale.value)
      } else {
        console.log('使用当前语言:', locale.value)
      }
      
      console.log('语言初始化完成')
      return Promise.resolve()
    } catch (error) {
      console.error('语言初始化失败:', error)
      return Promise.reject(error)
    }
  }
  
  // 检查语言是否可用
  const isLanguageAvailable = (code) => {
    const availableCodes = ['en', 'cn']
    return availableCodes.includes(code)
  }
  
  return {
    // 核心功能
    switchLanguage,
    getCurrentLanguageName,
    getLanguageName,
    initLanguage,
    
    // 工具函数
    isLanguageAvailable,
    
    // 状态
    currentLocale: computed(() => locale.value)
  }
}
