import request from "../request";
import { AxiosPromise } from "axios";

export class CollectionAndWrong {
  private urlList: {
    getList: string;
    add: string;
    deletes: string;
  };
  constructor(type: CollectionAndWrongType.type) {
    switch (type) {
      case "collection":
        this.urlList = {
          getList: "/student/question/collection/list",
          add: "/student/question/collection/collections",
          deletes: "/student/question/collection/",
        };
        break;
      case "wrong":
        this.urlList = {
          getList: "/student/question/wrong/list",
          add: "/student/question/wrong/wrongs",
          deletes: "/student/question/wrong/",
        };
        break;
    }
  }
  /**
   * 获取列表
   * @param params
   * @returns
   */
  getList = async (params: CollectionAndWrongType.ListParams) => {
    const res = await request({
      url: this.urlList.getList,
      params,
    });
    return <CollectionAndWrongType.ListRes>res.data;
  };

  /**
   * 批量新增问题至列表
   * @param params
   * @returns
   */
  adds(
    params: CollectionAndWrongType.AddCullectionsParams
  ): AxiosPromise<CollectionAndWrongType.AddCullectionsRes> {
    return request({
      url: this.urlList.add,
      data: params,
      method: "post",
    });
  }

  /**
   * 批量删除问题
   * @param ids
   */
  deletes = async (ids: number[]) => {
    const res = await request({
      url: this.urlList.deletes + ids,
      method: "delete",
    });
    return <CollectionAndWrongType.AddCullectionsRes>res.data;
  };
}

/**
 * 收藏题目接口列表
 */
export const collectionQuestion = new CollectionAndWrong("collection");

/**
 * 错题接口列表
 */
export const wrongQuestion = new CollectionAndWrong("wrong");
