import type { ObjectCategory, PermissionRight } from '@siop/shared';
import { useMe } from './use-auth';

/** Lecture UI de la matrice (l'API re-vérifie chaque requête, invariant R0). */
export function usePermissions() {
  const { data: me } = useMe();
  const can = (category: ObjectCategory, right: PermissionRight): boolean => {
    const entry = me?.permissions.find((p) => p.objectCategory === category);
    if (!entry) return false;
    switch (right) {
      case 'view': return entry.canView;
      case 'viewOther': return entry.canViewOther;
      case 'create': return entry.canCreate;
      case 'edit': return entry.canEdit;
      case 'delete': return entry.canDelete;
    }
  };
  return { can, chargé: !!me };
}
