import {
  iGetSesitiveList,
  iAddSesitive,
  iUpdateSesitive,
  iGetList,
  iGetItem,
  iGetTodayNum,
  iDeleteComment,
  iDeleteComments,
  iSetTopItem,
  iApproveItem,
  iSaveItem,
  iChangeShowStatus,
  iGetListInArticle,
  iGetSensitiveOptions,
  iDeleteSesitive
} from '@/api/comment'

import listResult from '@/model/listResult'

import MODULETYPE from '@/const/moduleType'

import { translateLabel } from '@/utils'

const getSensitiveOptions = async data => {
  const result = await iGetSensitiveOptions({ data, dictKey: 'SENSITIVE_WORDS_TPYE' })
  if (result.success) {
    const data = result.data
    result.data = data.map(x => ({
      label: x.value,
      value: x.dictKey
    }))
  }
  return result
}

const getSesitiveList = async data => listResult(await iGetSesitiveList(data), x => ({
  id: x.id,
  word: x.word
}))

const getList = async data => listResult(await iGetList(data), x => ({
  id: x.id,
  sort: x.sort.toString(),
  moduleType: translateLabel(MODULETYPE.ARTICLE_ITEM_MODAL_TYPE, x.moduleType),
  title: x.articleTitle,
  createDate: x.createDate,
  comment: x.content,
  isAdminRead: x.isAdminRead,
  isAdminReadF: x.isAdminRead ? '已查看' : '未查看',
  isIllegal: x.isIllegal,
  isIllegalF: x.isIllegal ? '是' : '否',
  isTop: x.isTop,
  isTopF: x.isTop ? '已置顶' : '未置顶',
  isShow: x.isShow,
  isShowF: x.isAdminRead ? (x.isShow ? '已展示' : '未展示') : '未审批',
  isPassed: x.isPassed,
  isPassedF: x.isAdminRead !== true ? '未审核' : (x.isPassed ? '通过' : '拒绝'),
  sensitiveWords: (x.sensitiveWords && x.sensitiveWords.length > 0) ? x.sensitiveWords.join(',') : null,
  personInfo: `${x.accountNickname}(${x.accountMobileNumber})`
}))

const getListInArticle = async data => listResult(await iGetListInArticle(data), x => ({
  id: x.id,
  sort: x.sort.toString(),
  moduleType: translateLabel(MODULETYPE.ARTICLE_ITEM_MODAL_TYPE, x.moduleType),
  title: x.articleTitle,
  createDate: x.createDate,
  comment: x.content,
  isAdminRead: x.isAdminRead,
  isAdminReadF: x.isAdminRead ? '已查看' : '未查看',
  isIllegal: x.isIllegal,
  isIllegalF: x.isIllegal ? '是' : '否',
  isTop: x.isTop,
  isTopF: x.isTop ? '已置顶' : '未置顶',
  isShow: x.isShow,
  isShowF: x.isAdminRead ? (x.isShow ? '已展示' : '未展示') : '未审批',
  isPassed: x.isPassed,
  isPassedF: x.isAdminRead !== true ? '未审核' : (x.isPassed ? '通过' : '拒绝'),
  sensitiveWords: (x.sensitiveWords && x.sensitiveWords.length > 0) ? x.sensitiveWords.join(',') : null,
  nickName: x.accountNickname,
  mobile: x.accountMobileNumber,
  personInfo: `${x.accountNickname}(${x.accountMobileNumber})`
}))

const getItem = async params => {
  const result = await iGetItem(params)
  if (result.success) {
    const data = result.data
    result.data = {
      moduleType: translateLabel(MODULETYPE.ARTICLE_ITEM_MODAL_TYPE, data.moduleType),
      title: data.title,
      createDate: data.createDate,
      comment: data.content,
      mobile: data.accountMobileNumber,
      nickName: data.accountNickname,
      articleTitle: data.articleTitle,
      sort: data.sort,
      isShow: data.isShow,
      isTop: data.isTop,
      isAdminRead: data.isAdminRead,
      isPassed: data.isPassed,
      passedStatus: data.passedStatus,
      isScan: data.isScan,
      sensitiveWords: (data.sensitiveWords && data.sensitiveWords.length > 0) ? data.sensitiveWords.join(',') : null
    }
  }
  return result
}

const getTodayNum = async params => await iGetTodayNum(params)

const saveItem = async params => await iSaveItem(params)

const deleteComment = async params => await iDeleteComment(params)

const deleteComments = async params => await iDeleteComments(params)

const approveItem = async params => await iApproveItem(params)

const setTopItem = async params => await iSetTopItem(params)

const changeShowStatus = async params => await iChangeShowStatus(params)

const deleteSesitive = async params => await iDeleteSesitive(params)

const saveSesitive = async({ sensitiveWordsId, ...params }) => {
  if (sensitiveWordsId) {
    return await iUpdateSesitive({ sensitiveWordsId, ...params })
  } else {
    return await iAddSesitive(params)
  }
}

export {
  getSesitiveList,
  saveSesitive,
  getList,
  getItem,
  getTodayNum,
  deleteComment,
  deleteComments,
  setTopItem,
  approveItem,
  saveItem,
  changeShowStatus,
  getListInArticle,
  getSensitiveOptions,
  deleteSesitive
}
