"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReadDeleteReader = void 0;
const policy_engine_1 = require("@solidlab/policy-engine");
const IdentifierMap_1 = require("../util/map/IdentifierMap");
const PermissionReader_1 = require("./PermissionReader");
/**
 * When trying to delete a non-existent resource, the server needs to return a different result
 * based on the read permissions on the resource or its parent container.
 * This {@link PermissionReader} makes sure read permissions on a resource and its parent container
 * are checked in such cases.
 */
class ReadDeleteReader extends PermissionReader_1.PermissionReader {
    reader;
    resourceSet;
    identifierStrategy;
    constructor(reader, resourceSet, identifierStrategy) {
        super();
        this.reader = reader;
        this.resourceSet = resourceSet;
        this.identifierStrategy = identifierStrategy;
    }
    async canHandle(input) {
        return this.reader.canHandle(input);
    }
    async handle(input) {
        const requestedModes = new IdentifierMap_1.IdentifierSetMultiMap(input.requestedModes);
        for (const identifier of requestedModes.distinctKeys()) {
            if (requestedModes.hasEntry(identifier, policy_engine_1.PERMISSIONS.Delete) && !await this.resourceSet.hasResource(identifier)) {
                requestedModes.add(identifier, policy_engine_1.PERMISSIONS.Read);
                if (!this.identifierStrategy.isRootContainer(identifier)) {
                    const parent = this.identifierStrategy.getParentContainer(identifier);
                    requestedModes.add(parent, policy_engine_1.PERMISSIONS.Read);
                }
            }
        }
        return this.reader.handle({ ...input, requestedModes });
    }
}
exports.ReadDeleteReader = ReadDeleteReader;
//# sourceMappingURL=ReadDeleteReader.js.map