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 { authenticate } from '../../../core/auth.js';
import { deleteAllOlderMessagesButKeepInitialWrite } from '../records-interface.js';
import { MessageReply } from '../../../core/message-reply.js';
import { RecordsWrite } from '../messages/records-write.js';
import { DwnError, DwnErrorCode } from '../../../core/dwn-error.js';
import { DwnInterfaceName, Message } from '../../../core/message.js';
export class RecordsWriteHandler {
    constructor(didResolver, messageStore, dataStore, eventLog) {
        this.didResolver = didResolver;
        this.messageStore = messageStore;
        this.dataStore = dataStore;
        this.eventLog = eventLog;
    }
    handle({ tenant, message, options, dataStream }) {
        return __awaiter(this, void 0, void 0, function* () {
            let recordsWrite;
            try {
                recordsWrite = yield RecordsWrite.parse(message);
            }
            catch (e) {
                return MessageReply.fromError(e, 400);
            }
            // authentication & authorization
            try {
                yield authenticate(message.authorization, this.didResolver);
                yield recordsWrite.authorize(tenant, this.messageStore);
            }
            catch (e) {
                return MessageReply.fromError(e, 401);
            }
            // get existing messages matching the `recordId`
            const query = {
                interface: DwnInterfaceName.Records,
                recordId: message.recordId
            };
            const existingMessages = yield this.messageStore.query(tenant, query);
            // if the incoming write is not the initial write, then it must not modify any immutable properties defined by the initial write
            const newMessageIsInitialWrite = yield recordsWrite.isInitialWrite();
            if (!newMessageIsInitialWrite) {
                try {
                    const initialWrite = yield RecordsWrite.getInitialWrite(existingMessages);
                    RecordsWrite.verifyEqualityOfImmutableProperties(initialWrite, message);
                }
                catch (e) {
                    return MessageReply.fromError(e, 400);
                }
            }
            const newestExistingMessage = yield Message.getNewestMessage(existingMessages);
            let incomingMessageIsNewest = false;
            let newestMessage; // keep reference of newest message for pruning later
            if (newestExistingMessage === undefined || (yield Message.isNewer(message, newestExistingMessage))) {
                incomingMessageIsNewest = true;
                newestMessage = message;
            }
            else { // existing message is the same age or newer than the incoming message
                newestMessage = newestExistingMessage;
            }
            if (!incomingMessageIsNewest) {
                return new MessageReply({
                    status: { code: 409, detail: 'Conflict' }
                });
            }
            const isLatestBaseState = true;
            const indexes = yield constructRecordsWriteIndexes(recordsWrite, isLatestBaseState);
            try {
                // try to store data, unless options explicitly say to skip storage
                if (options === undefined || !options.skipDataStorage) {
                    yield this.putData(tenant, message, dataStream, newestExistingMessage);
                }
            }
            catch (error) {
                const e = error;
                if (e.code === DwnErrorCode.RecordsWriteMissingDataStream ||
                    e.code === DwnErrorCode.RecordsWriteMissingData ||
                    e.code === DwnErrorCode.RecordsWriteDataCidMismatch ||
                    e.code === DwnErrorCode.RecordsWriteDataSizeMismatch) {
                    return MessageReply.fromError(error, 400);
                }
                // else throw
                throw error;
            }
            yield this.messageStore.put(tenant, message, indexes);
            yield this.eventLog.append(tenant, yield Message.getCid(message));
            const messageReply = new MessageReply({
                status: { code: 202, detail: 'Accepted' }
            });
            // delete all existing messages that are not newest, except for the initial write
            yield deleteAllOlderMessagesButKeepInitialWrite(tenant, existingMessages, newestMessage, this.messageStore, this.dataStore, this.eventLog);
            return messageReply;
        });
    }
    ;
    /**
     * Puts the given data in storage unless tenant already has that data for the given recordId
     *
     * @throws {DwnError} with `DwnErrorCode.RecordsWriteMissingDataStream`
     *                    if `dataStream` is absent AND the `dataCid` does not match the current data for the given recordId
     * @throws {DwnError} with `DwnErrorCode.RecordsWriteMissingData`
     *                    if `dataStream` is absent AND dataStore does not contain the given `dataCid`
     * @throws {DwnError} with `DwnErrorCode.RecordsWriteDataCidMismatch`
     *                    if the data stream resulted in a data CID that mismatches with `dataCid` in the given message
     * @throws {DwnError} with `DwnErrorCode.RecordsWriteDataSizeMismatch`
     *                    if `dataSize` in `descriptor` given mismatches the actual data size
     */
    putData(tenant, message, dataStream, newestExistingMessage) {
        return __awaiter(this, void 0, void 0, function* () {
            let result;
            const messageCid = yield Message.getCid(message);
            if (dataStream === undefined) {
                // dataStream must be included if message contains a new dataCid
                if ((newestExistingMessage === null || newestExistingMessage === void 0 ? void 0 : newestExistingMessage.descriptor.dataCid) !== message.descriptor.dataCid) {
                    throw new DwnError(DwnErrorCode.RecordsWriteMissingDataStream, 'Data stream is not provided.');
                }
                const associateResult = yield this.dataStore.associate(tenant, messageCid, message.descriptor.dataCid);
                if (associateResult === undefined) {
                    throw new DwnError(DwnErrorCode.RecordsWriteMissingData, `Unable to associate dataCid ${message.descriptor.dataCid} ` +
                        `to messageCid ${messageCid} because dataStream was not provided and data was not found in dataStore`);
                }
                result = associateResult;
            }
            else {
                result = yield this.dataStore.put(tenant, messageCid, message.descriptor.dataCid, dataStream);
            }
            // verify that given dataSize matches size of actual data
            if (message.descriptor.dataSize !== result.dataSize) {
                // there is an opportunity to improve here: handle the edge case of if the delete fails...
                yield this.dataStore.delete(tenant, messageCid, message.descriptor.dataCid);
                throw new DwnError(DwnErrorCode.RecordsWriteDataSizeMismatch, `actual data size ${result.dataSize} bytes does not match dataSize in descriptor: ${message.descriptor.dataSize}`);
            }
            // verify that given dataCid matches CID of actual data
            if (message.descriptor.dataCid !== result.dataCid) {
                // there is an opportunity to improve here: handle the edge cae of if the delete fails...
                yield this.dataStore.delete(tenant, messageCid, message.descriptor.dataCid);
                throw new DwnError(DwnErrorCode.RecordsWriteDataCidMismatch, `actual data CID ${result.dataCid} does not match dataCid in descriptor: ${message.descriptor.dataCid}`);
            }
        });
    }
}
export function constructRecordsWriteIndexes(recordsWrite, isLatestBaseState) {
    return __awaiter(this, void 0, void 0, function* () {
        const message = recordsWrite.message;
        const descriptor = Object.assign({}, message.descriptor);
        delete descriptor.published; // handle `published` specifically further down
        const indexes = Object.assign(Object.assign({}, descriptor), { isLatestBaseState, published: !!message.descriptor.published, author: recordsWrite.author, recordId: message.recordId, entryId: yield RecordsWrite.getEntryId(recordsWrite.author, recordsWrite.message.descriptor) });
        if (recordsWrite.attesters.length > 0) {
            indexes.attester = recordsWrite.attesters[0];
        }
        if (message.contextId !== undefined) {
            indexes.contextId = message.contextId;
        }
        return indexes;
    });
}
//# sourceMappingURL=records-write.js.map