import subjectApi from '@/api/subject/subject'

export const shareStore = defineStore('share', () => {
  const initPerson = {
    userId: '',
    subjectId: '',
  }
  // 任务设置了单个对象时的id
  const taskSinglePerson = ref(initPerson)
  const taskSinglePersonBookIds = ref([] as any[])

  // 监听任务信息改变
  watch(
    () => taskSinglePerson.value,
    async newVal => {
      if (newVal.userId && newVal.subjectId) {
        taskSinglePersonBookIds.value = (
          (
            await subjectApi.getUserTextBookList({
              subjectId: newVal.subjectId,
              userId: newVal.userId,
            })
          )?.rows || []
        ).map((b: any) => b.textBookId)
      } else {
        taskSinglePersonBookIds.value = []
      }
    },
    { deep: true },
  )

  const $reset = () => {
    taskSinglePerson.value = initPerson
    taskSinglePersonBookIds.value = []
  }

  return {
    $reset,
    taskSinglePerson,
    taskSinglePersonBookIds,
  }
})
