import { listApi, updateLabelApi, getExportUrlApi, importLabelByFileApi } from '@/apis/dataManage/dataExport.ts'
import { ref } from 'vue'

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

  // 获取列表
  const getList = async (params?: Record<string, any>) => {
    if (listLoading.value) return false
    listLoading.value = true
    const res = await listApi(params).catch(err => {
      return err
    })
    listLoading.value = false
    return res
  }


  // 编辑
  const updateLabelLoading = ref(false)

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

  // 获取导出地址
  const exportUrlLoading = ref(false)
  const getExportUrl = async (params: Record<string, any>) => {
    if (exportUrlLoading.value) return false
    exportUrlLoading.value = true
    const res = await getExportUrlApi(params).catch(err => {
      return err
    })
    exportUrlLoading.value = false
    return res
  }

  // 导入标注
  const importLabelLoading = ref(false)
  const importLabelByFile = async (params: Record<string, any>) => {
    if (importLabelLoading.value) return false
    importLabelLoading.value = true
    const res = await importLabelByFileApi(params).catch(err => {
      return err
    })
    importLabelLoading.value = false
    return res
  }



  return {
    listRequest: {
      request: getList,
      loading: listLoading
    },
    updateRequest: {
      request: onUpdateLabel,
      loading: updateLabelLoading
    },
    exportUrlRequest: {
      request: getExportUrl,
      loading: exportUrlLoading
    },
    importLabelRequest: {
      request: importLabelByFile,
      loading: importLabelLoading
    }
  }
}
