import { ref } from 'vue'
import { getRewardEventTrackList } from '@/api/integral'

export const getRewardEventList = async () => {
  const { data } = await getRewardEventTrackList()
  uni.setStorageSync('eventList', JSON.stringify(data) ?? [])
}

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

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