"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StreamMessageTranslator = void 0;
const trackerless_network_1 = require("@streamr/trackerless-network");
const utils_1 = require("@streamr/utils");
const EncryptedGroupKey_1 = require("./EncryptedGroupKey");
const MessageID_1 = require("./MessageID");
const MessageRef_1 = require("./MessageRef");
const StreamMessage_1 = require("./StreamMessage");
const oldToNewEncryptionType = (type) => {
    if (type === StreamMessage_1.EncryptionType.AES) {
        return trackerless_network_1.EncryptionType.AES;
    }
    return trackerless_network_1.EncryptionType.NONE;
};
const newToOldEncryptionType = (type) => {
    if (type === trackerless_network_1.EncryptionType.AES) {
        return StreamMessage_1.EncryptionType.AES;
    }
    return StreamMessage_1.EncryptionType.NONE;
};
const oldToNewContentType = (type) => {
    if (type === StreamMessage_1.ContentType.JSON) {
        return trackerless_network_1.ContentType.JSON;
    }
    return trackerless_network_1.ContentType.BINARY;
};
const newToOldContentType = (type) => {
    if (type === trackerless_network_1.ContentType.JSON) {
        return StreamMessage_1.ContentType.JSON;
    }
    return StreamMessage_1.ContentType.BINARY;
};
const oldToNewSignatureType = (type) => {
    if (type === StreamMessage_1.SignatureType.LEGACY_SECP256K1) {
        return trackerless_network_1.SignatureType.LEGACY_SECP256K1;
    }
    if (type === StreamMessage_1.SignatureType.ERC_1271) {
        return trackerless_network_1.SignatureType.ERC_1271;
    }
    return trackerless_network_1.SignatureType.SECP256K1;
};
const newToOldSignatureType = (type) => {
    if (type === trackerless_network_1.SignatureType.LEGACY_SECP256K1) {
        return StreamMessage_1.SignatureType.LEGACY_SECP256K1;
    }
    if (type === trackerless_network_1.SignatureType.ERC_1271) {
        return StreamMessage_1.SignatureType.ERC_1271;
    }
    return StreamMessage_1.SignatureType.SECP256K1;
};
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
class StreamMessageTranslator {
    static toProtobuf(msg) {
        const messageId = {
            timestamp: msg.getTimestamp(),
            sequenceNumber: msg.getSequenceNumber(),
            streamId: msg.getStreamId(),
            streamPartition: msg.getStreamPartition(),
            publisherId: (0, utils_1.toUserIdRaw)(msg.getPublisherId()),
            messageChainId: msg.getMsgChainId()
        };
        let previousMessageRef = undefined;
        if (msg.prevMsgRef) {
            previousMessageRef = {
                timestamp: msg.prevMsgRef.timestamp,
                sequenceNumber: msg.prevMsgRef.sequenceNumber
            };
        }
        let body;
        if (msg.messageType === StreamMessage_1.StreamMessageType.MESSAGE) {
            let newGroupKey = undefined;
            if (msg.newGroupKey) {
                newGroupKey = {
                    id: msg.newGroupKey.id,
                    data: msg.newGroupKey.data
                };
            }
            body = {
                oneofKind: 'contentMessage',
                contentMessage: {
                    content: msg.content,
                    contentType: oldToNewContentType(msg.contentType),
                    encryptionType: oldToNewEncryptionType(msg.encryptionType),
                    groupKeyId: msg.groupKeyId ?? undefined,
                    newGroupKey
                }
            };
        }
        else if (msg.messageType === StreamMessage_1.StreamMessageType.GROUP_KEY_REQUEST) {
            body = {
                oneofKind: 'groupKeyRequest',
                groupKeyRequest: trackerless_network_1.GroupKeyRequest.fromBinary(msg.content)
            };
        }
        else if (msg.messageType === StreamMessage_1.StreamMessageType.GROUP_KEY_RESPONSE) {
            body = {
                oneofKind: 'groupKeyResponse',
                groupKeyResponse: trackerless_network_1.GroupKeyResponse.fromBinary(msg.content)
            };
        }
        else {
            throw new Error('invalid message type');
        }
        const translated = {
            messageId,
            previousMessageRef,
            signature: msg.signature,
            signatureType: oldToNewSignatureType(msg.signatureType),
            body
        };
        return translated;
    }
    static toClientProtocol(msg) {
        let messageType;
        let content;
        let contentType = StreamMessage_1.ContentType.BINARY;
        let encryptionType = StreamMessage_1.EncryptionType.NONE;
        let newGroupKey = undefined;
        let groupKeyId = undefined;
        if (msg.body.oneofKind === 'contentMessage') {
            messageType = StreamMessage_1.StreamMessageType.MESSAGE;
            content = msg.body.contentMessage.content;
            contentType = newToOldContentType(msg.body.contentMessage.contentType);
            encryptionType = newToOldEncryptionType(msg.body.contentMessage.encryptionType);
            if (msg.body.contentMessage.newGroupKey) {
                newGroupKey = new EncryptedGroupKey_1.EncryptedGroupKey(msg.body.contentMessage.newGroupKey.id, msg.body.contentMessage.newGroupKey.data);
            }
            groupKeyId = msg.body.contentMessage.groupKeyId;
        }
        else if (msg.body.oneofKind === 'groupKeyRequest') {
            messageType = StreamMessage_1.StreamMessageType.GROUP_KEY_REQUEST;
            try {
                content = trackerless_network_1.GroupKeyRequest.toBinary(msg.body.groupKeyRequest);
            }
            catch (err) {
                // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
                throw new Error(`invalid group key request: ${err}`);
            }
        }
        else if (msg.body.oneofKind === 'groupKeyResponse') {
            messageType = StreamMessage_1.StreamMessageType.GROUP_KEY_RESPONSE;
            try {
                content = trackerless_network_1.GroupKeyResponse.toBinary(msg.body.groupKeyResponse);
            }
            catch (err) {
                // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
                throw new Error(`invalid group key response: ${err}`);
            }
        }
        else {
            throw new Error('invalid message type');
        }
        const messageId = new MessageID_1.MessageID(msg.messageId.streamId, msg.messageId.streamPartition, Number(msg.messageId.timestamp), msg.messageId.sequenceNumber, (0, utils_1.toUserId)(msg.messageId.publisherId), msg.messageId.messageChainId);
        let prevMsgRef = undefined;
        if (msg.previousMessageRef) {
            prevMsgRef = new MessageRef_1.MessageRef(Number(msg.previousMessageRef.timestamp), msg.previousMessageRef.sequenceNumber);
        }
        const translated = new StreamMessage_1.StreamMessage({
            messageId,
            prevMsgRef,
            messageType,
            content,
            contentType,
            signature: msg.signature,
            signatureType: newToOldSignatureType(msg.signatureType),
            encryptionType,
            groupKeyId,
            newGroupKey
        });
        return translated;
    }
}
exports.StreamMessageTranslator = StreamMessageTranslator;
//# sourceMappingURL=StreamMessageTranslator.js.map