"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebHookWebId = void 0;
const n3_1 = require("n3");
const OkResponseDescription_1 = require("../../../http/output/response/OkResponseDescription");
const BasicRepresentation_1 = require("../../../http/representation/BasicRepresentation");
const ContentTypes_1 = require("../../../util/ContentTypes");
const ErrorUtil_1 = require("../../../util/errors/ErrorUtil");
const PathUtil_1 = require("../../../util/PathUtil");
const OperationHttpHandler_1 = require("../../OperationHttpHandler");
/**
 * The WebHookSubscription2021 requires the server to have a WebID
 * that is used during the generation of the DPoP headers.
 * There are no real specifications about what this should contain or look like,
 * so we just return a Turtle document that contains a solid:oidcIssuer triple for now.
 * This way we confirm that our server was allowed to sign the token.
 */
class WebHookWebId extends OperationHttpHandler_1.OperationHttpHandler {
    constructor(baseUrl) {
        super();
        this.turtle = `@prefix solid: <http://www.w3.org/ns/solid/terms#>.
<> solid:oidcIssuer <${(0, PathUtil_1.trimTrailingSlashes)(baseUrl)}>.`;
        // This will throw an error if something is wrong with the issuer URL
        const parser = new n3_1.Parser();
        try {
            parser.parse(this.turtle);
        }
        catch (error) {
            throw new Error(`Invalid issuer URL: ${(0, ErrorUtil_1.createErrorMessage)(error)}`);
        }
    }
    async handle(input) {
        const representation = new BasicRepresentation_1.BasicRepresentation(this.turtle, input.operation.target, ContentTypes_1.TEXT_TURTLE);
        return new OkResponseDescription_1.OkResponseDescription(representation.metadata, representation.data);
    }
}
exports.WebHookWebId = WebHookWebId;
//# sourceMappingURL=WebHookWebId.js.map