import {
  fetchTemps,
  addTemp,
  deleteTemp,
  useTemp,
  cloneTemp
} from '@/api/app-custom-center/tab-module-list'

const actions = {
  fetchTemps({ commit }, params) {
    return new Promise((resolve, reject) => {
      fetchTemps(params)
        .then(response => {
          const { data } = response
          resolve(data)
        })
        .catch(error => {
          console.error(error)
          reject(error)
        })
    })
  },

  editTemp({ commit }, params) {
    const query = { name: params.name, saveType: params.saveType }
    if (params.id) {
      query['id'] = params.id
    }
    if (params.selectImage) {
      query['image'] = params.selectImage
    }

    return new Promise((resolve, reject) => {
      addTemp(query)
        .then(response => {
          const { data } = response
          resolve(data)
        })
        .catch(error => {
          console.error(error)
          reject(error)
        })
    })
  },

  deleteTemp({ commit }, ids) {
    return new Promise((resolve, reject) => {
      deleteTemp({ id: ids })
        .then(response => {
          const { data } = response
          resolve(data)
        })
        .catch(error => {
          console.error(error)
          reject(error)
        })
    })
  },

  useTemp({ commit }, params) {
    return new Promise((resolve, reject) => {
      useTemp(params)
        .then(response => {
          const { data } = response
          resolve(data)
        })
        .catch(error => {
          console.error(error)
          reject(error)
        })
    })
  },

  cloneTemp({ commit }, params) {
    return new Promise((resolve, reject) => {
      cloneTemp(params)
        .then(response => {
          const { data } = response
          resolve(data)
        })
        .catch(error => {
          console.error(error)
          reject(error)
        })
    })
  }
}

export default {
  namespaced: true,
  actions
}
