import { useState, useEffect } from 'react'
import { getChannelRank } from '@/services/agent'
import { fillTime } from '../utils'

const now = new Date()

export default function useRank() {
  const [rankDateRange, setRankDateRange] = useState([now.toJSON().substring(0, 10), now.toJSON().substring(0, 10)])
  const [rankList, setRankList] = useState([])

  useEffect(() => {
    const [startDate, endDate] = rankDateRange
    getChannelRank({
      startDate: fillTime(startDate, true),
      endDate: fillTime(endDate, false),
    }).then((res) => {
      setRankList(res || [])
    })
  }, [rankDateRange])

  return [rankDateRange, setRankDateRange, rankList]
}
