"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 MessageID_1 = require("./MessageID");
const MessageRef_1 = require("./MessageRef");
const StreamMessage_1 = require("./StreamMessage");
// 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: msg.contentType,
                    encryptionType: 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: msg.signatureType,
            body
        };
        return translated;
    }
    static toClientProtocol(msg) {
        let messageType;
        let content;
        let contentType = trackerless_network_1.ContentType.BINARY;
        let encryptionType = trackerless_network_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 = msg.body.contentMessage.contentType;
            encryptionType = msg.body.contentMessage.encryptionType;
            newGroupKey = msg.body.contentMessage.newGroupKey;
            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: msg.signatureType,
            encryptionType,
            groupKeyId,
            newGroupKey
        });
        return translated;
    }
}
exports.StreamMessageTranslator = StreamMessageTranslator;
//# sourceMappingURL=StreamMessageTranslator.js.map