import {
  useCourseDataWithCompletion,
  useCourseSessionDetailQuery,
} from "@/composables";
import { useExpertCockpitStore } from "@/stores/expertCockpit";
import { ref } from "vue";

export function useExpertCockpitPageData(courseSlug: string) {
  const loading = ref(true);

  const cockpitStore = useExpertCockpitStore();
  const courseSessionDetailResult = useCourseSessionDetailQuery();

  async function loadData() {
    loading.value = true;
    await courseSessionDetailResult.waitForData();
    await cockpitStore.loadCircles(
      courseSlug,
      courseSessionDetailResult.findCurrentUser()
    );

    const userDataPromises = courseSessionDetailResult.filterMembers().map((m) => {
      const completionData = useCourseDataWithCompletion(courseSlug, m.id);
      return completionData.resultPromise;
    });

    await Promise.all(userDataPromises);
    loading.value = false;
  }

  loadData();

  return {
    loading,
  };
}
