import { useCourseSessionsStore } from "@/stores/courseSessions";
import type { CourseSession } from "@/types";
import log from "loglevel";
import type { ComputedRef } from "vue";
import { computed } from "vue";

export function useCurrentCourseSession() {
  /**
   * We often need the current course session in our components.
   * With this composable we can get it easily.
   */
  const store = useCourseSessionsStore();

  const result: ComputedRef<CourseSession> = computed(
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    () => {
      if (!store.currentCourseSession) {
        log.error(
          "currentCourseSession is only defined in pages with :courseSlug in the route"
        );
        throw new Error(
          `currentCourseSession is not defined in the store.
      It is only defined in pages with :courseSlug in the route`
        );
      }
      return store.currentCourseSession;
    }
  );
  return result;
}
