import apis from "~/apis/permitApi";

export const usePermitStore = defineStore("permitStore", {
  // persist: true,
  state: () => ({
    projects: undefined,
    project: undefined,
  }),
  getters: {
    projectsGetter(state) {
      return state.projects;
    },
    projectGetter(state) {
      if (!state.project)
        state.project = JSON.parse(localStorage.getItem("project") as string);
      return state.project;
    },
  },
  actions: {
    projectsSetter(projects) {
      this.projects = projects;
    },
    SET_PROJECT(project) {
      this.project = project;
      localStorage.setItem("project", JSON.stringify(project));
    },
    async getProjects() {
      try {
        const { $api } = useNuxtApp();
        const res = await $api(apis.projects.list, {
          baseURL: import.meta.env.VITE_AUTH_BASE_URL,
          method: "POST",
        });
        this.projectsSetter(res.data);
        return res;
      } catch (err) {
      }
    },
  },
});
