"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertStreamMessageToLogStoreMessage = exports.convertStreamMessageToMessage = exports.LogStoreMessageStream = void 0;
const omit_1 = __importDefault(require("lodash/omit"));
const rxjs_1 = require("rxjs");
const PushBuffer_1 = require("./streamr/utils/PushBuffer");
/**
 * Provides asynchronous iteration with
 * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of | for await .. of}.
 */
class LogStoreMessageStream {
    constructor(messageStream, metadataStream) {
        this.messageStream = messageStream;
        this.metadataStream = metadataStream;
        this.messages$ = (0, rxjs_1.defer)(() => {
            // @ts-expect-error Property 'iterate' does not exist on type 'IPushPipeline<StreamMessage<unknown>, StreamMessage<unknown>>'
            return messageStream.iterate();
        }).pipe((0, rxjs_1.shareReplay)({ refCount: true }));
    }
    /**
     * Attach a legacy onMessage handler and consume if necessary.
     * onMessage is passed parsed content as first arument, and streamMessage as second argument.
     */
    useLegacyOnMessageHandler(onMessage) {
        if (this.messageStream instanceof PushBuffer_1.PushBuffer) {
            return this;
        }
        this.messageStream.onMessage.listen(async (streamMessage) => {
            if (typeof streamMessage !== 'string') {
                const msg = (0, exports.convertStreamMessageToMessage)(streamMessage);
                await onMessage(msg.content, (0, omit_1.default)(msg, 'content'));
            }
        });
        this.messageStream.flow();
        return this;
    }
    asObservable() {
        return (0, rxjs_1.defer)(async () => this).pipe((0, rxjs_1.switchMap)(() => this));
    }
    async *[Symbol.asyncIterator]() {
        for await (const msg of this.messages$) {
            if (typeof msg === 'string') {
                yield msg;
            }
            else {
                yield (0, exports.convertStreamMessageToLogStoreMessage)(msg);
            }
        }
    }
}
exports.LogStoreMessageStream = LogStoreMessageStream;
const convertStreamMessageToMessage = (msg) => {
    return {
        content: msg.getParsedContent(),
        streamId: msg.getStreamId(),
        streamPartition: msg.getStreamPartition(),
        timestamp: msg.getTimestamp(),
        sequenceNumber: msg.getSequenceNumber(),
        signature: msg.signature,
        publisherId: msg.getPublisherId(),
        msgChainId: msg.getMsgChainId(),
        // @ts-expect-error streamMessage is marked as internal in Message interface
        streamMessage: msg,
    };
};
exports.convertStreamMessageToMessage = convertStreamMessageToMessage;
const convertStreamMessageToLogStoreMessage = (msg) => {
    return {
        content: msg.getParsedContent(),
        metadata: {
            id: msg.getMessageID(),
            prevMsgRef: msg.getPreviousMessageRef(),
            newGroupKey: msg.getNewGroupKey(),
            signature: msg.signature,
            // I'm also including these for further usage:
            messageType: msg.messageType,
            contentType: msg.contentType,
            encryptionType: msg.encryptionType,
            groupKeyId: msg.groupKeyId,
        },
    };
};
exports.convertStreamMessageToLogStoreMessage = convertStreamMessageToLogStoreMessage;
//# sourceMappingURL=LogStoreMessageStream.js.map