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 MessagesQuery 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 MessagesQuery(message);
    }
    /**
     * Creates a MessagesQuery message.
     *
     * @throws {DwnError} if json schema validation fails.
     */
    static async create(options) {
        const permissionGrantInvocation = Message.normalizePermissionGrantInvocation({
            permissionGrantIds: options.permissionGrantIds
        });
        const descriptor = {
            interface: DwnInterfaceName.Messages,
            method: DwnMethodName.Query,
            filters: options.filters ?? [],
            messageTimestamp: options.messageTimestamp ?? Time.getCurrentTimestamp(),
            cursor: options.cursor,
            limit: options.limit,
            cidsOnly: options.cidsOnly,
            ...permissionGrantInvocation,
        };
        removeUndefinedProperties(descriptor);
        const authorization = await Message.createAuthorization({
            descriptor,
            signer: options.signer,
            ...permissionGrantInvocation,
        });
        const message = { descriptor, authorization };
        Message.validateJsonSchema(message);
        return new MessagesQuery(message);
    }
}
//# sourceMappingURL=messages-query.js.map