export function usePermissions() {
    const auth = useAuthStore()

    function isAdmin(): boolean {
        return auth.user?.roles?.includes('ROLE_ADMIN') ?? false
    }

    function can(code: string): boolean {
        if (!auth.user) {
            return false
        }
        if (isAdmin()) {
            return true
        }
        return auth.user.effectivePermissions?.includes(code) ?? false
    }

    function canAny(codes: string[]): boolean {
        return codes.some((c) => can(c))
    }

    function canAll(codes: string[]): boolean {
        return codes.every((c) => can(c))
    }

    return { can, canAny, canAll, isAdmin }
}
