import type { PaperDetail } from "@/types/doc.types";
import { defineStore } from "pinia";
import { ref, type Ref } from "vue";
import * as paperDetailService from "@/services/paperDetail.service"
import { useAuthStore } from "./auth.store";

export const useDocDetailStore = defineStore("docDetail", () => {
  const detail: Ref<PaperDetail | null> = ref(null);

  const pdfUrl: Ref<string | null> = ref(null);

  const authStore = useAuthStore();

  function fetch(id: number) {
    return paperDetailService.get(id).then((resp) => {
      detail.value = resp;
      const token = authStore.token;
      if (token) {
        pdfUrl.value = paperDetailService.pdfUrl(resp.filePath, token)
      }
    })
  }

  return {
    detail,
    pdfUrl,
    fetch,
  }
});
