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());
    });
};
import { getCurrentTimeInHighPrecision } from '../utils/time.js';
// import { GrantAuthorization } from '../core/grant-authorization.js';
import { validateAuthorizationIntegrity } from '../core/auth.js';
// import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
import { DwnInterfaceName, DwnMethodName, Message } from '../core/message.js';
import { normalizeProtocolUrl, normalizeSchemaUrl, validateProtocolUrlNormalized, validateSchemaUrlNormalized } from '../utils/url.js';
export class ProtocolsConfigure extends Message {
    static parse(message) {
        return __awaiter(this, void 0, void 0, function* () {
            yield 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: DwnInterfaceName.Protocols,
                method: DwnMethodName.Configure,
                messageTimestamp: (_a = options.messageTimestamp) !== null && _a !== void 0 ? _a : getCurrentTimeInHighPrecision(),
                definition: ProtocolsConfigure.normalizeDefinition(options.definition)
            };
            const authorization = yield Message.signAsAuthorization(descriptor, options.authorizationSignatureInput, options.permissionsGrantId);
            const message = { descriptor, authorization };
            Message.validateJsonSchema(message);
            const protocolsConfigure = new ProtocolsConfigure(message);
            return protocolsConfigure;
        });
    }
    static validateDefinitionNormalized(definition) {
        const { protocol, types } = definition;
        // validate protocol url
        validateProtocolUrlNormalized(protocol);
        // validate schema url normalized
        for (const typeName in types) {
            const schema = types[typeName].schema;
            if (schema !== undefined) {
                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 = normalizeSchemaUrl(schema);
            }
        }
        return Object.assign(Object.assign({}, definition), { protocol: normalizeProtocolUrl(definition.protocol), types: typesCopy });
    }
}
//# sourceMappingURL=protocols-configure.js.map