"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProtocolsConfigure = void 0;
const time_js_1 = require("../utils/time.js");
const grant_authorization_js_1 = require("../core/grant-authorization.js");
const auth_js_1 = require("../core/auth.js");
const index_js_1 = require("../index.js");
const message_js_1 = require("../core/message.js");
const url_js_1 = require("../utils/url.js");
class ProtocolsConfigure extends message_js_1.Message {
    static parse(message) {
        return __awaiter(this, void 0, void 0, function* () {
            yield (0, auth_js_1.validateAuthorizationIntegrity)(message);
            ProtocolsConfigure.validateDefinitionNormalized(message.descriptor.definition);
            return new ProtocolsConfigure(message);
        });
    }
    static create(options) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const descriptor = {
                interface: message_js_1.DwnInterfaceName.Protocols,
                method: message_js_1.DwnMethodName.Configure,
                messageTimestamp: (_a = options.messageTimestamp) !== null && _a !== void 0 ? _a : (0, time_js_1.getCurrentTimeInHighPrecision)(),
                definition: ProtocolsConfigure.normalizeDefinition(options.definition)
            };
            const authorization = yield message_js_1.Message.signAsAuthorization(descriptor, options.authorizationSignatureInput, options.permissionsGrantId);
            const message = { descriptor, authorization };
            message_js_1.Message.validateJsonSchema(message);
            const protocolsConfigure = new ProtocolsConfigure(message);
            return protocolsConfigure;
        });
    }
    static validateDefinitionNormalized(definition) {
        const { protocol, types } = definition;
        // validate protocol url
        (0, url_js_1.validateProtocolUrlNormalized)(protocol);
        // validate schema url normalized
        for (const typeName in types) {
            const schema = types[typeName].schema;
            if (schema !== undefined) {
                (0, url_js_1.validateSchemaUrlNormalized)(schema);
            }
        }
    }
    static normalizeDefinition(definition) {
        const typesCopy = Object.assign({}, definition.types);
        // Normalize schema url
        for (const typeName in typesCopy) {
            const schema = typesCopy[typeName].schema;
            if (schema !== undefined) {
                typesCopy[typeName].schema = (0, url_js_1.normalizeSchemaUrl)(schema);
            }
        }
        return Object.assign(Object.assign({}, definition), { protocol: (0, url_js_1.normalizeProtocolUrl)(definition.protocol), types: typesCopy });
    }
    authorize(tenant, messageStore) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            // if author is the same as the target tenant, we can directly grant access
            if (this.author === tenant) {
                return;
            }
            else if (this.author !== undefined && ((_a = this.authorizationPayload) === null || _a === void 0 ? void 0 : _a.permissionsGrantId)) {
                yield grant_authorization_js_1.GrantAuthorization.authorizeGenericMessage(tenant, this, this.author, messageStore);
            }
            else {
                throw new index_js_1.DwnError(index_js_1.DwnErrorCode.ProtocolsConfigureUnauthorized, 'The ProtocolsConfigure failed authorization');
            }
        });
    }
}
exports.ProtocolsConfigure = ProtocolsConfigure;
//# sourceMappingURL=protocols-configure.js.map