import { useAccount, useChainId } from 'wagmi'
import { useEffect, useState } from 'react'

/**
 * 获取钱包连接状态的 Hook
 * 带有客户端渲染保护
 */
export function useWalletStatus() {
  const { address, isConnected, isConnecting } = useAccount()
  const chainId = useChainId()
  const [mounted, setMounted] = useState(false)

  useEffect(() => {
    setMounted(true)
  }, [])

  // 在 SSR 期间或未 mounted 时返回安全的默认值
  if (!mounted) {
    return {
      address: undefined,
      isConnected: false,
      isConnecting: false,
      chainId: undefined,
      mounted: false,
    }
  }

  return {
    address,
    isConnected,
    isConnecting,
    chainId,
    mounted: true,
  }
}
