import {
  iGetList,
  iDeleteWord,
  iDeleteWords,
  iGetItem,
  iAddItem,
  iUpdateItem
} from '@/api/hotWords'

const getList = async data => {
  const result = await iGetList(data)
  if (result.success) {
    const data = result.data
    result.data = data.map(x => ({
      id: x.id,
      sort: x.sort,
      word: x.word,
      isTop: x.isTop,
      isTopF: x.isTop ? '已置顶' : '未置顶',
      isUse: x.isDel,
      isUseF: x.isDel ? '使用中' : '未使用'
    }))
  }
  return result
}

const getItem = async params => {
  const result = await iGetItem(params)
  if (result.success) {
    const data = result.data
    result.data = {
      word: data.word,
      sort: data.sort,
      isTop: data.isTop,
      isDel: data.isDel
    }
  }
  return result
}

const saveItem = async({ id, ...params }) => {
  if (id) {
    return await iUpdateItem(id, params)
  } else {
    return await iAddItem(params)
  }
}

const deleteWord = async params => await iDeleteWord(params)

const deleteWords = async params => await iDeleteWords(params)

export {
  getList,
  getItem,
  deleteWord,
  deleteWords,
  saveItem
}
