import { ref } from 'vue'
import { addApi, updateApi, deleteApi, listApi, importPretrainedForModelApi, deletePretrainedForModelApi } from '@/apis/model/index.ts'

export const useModelHooks = () => {
  // 新增
  const addLoading = ref(false)
  const addModel = async (params: any) => {
    if (addLoading.value) return false
    addLoading.value = true
    const res = await addApi(params).catch(err => {
      return err
    })
    addLoading.value = false
    return res
  }

  // 修改
  const updateLoading = ref(false)
  const updateModel = async (params: any) => {
    if (updateLoading.value) return false
    updateLoading.value = true
    const res = await updateApi(params).catch(err => {
      return err
    })
    updateLoading.value = false
    return res
  }

  // 删除
  const deleteLoading = ref(false)
  const deleteModel = async (id: string) => {
    if (deleteLoading.value) return false
    deleteLoading.value = true
    const res = await deleteApi(id).catch(err => {
      return err
    })
    deleteLoading.value = false
    return res
  }

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


  // 导入预训练权重
  const importPretrainedForModel = async (data: FormData) => {
    const res = await importPretrainedForModelApi(data).catch(err => {
      return err
    })
    return res
  }

  // 删除预训练权重
  const deletePretrainedForModel = async (data: Record<string, any>) => {
    const res = await deletePretrainedForModelApi(data).catch(err => {
      return err
    })
    return res
  }


  return {
    addRequest: {
      request: addModel,
      loading: addLoading
    },
    updateRequest: {
      request: updateModel,
      loading: updateLoading
    },
    deleteRequest: {
      request: deleteModel,
      loading: deleteLoading
    },
    listRequest: {
      request: getModelList,
      loading: listLoading
    },
    importPretrainedForModelRequest: {
      request: importPretrainedForModel
    },
    deletePretrainedForModelRequest: {
      request: deletePretrainedForModel
    }
  }
}
