import { http } from '../http'
import { only_last_id } from '../only-last'
import { get } from '../request'

const extractList = (res: any): any[] => {
  if (Array.isArray(res)) return res
  if (res && typeof res === 'object') {
    return res.records || res.rows || res.list || []
  }
  return []
}

const processMemoryTips = (tips: any) => {
  if (!tips) {
    return []
  }
  if (typeof tips === 'string') {
    try {
      const parsedTips = JSON.parse(tips)
      if (Array.isArray(parsedTips)) {
        return _processMemoryTipsArray(parsedTips)
      } else {
        return [parsedTips]
      }
    } catch (e) {
      return [tips]
    }
  } else if (Array.isArray(tips)) {
    return _processMemoryTipsArray(tips)
  } else {
    return [tips]
  }
}

const _processMemoryTipsArray = (memoryTips: any[]) => {
  return memoryTips.map(tip => {
    if (typeof tip === 'string') {
      return tip
    }
    return tip
  })
}

const wordTableApi = {
  /**
   * @description: 获取阶段筛选项
   * @param {*} params：
   * @return {*}
   */
  getWordTableStage: () => {
    return http.request({
      url: `/vocabulary/helpVocabulary`,
    })
  },

  /**
   * @description: 获取单词熟悉程度枚举
   * @param {*} params：
   * @return {*}
   */
  getWordTableFamiliarity: () => {
    return http.request({
      url: `/wordTable/graspEnum`,
    })
  },

  /**
   * @description: 获取阶段单词表
   * @param {WordTableStageParams} params：查询参数
   * @return {*}
   */
  getWordTableStageList: (params: WordTableStageParams) => {
    return http.request({
      url: `/wordTable/wordTableStage`,
      params,
      [only_last_id]: 'word-table-list',
    })
  },

  /**
   * @description: 获取教材单词表可用阶段
   * @param {*} params：
   * @return {*}
   */
  getTextbookStage: () => {
    return http.request({
      url: `/textbookStage/wordTableUseList`,
    })
  },

  /**
   * @description: 获取教材单词表版本
   * @param {*} params：
   * @return {*}
   */
  getTextbookVersion: async (stageId: any): Promise<any[]> => {
    return await get(`/textbookVersion/list/${stageId}`)
  },

  /**
   * @description: 获取教材单词表教材
   * @param {*} params：
   * @return {*}
   */
  getTextbookList: async (versionId: any): Promise<any[]> => {
    return await get(`/textbook/list?versionId=${versionId ?? ''}`)
  },

  getXkwStageList: async (): Promise<any[]> => {
    const res = await get<any>(`/xkw/stage/list`, { size: -1, status: 0 })
    return extractList(res)
  },

  getXkwCourseList: async (stageId: any): Promise<any[]> => {
    const res = await get<any>(`/xkw/course/list`, { size: -1, status: 0, stageId })
    return extractList(res)
  },

  getXkwTextbookVersionList: async (courseId: any): Promise<any[]> => {
    const res = await get<any>(`/xkw/textbookVersion/list`, { size: -1, status: 0, courseId })
    return extractList(res)
  },

  getXkwTextbookList: async (versionId: any): Promise<any[]> => {
    const res = await get<any>(`/xkw/textbook/list`, { size: -1, status: 0, versionId })
    return extractList(res)
  },

  getXkwTextbookCatalogList: async (textbookId: any): Promise<any[]> => {
    const res = await get<any>(`/xkw/textbookCatalog/list`, { parentId: 0, textbookId, size: -1 })
    return extractList(res)
  },

  /**
   * @description: 获取教材单词表章节
   * @param {*} params：
   * @return {*}
   */
  getChapterList: async (textbookId: any): Promise<any[]> => {
    return await get(`/textbookChapter/data/list`, { textbookId })
  },

  /**
   * @description: 获取教材单词表教材
   * @param {TextbookWordParams} params：
   * @return {*}
   */
  getTextbookWord: (params: TextbookWordParams) => {
    return http.request({
      url: `/wordTable/wordTableTextbook`,
      params,
      [only_last_id]: 'word-table-list',
    })
  },

  /**
   * @description: 获取教材单词表教材
   * @param {WordCardDetailParams} params：
   * @return {*}
   */
  getWordCardDetail: async (params: WordCardDetailParams): Promise<any> => {
    const data = (await get(`/wordTable/wordDetail`, params)) as any
    data.memoryTips = JSON.parse(data.memoryTips)
    processMemoryTips(data.memoryTips)
    return data
  },

  /**
   * @description: 记录用户单词书筛选条件
   * @param {UserWordFilterData} data：
   * @return {*}
   */
  saveUserWordFilterRecord: (data: UserWordFilterData) => {
    return http.request({
      url: `/sysUser/setConfigInfo`,
      data,
      method: 'POST',
    })
  },

  /**
   * @description: 获取用户单词书筛选条件
   * @param {UserWordFilterParams} params：
   * @return {*}
   */
  getUserWordFilterRecord: (params: UserWordFilterParams) => {
    return http.request({
      url: `/sysUser/getConfigInfo`,
      params,
    })
  },
}

interface WordTableStageParams {
  current?: number
  size?: number
  [key: string]: any
}

interface TextbookWordParams {
  textbookId?: string
  chapterId?: string
  [key: string]: any
}

interface WordCardDetailParams {
  wordId: string
  [key: string]: any
}

interface UserWordFilterData {
  textbookId?: string
  chapterId?: string
  familiarityId?: string
  [key: string]: any
}

interface UserWordFilterParams {
  textbookId?: string
  [key: string]: any
}

export default wordTableApi
export type {
  WordTableStageParams,
  TextbookWordParams,
  WordCardDetailParams,
  UserWordFilterData,
  UserWordFilterParams,
}
