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 { Cid } from '../utils/cid.js';
import { Time } from '../utils/time.js';
import { validateMessageSignatureIntegrity } from '../core/auth.js';
import { DwnError, DwnErrorCode } from '../index.js';
import { DwnInterfaceName, DwnMethodName, Message } from '../core/message.js';
export class MessagesGet extends Message {
    static parse(message) {
        return __awaiter(this, void 0, void 0, function* () {
            Message.validateJsonSchema(message);
            this.validateMessageCids(message.descriptor.messageCids);
            yield validateMessageSignatureIntegrity(message.authorization.signature, message.descriptor);
            Time.validateTimestamp(message.descriptor.messageTimestamp);
            return new MessagesGet(message);
        });
    }
    static create(options) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const descriptor = {
                interface: DwnInterfaceName.Messages,
                method: DwnMethodName.Get,
                messageCids: options.messageCids,
                messageTimestamp: (_a = options === null || options === void 0 ? void 0 : options.messageTimestamp) !== null && _a !== void 0 ? _a : Time.getCurrentTimestamp(),
            };
            const authorization = yield Message.createAuthorization(descriptor, options.signer);
            const message = { descriptor, authorization };
            Message.validateJsonSchema(message);
            MessagesGet.validateMessageCids(options.messageCids);
            return new MessagesGet(message);
        });
    }
    /**
     * validates the provided cids
     * @param messageCids - the cids in question
     * @throws {DwnError} if an invalid cid is found.
     */
    static validateMessageCids(messageCids) {
        for (const cid of messageCids) {
            try {
                Cid.parseCid(cid);
            }
            catch (_) {
                throw new DwnError(DwnErrorCode.MessageGetInvalidCid, `${cid} is not a valid CID`);
            }
        }
    }
}
//# sourceMappingURL=messages-get.js.map