import { itGetCached } from "@/fetchHelpers";
import { defineStore } from "pinia";
import type { Ref } from "vue";
import { computed, ref } from "vue";

type CockpitType = "mentor" | "expert" | null;

export const useCockpitStore = defineStore("cockpit", () => {
  const cockpitType: Ref<CockpitType> = ref(null);
  const isLoading = ref(false);

  const hasExpertCockpitType = computed(() => cockpitType.value === "expert");
  const hasMentorCockpitType = computed(() => cockpitType.value === "mentor");
  const hasNoCockpitType = computed(() => cockpitType.value === null);

  async function fetchCockpitType(courseId: string | null) {
    if (!courseId) {
      cockpitType.value = null;
      return;
    }

    isLoading.value = true;
    const url = `/api/course/${courseId}/cockpit/`;
    const response = await itGetCached(url);
    cockpitType.value = response.type;
    isLoading.value = false;
  }

  return {
    fetchCockpitType,
    hasExpertCockpitType,
    hasMentorCockpitType,
    hasNoCockpitType,
    isLoading,
    cockpitType,
  };
});
