import { AbstractMessage } from '../core/abstract-message.js';
import { Message } from '../core/message.js';
import { removeUndefinedProperties } from '@enbox/common';
import { Time } from '../utils/time.js';
import { validateProtocolUrlNormalized } from '../utils/url.js';
import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
export class MessagesSubscribe extends AbstractMessage {
    static async parse(message) {
        Message.validateJsonSchema(message);
        await Message.validateSignatureStructure(message.authorization.signature, message.descriptor);
        for (const filter of message.descriptor.filters) {
            if ('protocol' in filter && filter.protocol !== undefined) {
                validateProtocolUrlNormalized(filter.protocol);
            }
        }
        Time.validateTimestamp(message.descriptor.messageTimestamp);
        return new MessagesSubscribe(message);
    }
    /**
     * Creates a MessagesSubscribe message.
     *
     * @throws {DwnError} if json schema validation fails.
     */
    static async create(options) {
        const currentTime = Time.getCurrentTimestamp();
        const permissionGrantInvocation = Message.normalizePermissionGrantInvocation({
            permissionGrantIds: options.permissionGrantIds
        });
        const descriptor = {
            interface: DwnInterfaceName.Messages,
            method: DwnMethodName.Subscribe,
            filters: options.filters ?? [],
            messageTimestamp: options.messageTimestamp ?? currentTime,
            cursor: options.cursor,
            ...permissionGrantInvocation,
        };
        removeUndefinedProperties(descriptor);
        const { signer } = options;
        const authorization = await Message.createAuthorization({
            descriptor,
            signer,
            ...permissionGrantInvocation
        });
        const message = { descriptor, authorization };
        Message.validateJsonSchema(message);
        return new MessagesSubscribe(message);
    }
}
//# sourceMappingURL=messages-subscribe.js.map