import { defineStore } from "pinia";
import { ref } from "vue";



const AUTH_PAGE_KEY = "pccv_auth_page";

const useUserInfoStore = defineStore("userInfo", () => {
  const user = ref({
    token: "",
    expires: 0,
  });

  // 初始化時從 sessionStorage 載入
  const storedAuthPage = sessionStorage.getItem(AUTH_PAGE_KEY);
  const auth_page = ref(storedAuthPage ? JSON.parse(storedAuthPage) : []);

  const updateAuthPage = (data) => {
    auth_page.value = data;
    sessionStorage.setItem(AUTH_PAGE_KEY, JSON.stringify(data));
  };

  // 提供一個清除權限的方法（例如登出時用）
  const clearAuthPage = () => {
    auth_page.value = [];
    sessionStorage.removeItem(AUTH_PAGE_KEY);
  };

  return { user, auth_page, updateAuthPage, clearAuthPage };
});

export default useUserInfoStore;
