import { del, get, post, put } from '../../http'

type Word = {
  id: any
  spell: string
}

/**
 * @description: 获取单词列表
 * @param {*} params
 * @return {*}
 */
export const getWordEditingList = async (params: Record<string, any>) => {
  return await get<any>('/word/adminList', params)
}

export async function getWordBySpellApi(spell: string): Promise<Word> {
  const data: any = await get<any>('/word/adminList', { spell })
  if (!data.rows?.length) return undefined as any
  for (const word of data.rows) {
    if (word.spell === spell) return word
  }
  return undefined as any
}

/**
 * @description: 获取单词详情
 * @param {*} params
 * @return {*}
 */
export const getWordDetail = async (params: Record<string, any>) => {
  return await get<any>(`word/${params.wordId}`, params)
}

export async function getWordDetailApi(wordId: string): Promise<any> {
  return await get<any>(`/word/${wordId}`)
}

/**
 * @description:更新释义
 * @param {*} data
 * @return {*}
 */
export const updateMeaning = async (data: Record<string, any>) => {
  return await put('/meaning', data)
}

/**
 * @description:将单词加入关卡
 * @param {*} data
 * @return {*}
 */
export const forceMoveWordToLevel = async (data: Record<string, any>) => {
  return await post<any>('/level/levelWord/forceMoveWordToLevel', data)
}

// 替换单词
type ForceReplaceLevelWordApi = {
  levelId: any
  originMeaningId?: any
  targetMeaningId?: any
}
export async function forceReplaceLevelWordApi(data: ForceReplaceLevelWordApi) {
  await post<any>('/level/levelWord/forceReplace', data)
}

/**
 * @description:更新例句
 * @param {*} data
 * @return {*}
 */
export const updateSentence = async (data: Record<string, any>) => {
  return await put('/sentence', data)
}

/**
 * @description:新增例句
 * @param {*} data
 * @return {*}
 */
export const addSentence = async (data: Record<string, any>) => {
  return await post<any>('/sentence', data)
}

/**
 * @description: 删除例句
 * @param {*} data
 * @return {*}
 */
export const deleteSentence = async (id: string) => {
  return await del(`/sentence/${id}`)
}

/**
 * @description: 删除例句
 * @param {*} data
 * @return {*}
 */
export const moveImage = async (data: Record<string, any>) => {
  return await post<any>('/word/moveImage', data)
}

/**
 * @description: 例句来源
 * @param {*} params
 * @return {*}
 */
export const getOriginList = async () => {
  return await get<any>('/sentence/originList')
}

/**
 * @description: 获取
 * @param {*} params
 * @return {*}
 */
export const getVocabularyList = async (params: Record<string, any>) => {
  return await get<any>('/vocabulary/list', params)
}

/**
 * @description:单词修改
 * @param {*} data
 * @return {*}
 */
export const updateWord = async (data: Record<string, any>) => {
  return await put('/word', data)
}

/**
 * @description:
 * @param {*} data
 * @return {*}
 */
export const uploadUrl = async (data: Record<string, any>) => {
  return await post<any>('/sysFileInfo/tenUploadUrl', data)
}

/**
 * @description: 单词助记表-查询列表
 * @param {*} params
 * @return {*}
 */
export const getAssistMemoryList = async (
  wordId: string,
  meaningId: string,
) => {
  return await get<any>(`/assistMemory/queryByWordId/${wordId}/${meaningId}`)
}

/**
 * @description:单词助记表-新增
 * @param {*} data
 * @return {*}
 */
export const postAssistMemory = async (data: Record<string, any>) => {
  return await post<any>('/assistMemory', data)
}

/**
 * @description:单词助记表-保存或更新
 * @param {*} data
 * @return {*}
 */
export const putAssistMemory = async (data: Record<string, any>) => {
  return await put('/assistMemory', data)
}

/**
 * @description:单词助记表-通过id删除
 * @param {*} data
 * @return {*}
 */
export const detailAssistMemory = async (id: string) => {
  return await del(`/assistMemory/${id}`)
}

/**
 * @description:单词编辑-例句-所属释义的单词变形
 * @param {*} data
 * @return {*}
 */
export const updateWordForm = async (data: Record<string, any>) => {
  return await post<any>('/sentence/updateWordForm', data)
}

/**
 * @description:单词编辑-图片搜索
 * @param {*} data
 * @return {*}
 */
export const picSearchByKeyWord = async (data: Record<string, any>) => {
  return await post<any>('/word/picSearchByKeyWord', data)
}

/**
 * @description:单词编辑-添加单词
 * @param {*} data
 * @return {*}
 */
export const postWordSaveApi = async (data: Record<string, any>) => {
  return await post<any>('/word/save', data)
}

export async function addWordApi(data: any) {
  return await post<any>('/word/save', data)
}

export async function removeWordLinkApi(ids: any) {
  if (!Array.isArray(ids)) {
    ids = [ids]
  }
  return await post<any>('/word/removeLink', ids)
}

/**
 * @description:单词编辑-修改单词
 * @param {*} data
 * @return {*}
 */
export const putWordSaveApi = async (data: Record<string, any>) => {
  return await put('/word/edit', data)
}

/**
 * @description:单词编辑-添加单词-获取形式下拉框
 * @param {*} data
 * @return {*}
 */
export const getSpinnerListApi = async () => {
  return await get<any>('/word/form/spinner')
}

/**
 * @description:单词编辑-添加单词-获取子词性下拉框
 * @param {*} data
 * @return {*}
 */
export const getSonSpinnerListApi = async () => {
  return await get<any>('/word/subType/spinner')
}

/**
 * @description:单词编辑-添加单词-获取词性下拉框
 * @param {*} data
 * @return {*}
 */
export const getSpinnerTypeApi = async () => {
  return await get<any>('/word/type/spinner')
}

/**
 * @description:单词编辑-搜索功能
 * @param {*} data
 * @return {*}
 */
export const searchWordAdminApi = async (params: Record<string, any>) => {
  return await get<any>('/word/adminSearch', params)
}

/**
 * @description:单词删除
 * @param {*} data
 * @return {*}
 */
export const deleteWordApi = async (params: Record<string, any>) => {
  return await del('/word/delete', params)
}
