// eventList 
import { getToken } from '@/utils/auth'
import { ref } from 'vue'
import { getRewardEventTrackList } from '@/api/integral'
import { useUserStore } from '@/store/user'

const getRewardEventList = async () => {
  const eventList = await getRewardEventTrackList()
  localStorage.setItem('eventList', JSON.stringify(eventList) ?? [])
}

// 规则配置跟踪列表(每5分钟更新一次)
export const updateEventList = (type) => {
  const store = useUserStore()
  const timer = ref(null)
  if (getToken()) {
    getRewardEventList()
    if (timer.value) clearInterval(timer.value)
      
    timer.value = setInterval(async () => {
      // 没有token时销毁定时器
      if (!getToken()) {
        clearInterval(timer.value)
        timer.value = null
        return
      }
      getRewardEventList()

      // 更新账户信息
      if (type) await store.getUserAccountInfo()
      else await store.getEnterpriseUserAccountInfo()
    }, 200000)

  } else {
    clearInterval(timer.value)
    timer.value = null
    localStorage.setItem('eventList', [])
  }
}