"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.RecordsRead = void 0;
const time_js_1 = require("../utils/time.js");
const message_js_1 = require("../core/message.js");
const protocol_authorization_js_1 = require("../core/protocol-authorization.js");
const auth_js_1 = require("../core/auth.js");
const message_js_2 = require("../core/message.js");
class RecordsRead extends message_js_1.Message {
    static parse(message) {
        return __awaiter(this, void 0, void 0, function* () {
            if (message.authorization !== undefined) {
                yield (0, auth_js_1.validateAuthorizationIntegrity)(message);
            }
            const recordsRead = new RecordsRead(message);
            return recordsRead;
        });
    }
    /**
     * Creates a RecordsRead message.
     * @param options.recordId If `undefined`, will be auto-filled as a originating message as convenience for developer.
     * @param options.date If `undefined`, it will be auto-filled with current time.
     */
    static create(options) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const { recordId, authorizationSignatureInput } = options;
            const currentTime = (0, time_js_1.getCurrentTimeInHighPrecision)();
            const descriptor = {
                interface: message_js_2.DwnInterfaceName.Records,
                method: message_js_2.DwnMethodName.Read,
                recordId,
                messageTimestamp: (_a = options.date) !== null && _a !== void 0 ? _a : currentTime
            };
            // only generate the `authorization` property if signature input is given
            const authorization = authorizationSignatureInput ? yield message_js_1.Message.signAsAuthorization(descriptor, authorizationSignatureInput) : undefined;
            const message = { descriptor, authorization };
            message_js_1.Message.validateJsonSchema(message);
            return new RecordsRead(message);
        });
    }
    authorize(tenant, newestRecordsWrite, messageStore) {
        return __awaiter(this, void 0, void 0, function* () {
            const { descriptor } = newestRecordsWrite.message;
            // if author is the same as the target tenant, we can directly grant access
            if (this.author === tenant) {
                return;
            }
            else if (descriptor.published === true) {
                // authentication is not required for published data
                return;
            }
            else if (this.author !== undefined && this.author === descriptor.recipient) {
                // The recipient of a message may always read it
                return;
            }
            else if (descriptor.protocol !== undefined) {
                yield protocol_authorization_js_1.ProtocolAuthorization.authorize(tenant, this, this.author, messageStore);
            }
            else {
                throw new Error('message failed authorization');
            }
        });
    }
}
exports.RecordsRead = RecordsRead;
//# sourceMappingURL=records-read.js.map