import { defineStore } from "pinia";
import { ref } from "vue";
import { store } from "@/store";
import { getPermissions } from "@/api/common/login";

export const useBtnPermissionStore = defineStore("btnPermission", () => {
  const btnPermissions = ref<string[]>([]);

  const fetchPermissions = async () => {
    const res = await getPermissions();
    btnPermissions.value = res.data;
  }

  const hasPermission = (permission: string) => {
    return btnPermissions.value.includes(permission);
  }

  return {
    btnPermissions,
    fetchPermissions,
    hasPermission,
  };
});


/**
 * @description 用于在 setup 外使用 store
 */
export function useBtnPermissionStoreOutside() {
  return useBtnPermissionStore(store)
}
