import {
  iGetList,
  iGetItem,
  iGetNotUsed,
  iUpdateItem,
  iAddItem,
  iGetArticleList,
  iDeleteTag,
  iDeleteTags,
  iDeleteTagInArticle,
  iDeleteTagsInArticle,
  iChangeShowStatus,
  iGetTagList
} from '@/api/tag'

import listResult from '@/model/listResult'

import MODULETYPE from '@/const/moduleType'
import CARRIERKEY from '@/const/carrierKey'

import { translateLabel } from '@/utils'

const getList = async data => listResult(await iGetList(data), x => ({
  id: x.id,
  tagType: translateLabel(MODULETYPE.TAG_MODAL_TYPE, x.moduleType),
  usedStatus: x.used ? '已使用' : '未使用',
  tagName: x.name,
  usedNum: x.used ? x.used : 0,
  isShow: x.isShow,
  showStatus: x.isShow ? '展示中' : '未展示'
}))

const getItem = async params => {
  const result = await iGetItem(params)
  if (result.success) {
    const data = result.data
    result.data = {
      id: data.id,
      tagType: translateLabel(MODULETYPE.TAG_MODAL_TYPE, data.moduleType),
      usedStatusF: '', // data.used ? '已使用' : '未使用',
      tagName: data.name,
      usedNum: data.used ? data.used : 0,
      showStatusF: data.isShow ? '展示中' : '未展示'
    }
  }
  return result
}

const getNotUsed = async params => await iGetNotUsed(params)

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

const getArticleList = async data => listResult(await iGetArticleList(data), x => ({
  id: x.id,
  tagRelationId: x.relationId,
  contentType: translateLabel(CARRIERKEY.CARRIER, x.contentType),
  title: x.title,
  isReleaseF: x.isRelease ? '已发布' : '未发布',
  isFeatureF: x.isFeature ? '是' : '否'
}))

const deleteTag = async params => await iDeleteTag(params)

const deleteTags = async params => await iDeleteTags(params)

const deleteTagInArticle = async params => await iDeleteTagInArticle(params)

const deleteTagsInArticle = async params => await iDeleteTagsInArticle(params)

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

const getTagList = async params => {
  const result = await iGetTagList(params)
  if (result.success) {
    const data = result.data
    result.data = data.map(x => ({
      value: x.id,
      label: x.name
    }))
  }
  return result
}

export {
  getList,
  getItem,
  getNotUsed,
  saveItem,
  getArticleList,
  deleteTag,
  deleteTags,
  deleteTagInArticle,
  deleteTagsInArticle,
  changeShowStatus,
  getTagList
}
