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

const courseTeacherApi = {
  list, add, remove, update
}

export default courseTeacherApi

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

  return processResponse(response)
}

/**
 *
 * @param courseId {number}
 * @param teacherId {number}
 * @returns {Promise<*>}
 */
async function add(courseId, teacherId) {
  const data = {
    teacher: teacherId
  }

  const response = await client.post(apiUrls.admin.course.courseTeacher.add, qs.stringify(data), {
    params: {
      course: courseId,
    }
  })

  return processResponse(response)
}

/**
 *
 * @param courseId {number}
 * @param teachers {number[]}
 * @returns {Promise<*>}
 */
async function update(courseId, teachers) {
  const data = {
    teachers: teachers
  }

  const response = await client.post(apiUrls.admin.course.courseTeacher.update, data, {
    params: {
      course: courseId,
    }
  })

  return processResponse(response)
}

/**
 *
 * @param courseId {number}
 * @param teacherId {number}
 * @returns {Promise<*>}
 */
async function remove(courseId, teacherId) {
  const data = {
    teachers: teacherId
  }

  const response = await client.post(apiUrls.admin.course.courseTeacher.remove, qs.stringify(data), {
    params: {
      course: courseId,
    }
  })

  return processResponse(response)
}
