
import { statisticsApi, lineStatisticsApi, getSpectrumImgApi } from '@/apis/dataManage/statistics.ts'
import { ref } from 'vue'

export const useMain = () => {
  // 列表loading
  const loading = ref(false)

  // 获取统计
  const getStatistics = async () => {
    if (loading.value) return false
    loading.value = true
    const res = await statisticsApi().catch(err => {
      return err
    })
    loading.value = false
    return res
  }

  const lineLoading = ref(false)
  let lineController: undefined | AbortController = undefined
  const getLineStatistics = async (batchId: string) => {
    if (lineLoading.value) return false
    lineController = new AbortController()
    lineLoading.value = true
    const res = await lineStatisticsApi(batchId, lineController).catch(err => {
      return err
    }).finally(() => {
      lineController = undefined
    })
    lineLoading.value = false
    return res
  }

  const onLineStatisticsInterrupt = () => {
    if (lineController) lineController.abort()
  }


  const getSpectrumImg = async (data: Record<string, any>) => {
    const res = await getSpectrumImgApi(data).catch(err => {
      return err
    })
    return res
  }



  return {
    statisticsRequest: {
      request: getStatistics,
      loading: loading
    },
    lineStatisticsRequest: {
      request: getLineStatistics,
      loading: lineLoading,
      interrupt: onLineStatisticsInterrupt
    },
    getSpectrumImgRequest: {
      request: getSpectrumImg
    }
  }
}
