import { getPlatformByModuleNameApi } from '@/apis/system/config.ts'
import { testDatasetListApi, trainDatasetListApi } from '@/apis/dataset/index.ts'
import { listApi as modelListApi } from '@/apis/model/index.ts'


import { ref } from 'vue'


export const useMain = () => {
  // 模型
  const modelListLoading = ref(false)
  const getModelList = async () => {
    if (modelListLoading.value) return false
    modelListLoading.value = true
    const res = await modelListApi().catch(err => {
      return err
    })
    modelListLoading.value = false
    return res
  }

  const platformLoading = ref(false)
  const getPlatformList = async (moduleName: string) => {
    if (platformLoading.value) return false
    platformLoading.value = true
    const res = await getPlatformByModuleNameApi(moduleName).catch(err => {
      return err
    })
    platformLoading.value = false
    return res
  }

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

  // 获取测试集
  const getTestDatasetList = async () => {
    if (testDatasetListLoading.value) return false
    testDatasetListLoading.value = true
    const res = await testDatasetListApi().catch(err => {
      return err
    })
    testDatasetListLoading.value = false
    return res
  }

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

  // 获取训练集
  const getTrainDatasetList = async () => {
    if (trainDatasetListLoading.value) return false
    trainDatasetListLoading.value = true
    const res = await trainDatasetListApi().catch(err => {
      return err
    })
    trainDatasetListLoading.value = false
    return res
  }

  return {
    modelListRequest: {
      request: getModelList,
      loading: modelListLoading
    },
    platformRequest: {
      request: getPlatformList,
      loading: platformLoading
    },
    testDatasetListRequest: {
      request: getTestDatasetList,
      loading: testDatasetListLoading
    },
    trainDatasetListRequest: {
      request: getTrainDatasetList,
      loading: trainDatasetListLoading
    }
  }
}
