import {client, processFileDL, processResponse} from '../client';
import {apiUrls} from "app/src/api/api-urls";
import qs from "qs";

const courseSelectionApi = {
  list, listNotSelected, exportStudentExcel, exportNotSelectStudentExcel
}

export default courseSelectionApi

/**
 *
 * @param courseId {number}
 * @param term {string}
 * @returns {Promise<*>}
 */
async function list(courseId, term) {
  const response = await client.get(apiUrls.course.getSelectedStudents, {
    params: {
      course: courseId,
      term: term
    }
  })

  return processResponse(response)
}

/**
 *
 * @param courseId {number}
 * @param term {string}
 * @returns {Promise<*>}
 */
async function listNotSelected(courseId, term) {
  const response = await client.get(apiUrls.course.getNotSelectedStudents, {
    params: {
      course: courseId,
      term: term
    }
  })

  return processResponse(response)
}

const filenameRegex = /filename[^;=\n]*="((['"]).*?\2|[^;\n]*)"/;

async function exportStudentExcel(courseId, term) {
  const response = await client.get(apiUrls.course.exportStudents, {
    params: {
      course: courseId,
      term: term
    },
    responseType: "blob"
  })

  return processFileDL(response)
}

async function exportNotSelectStudentExcel(courseId, term) {
  const response = await client.get(apiUrls.course.exportNotSelectStudents, {
    params: {
      course: courseId,
      term: term
    },
    responseType: "blob"
  })

  return processFileDL(response)
}
