"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OwnerPermissionReader = void 0;
const LogUtil_1 = require("../logging/LogUtil");
const ErrorUtil_1 = require("../util/errors/ErrorUtil");
const NotImplementedHttpError_1 = require("../util/errors/NotImplementedHttpError");
const IterableUtil_1 = require("../util/IterableUtil");
const IdentifierMap_1 = require("../util/map/IdentifierMap");
const PermissionReader_1 = require("./PermissionReader");
/**
 * Allows control access if the request is being made by the owner of the pod containing the resource.
 */
class OwnerPermissionReader extends PermissionReader_1.PermissionReader {
    constructor(accountStore, authStrategy, identifierStrategy) {
        super();
        this.logger = (0, LogUtil_1.getLoggerFor)(this);
        this.accountStore = accountStore;
        this.authStrategy = authStrategy;
        this.identifierStrategy = identifierStrategy;
    }
    async handle(input) {
        const result = new IdentifierMap_1.IdentifierMap();
        const requestedResources = input.requestedModes.distinctKeys();
        const auths = [...(0, IterableUtil_1.filter)(requestedResources, (id) => this.authStrategy.isAuxiliaryIdentifier(id))];
        if (auths.length === 0) {
            this.logger.debug(`No authorization resources found that need an ownership check.`);
            return result;
        }
        let podBaseUrl;
        try {
            podBaseUrl = await this.findPodBaseUrl(input.credentials);
        }
        catch (error) {
            this.logger.debug(`No pod owner Control permissions: ${(0, ErrorUtil_1.createErrorMessage)(error)}`);
            return result;
        }
        for (const auth of auths) {
            if (this.identifierStrategy.contains(podBaseUrl, auth, true)) {
                this.logger.debug(`Granting Control permissions to owner on ${auth.path}`);
                result.set(auth, {
                    read: true,
                    write: true,
                    append: true,
                    create: true,
                    delete: true,
                    control: true,
                });
            }
        }
        return result;
    }
    /**
     * Find the base URL of the pod the given credentials own.
     * Will throw an error if none can be found.
     */
    async findPodBaseUrl(credentials) {
        if (!credentials.agent?.webId) {
            throw new NotImplementedHttpError_1.NotImplementedHttpError('Only authenticated agents could be owners');
        }
        let settings;
        try {
            settings = await this.accountStore.getSettings(credentials.agent.webId);
        }
        catch {
            throw new NotImplementedHttpError_1.NotImplementedHttpError('No account registered for this WebID');
        }
        if (!settings.podBaseUrl) {
            throw new NotImplementedHttpError_1.NotImplementedHttpError('This agent has no pod on the server');
        }
        return { path: settings.podBaseUrl };
    }
}
exports.OwnerPermissionReader = OwnerPermissionReader;
//# sourceMappingURL=OwnerPermissionReader.js.map