import { useRouter } from '#imports'
import { useLocalePath, useSwitchLocalePath } from '#i18n'

/**
 * 统一的导航跳转 composable
 * 处理国际化路由跳转，确保在不同语言环境下都能正确跳转
 */
export const useNavigation = () => {
  const router = useRouter()
  const localePath = useLocalePath()
  const switchLocalePath = useSwitchLocalePath()

  /**
   * 统一的路由跳转方法
   * @param {string|object} target - 目标路径或路由对象
   * @param {object} options - 跳转选项
   */
  const navigateTo = (target, options = {}) => {
    const { replace = false, query = {} } = options

    // 如果传入的是字符串路径
    if (typeof target === 'string') {
      const targetPath = localePath(target)
      if (replace) {
        router.replace({ path: targetPath, query })
      } else {
        router.push({ path: targetPath, query })
      }
      return
    }

    // 如果传入的是路由名称对象
    if (target.name) {
      const targetPath = localePath(target.name)
      if (replace) {
        router.replace({ path: targetPath, query: { ...target.query, ...query } })
      } else {
        router.push({ path: targetPath, query: { ...target.query, ...query } })
      }
      return
    }

    // 如果传入的是完整路径对象
    if (target.path) {
      const targetPath = localePath(target.path)
      if (replace) {
        router.replace({ path: targetPath, query: { ...target.query, ...query } })
      } else {
        router.push({ path: targetPath, query: { ...target.query, ...query } })
      }
      return
    }

    // 默认处理
    if (replace) {
      router.replace(target)
    } else {
      router.push(target)
    }
  }

  /**
   * 跳转到首页
   */
  const goHome = (options = {}) => {
    navigateTo('/', options)
  }

  /**
   * 跳转到关于我们页面
   */
  const goAbout = (options = {}) => {
    navigateTo('/about-us', options)
  }

  /**
   * 跳转到产品页面
   */
  const goProduct = (category = null, options = {}) => {
    const query = category ? { category, ...options.query } : options.query
    navigateTo('/product', { ...options, query })
  }

  /**
   * 跳转到产品页面（查看更多）
   */
  const goToProductPage = (options = {}) => {
    navigateTo('/product', options)
  }

  /**
   * 跳转到联系我们页面
   */
  const goContact = (options = {}) => {
    navigateTo('/contact-us', options)
  }

  /**
   * 跳转到隐私政策页面
   */
  const goPrivacyPolicy = (options = {}) => {
    navigateTo('/privacy-policy', options)
  }

  /**
   * 跳转到产品详情页面
   */
  const goProductDetail = (productId, options = {}) => {
    navigateTo('/product/detail', { 
      ...options, 
      query: { id: productId, ...options.query } 
    })
  }

  /**
   * 语言切换跳转
   * @param {string} locale - 目标语言代码
   */
  const switchLanguage = (locale) => {
    const targetPath = switchLocalePath(locale)
    router.replace(targetPath)
  }

  /**
   * 获取当前语言环境下的路径
   * @param {string} path - 路径
   * @returns {string} 当前语言环境下的完整路径
   */
  const getLocalizedPath = (path) => {
    return localePath(path)
  }

  return {
    navigateTo,
    goHome,
    goAbout,
    goProduct,
    goToProductPage,
    goContact,
    goPrivacyPolicy,
    goProductDetail,
    switchLanguage,
    getLocalizedPath,
    localePath,
    switchLocalePath
  }
}
