import type { ComputedRef } from 'vue';
import { useAccountStore } from '@/stores';
import { storeToRefs } from 'pinia';

export function usePermission(code: string | string[], not = false): ComputedRef<boolean> {
  const store = useAccountStore();
  const { permission } = storeToRefs(store);

  const codes = Array.isArray(code) ? code : code.split('|');
  return computed(() => {
    if (!permission.value.length || !codes.length) return true;
    const value = codes.some((value) => permission.value.includes(value));
    return not ? !value : value;
  });
}
