"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PolicyEngineReader = void 0;
const global_logger_factory_1 = require("global-logger-factory");
const IdentifierMap_1 = require("../util/map/IdentifierMap");
const PermissionReader_1 = require("./PermissionReader");
/**
 * A {@link PermissionReader} that uses a {@link PolicyEngine} to determine the available permissions.
 */
class PolicyEngineReader extends PermissionReader_1.PermissionReader {
    logger = (0, global_logger_factory_1.getLoggerFor)(this);
    engine;
    constructor(engine) {
        super();
        this.engine = engine;
    }
    async handle(input) {
        const credentials = {
            agent: input.credentials.agent?.webId,
            client: input.credentials.client?.clientId,
            issuer: input.credentials.issuer?.url,
        };
        const result = new IdentifierMap_1.IdentifierMap();
        for (const identifier of input.requestedModes.distinctKeys()) {
            const permissions = await this.engine.getPermissions(identifier.path, credentials, [...input.requestedModes.get(identifier)]);
            result.set(identifier, permissions);
        }
        return result;
    }
}
exports.PolicyEngineReader = PolicyEngineReader;
//# sourceMappingURL=PolicyEngineReader.js.map