"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.__ΩSrpcMessageObserver = void 0;
exports.createDevConsoleSrpcObserver = createDevConsoleSrpcObserver;
exports.identifyMessageType = identifyMessageType;
exports.sanitizeData = sanitizeData;
const __ΩRecord = ['K', 'T', 'Record', 'l\'e#"Rb!b"Pde"!N#!w#y'];
function __assignType(fn, args) {
    fn.__type = args;
    return fn;
}
const __ΩSrpcMessageObserver = ['SrpcStream', 'stream', 'messageType', () => __ΩRecord, 'data', 'onInboundMessage', () => __ΩRecord, 'onOutboundMessage', 'onConnectionEstablished', 'cause', 'onConnectionClosed', 'SrpcMessageObserver', 'PP"w!2"&2#&#o$#2%$1&8P"w!2"&2#&#o\'#2%$1(8P"w!2"$1)8P"w!2"&2*$1+8Mw,y'];
exports.__ΩSrpcMessageObserver = __ΩSrpcMessageObserver;
const BASE_MESSAGE_KEYS = new Set(['requestId', 'reply', 'error', 'userError', 'trace', 'pingPong', 'byteStreamOperation']);
function identifyMessageType(data) {
    for (const key of Object.keys(data)) {
        if (!BASE_MESSAGE_KEYS.has(key) && data[key] != null) {
            return key;
        }
    }
    return 'unknown';
}
identifyMessageType.__type = [() => __ΩRecord, 'data', 'identifyMessageType', 'P&#o!#2"&/#'];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function sanitizeData(data) {
    const result = {};
    for (const [key, value] of Object.entries(data)) {
        if (key === 'trace')
            continue;
        if (key === 'byteStreamOperation' && value) {
            result[key] = {
                streamId: value.streamId,
                operation: value.write ? 'write' : value.finish ? 'finish' : value.destroy ? 'destroy' : 'unknown'
            };
            continue;
        }
        result[key] = value;
    }
    return result;
}
sanitizeData.__type = [() => __ΩRecord, 'data', () => __ΩRecord, 'sanitizeData', 'P&"o!#2"&#o##/$'];
function createDevConsoleSrpcObserver(store) {
    return {
        onInboundMessage: __assignType(function onInboundMessage(stream, messageType, data) {
            const conn = store.srpcConnections.get(stream.id);
            if (conn)
                conn.messageCount++;
            store.addSrpcMessage({
                id: data.requestId ?? '',
                timestamp: Date.now(),
                streamId: stream.id,
                clientId: stream.clientId,
                direction: 'inbound',
                messageType,
                isReply: !!data.reply,
                data: sanitizeData(data),
                error: data.error,
                isUserError: data.userError
            });
        }, ['SrpcStream', 'stream', 'messageType', () => __ΩRecord, 'data', 'onInboundMessage', 'P"w!2"&2#&#o$#2%"/&']),
        onOutboundMessage: __assignType(function onOutboundMessage(stream, messageType, data) {
            store.addSrpcMessage({
                id: data.requestId ?? '',
                timestamp: Date.now(),
                streamId: stream.id,
                clientId: stream.clientId,
                direction: 'outbound',
                messageType,
                isReply: !!data.reply,
                data: sanitizeData(data),
                error: data.error,
                isUserError: data.userError
            });
        }, ['SrpcStream', 'stream', 'messageType', () => __ΩRecord, 'data', 'onOutboundMessage', 'P"w!2"&2#&#o$#2%"/&']),
        onConnectionEstablished: __assignType(function onConnectionEstablished(stream) {
            store.addSrpcConnection({
                streamId: stream.id,
                clientId: stream.clientId,
                clientStreamId: stream.clientStreamId,
                appVersion: stream.appVersion,
                address: stream.address,
                connectedAt: stream.connectedAt,
                lastPingAt: stream.lastPingAt,
                meta: stream.meta,
                messageCount: 0
            });
        }, ['SrpcStream', 'stream', 'onConnectionEstablished', 'P"w!2""/#']),
        onConnectionClosed: __assignType(function onConnectionClosed(stream, cause) {
            store.removeSrpcConnection(stream.id, stream.clientId, cause);
        }, ['SrpcStream', 'stream', 'cause', 'onConnectionClosed', 'P"w!2"&2#"/$'])
    };
}
createDevConsoleSrpcObserver.__type = ['DevConsoleStore', 'store', () => __ΩSrpcMessageObserver, 'createDevConsoleSrpcObserver', 'P"w!2"n#/$'];
//# sourceMappingURL=devconsole.srpc.js.map