import { AbstractMessage } from '../core/abstract-message.js';
import { Cid } from '../utils/cid.js';
import { Message } from '../core/message.js';
import { Time } from '../utils/time.js';
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
export class MessagesRead extends AbstractMessage {
    static async parse(message) {
        Message.validateJsonSchema(message);
        this.validateMessageCid(message.descriptor.messageCid);
        await Message.validateSignatureStructure(message.authorization.signature, message.descriptor);
        Time.validateTimestamp(message.descriptor.messageTimestamp);
        return new MessagesRead(message);
    }
    static async create(options) {
        const { signer } = options;
        const permissionGrantInvocation = Message.normalizePermissionGrantInvocation({
            permissionGrantIds: options.permissionGrantIds
        });
        const descriptor = {
            interface: DwnInterfaceName.Messages,
            method: DwnMethodName.Read,
            messageCid: options.messageCid,
            messageTimestamp: options.messageTimestamp ?? Time.getCurrentTimestamp(),
            ...permissionGrantInvocation,
        };
        const authorization = await Message.createAuthorization({
            descriptor,
            signer,
            ...permissionGrantInvocation,
        });
        const message = { descriptor, authorization };
        Message.validateJsonSchema(message);
        MessagesRead.validateMessageCid(options.messageCid);
        return new MessagesRead(message);
    }
    /**
     * validates the provided cid
     * @param messageCid - the cid in question
     * @throws {DwnError} if an invalid cid is found.
     */
    static validateMessageCid(messageCid) {
        try {
            Cid.parseCid(messageCid);
        }
        catch {
            throw new DwnError(DwnErrorCode.MessagesReadInvalidCid, `${messageCid} is not a valid CID`);
        }
    }
}
//# sourceMappingURL=messages-read.js.map