import { itGet } from "@/fetchHelpers";
import type { MediaLibraryPage } from "@/types";
import log from "loglevel";
import { defineStore } from "pinia";

export type MediaLibraryStoreState = {
  mediaLibraryPage: MediaLibraryPage | undefined;
  selectedLearningPath: { id: number; name: string };
  availableLearningPaths: { id: number; name: string }[];
};

export const useMediaLibraryStore = defineStore({
  id: "mediaLibrary",
  state: () => {
    return {
      mediaLibraryPage: undefined,
      selectedLearningPath: { id: 1, name: "Alle Lehrgänge" },
      availableLearningPaths: [
        { id: 1, name: "Alle Lehrgänge" },
        { id: 2, name: "Versicherungsvermittler/-in" },
      ],
    } as MediaLibraryStoreState;
  },
  getters: {},
  actions: {
    async loadMediaLibraryPage(slug: string, reload = false) {
      if (this.mediaLibraryPage && !reload) {
        return this.mediaLibraryPage;
      }
      log.debug("load mediaLibraryPageData");
      const mediaLibraryPageData = await itGet(`/api/course/page/${slug}/`);

      if (!mediaLibraryPageData) {
        throw `No mediaLibraryPageData found with: ${slug}`;
      }

      this.mediaLibraryPage = mediaLibraryPageData;
      return this.mediaLibraryPage;
    },
  },
});
