import { datasetListApi, deleteDatasetApi, updateDatasetApi, downloadApi } from '@/apis/dataset/index.ts'

import { ref } from 'vue'


export const useDatasetHooks = () => {

  // 获取测试集loading
  const testDatasetListLoading = ref(false)

  // 获取测试集
  const getTestDatasetList = async (params?: Record<string, any>) => {
    if (testDatasetListLoading.value) return false
    testDatasetListLoading.value = true
    const res = await datasetListApi({
      ...params || {},
      type: 'test'
    }).catch(err => {
      return err
    })
    testDatasetListLoading.value = false
    return res
  }

  // 获取训练集loading
  const trainDatasetListLoading = ref(false)

  // 获取训练集
  const getTrainDatasetList = async (params?: Record<string, any>) => {
    if (trainDatasetListLoading.value) return false
    trainDatasetListLoading.value = true
    const res = await datasetListApi({
      ...params || {},
      type: 'train'
    }).catch(err => {
      return err
    })
    trainDatasetListLoading.value = false
    return res
  }

  const deleteLoading = ref(false)

  // 删除数据集
  const deleteRequest = async (id: string) => {
    if (deleteLoading.value) return false
    deleteLoading.value = true
    const res = await deleteDatasetApi(id).catch(err => {
      return err
    })
    deleteLoading.value = false
    return res
  }

  // 更新数据集
  const updateLoading = ref(false)

  const updateRequest = async (params: Record<string, any>) => {
    if (updateLoading.value) return false
    updateLoading.value = true
    const res = await updateDatasetApi(params).catch(err => {
      return err
    })
    updateLoading.value = false
    return res
  }

  const downloadRequest = async (params: Record<string, any>) => {
    return await downloadApi(params)
  }


  return {
    testDatasetListRequest: {
      request: getTestDatasetList,
      loading: testDatasetListLoading
    },
    trainDatasetListRequest: {
      request: getTrainDatasetList,
      loading: trainDatasetListLoading
    },
    deleteRequest: {
      request: deleteRequest,
      loading: deleteLoading
    },
    updateRequest: {
      request: updateRequest,
      loading: updateLoading
    },
    downloadRequest: {
      request: downloadRequest
    }
  }
}
