"use strict";
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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessagesGet = void 0;
const cid_js_1 = require("../utils/cid.js");
const time_js_1 = require("../utils/time.js");
const auth_js_1 = require("../core/auth.js");
const message_js_1 = require("../core/message.js");
class MessagesGet extends message_js_1.Message {
    static parse(message) {
        return __awaiter(this, void 0, void 0, function* () {
            message_js_1.Message.validateJsonSchema(message);
            this.validateMessageCids(message.descriptor.messageCids);
            yield (0, auth_js_1.validateAuthorizationIntegrity)(message);
            return new MessagesGet(message);
        });
    }
    static create(options) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const descriptor = {
                interface: message_js_1.DwnInterfaceName.Messages,
                method: message_js_1.DwnMethodName.Get,
                messageCids: options.messageCids,
                messageTimestamp: (_a = options === null || options === void 0 ? void 0 : options.messageTimestamp) !== null && _a !== void 0 ? _a : (0, time_js_1.getCurrentTimeInHighPrecision)(),
            };
            const authorization = yield message_js_1.Message.signAsAuthorization(descriptor, options.authorizationSignatureInput);
            const message = { descriptor, authorization };
            message_js_1.Message.validateJsonSchema(message);
            MessagesGet.validateMessageCids(options.messageCids);
            return new MessagesGet(message);
        });
    }
    /**
     * validates the provided cids
     * @param messageCids - the cids in question
     * @throws {Error} if an invalid cid is found.
     */
    static validateMessageCids(messageCids) {
        for (const cid of messageCids) {
            try {
                cid_js_1.Cid.parseCid(cid);
            }
            catch (_) {
                throw new Error(`${cid} is not a valid CID`);
            }
        }
    }
}
exports.MessagesGet = MessagesGet;
//# sourceMappingURL=messages-get.js.map