// /stores/permissionStore.ts
import { defineStore } from "pinia";

export const usePermissionStore = defineStore("permission", {
  state: () => ({
    data: null as any,
  }),
  actions: {
    async load(path: string) {
      const parts = path.split("/").filter(Boolean);
      const group = parts[1] || "";
      const role = parts.at(-1)?.toLowerCase() || "";
      const config = useRuntimeConfig();
      const apiBase = config.public.verificationApiBaseUrl || 'http://10.10.123.140:8089/api/v1';
      const origin = new URL(apiBase).origin;
      const url = `${origin}/api/permission?roles=${role}&groups=${group}`;
      const { data } = await useFetch(url);
      this.data = data.value;
    },
    can(action: string) {
      return this.data?.[action] === true;
    },
  },
});
