import { AccessDetermination, ArchonResourceId, Resource, ResourceIntent, ResourceIntentName } from "../types/policy.js";
import { Session, SessionId } from "../types/user.js";
import determineAccess from "./determineAccess.js";

/**
 * Given a list of ARIDs, determine whether the current user session has access to each resource. 
 * Good for checking access to multiple resources at once, like when rendering a list of resources from a database.
 * @param session The session of the user attempting to access the resources.
 * @param intent The intent with which the user is attempting to access the resources.
 * @param resources The list of resources to check access for.
 * @returns A map of ARIDs to whether the user has access to each resource.
 */
export default async function bulkDetermineAccess(session: Session | SessionId, intent: ResourceIntent | ResourceIntentName, resources: Resource[] | ArchonResourceId[]): Promise<Map<ArchonResourceId, AccessDetermination>> {
    const map = new Map<ArchonResourceId, AccessDetermination>();
    await Promise.all(resources.map(async resource => {
        const arid = typeof resource === "string" ? resource : resource.arid;
        const det = await determineAccess(
            session,
            arid,
            intent
        )
        map.set(arid, det);
    }));
    return map;
}