import { useEffect } from 'react'

import { useUserBalanceStore } from '@/stores/userBalanceStore'

/**
 * 用户积分余额 Hook
 * 提供积分余额的获取和管理功能
 */
export const useUserBalance = () => {
  const { balance, loading, error, load, startPolling, stopPolling, deductBalance } = useUserBalanceStore()

  // 组件挂载时开始轮询，卸载时停止
  useEffect(() => {
    startPolling()
    return () => {
      stopPolling()
    }
  }, [startPolling, stopPolling])

  return {
    balance,
    loading,
    error,
    load,
    deductBalance,
  }
}
