import { getCourseList, getCourseChapterList } from '@/api/base'

interface Semester {
  id: string
  name: string
}

interface Course {
  id: string
  course_id: string
  name: string
  cover: string
  course_alias_name: string
  semester: Semester
}

interface Chapter {
  id: string
  name: string
}

export function useGetCourseList() {
  const courseValue = ref('')
  const courses = ref<Course[]>([])
  const chapters = ref<Chapter[]>([])
  // 获取课程列表
  function getCourses() {
    getCourseList().then(res => {
      courses.value = res.data.items
    })
  }
  // 获取章节列表
  function getChapters() {
    if (!courseValue.value) {
      chapters.value = []
      return
    }
    getCourseChapterList({ course_id: courseValue.value }).then(res => {
      chapters.value = res.data.items
    })
  }

  getCourses()

  watch(courseValue, () => {
    getChapters()
  })

  return { courses, courseValue, chapters }
}
