"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DevConsoleStore = exports.__ΩDevConsoleMutexEntry = exports.__ΩDevConsoleDatabaseQueryEntry = exports.__ΩDevConsoleSrpcDisconnection = exports.__ΩDevConsoleSrpcConnection = exports.__ΩDevConsoleSrpcMessage = exports.__ΩDevConsoleHttpEntry = exports.__ΩDevConsoleErrorInfo = exports.RingBuffer = void 0;
const __ΩRecord = ['K', 'T', 'Record', 'l\'e#"Rb!b"Pde"!N#!w#y'];
class RingBuffer {
    constructor(capacity) {
        this.capacity = capacity;
        this.head = 0;
        this.count = 0;
        this.buffer = Array.from({ length: capacity });
    }
    push(item) {
        this.buffer[this.head] = item;
        this.head = (this.head + 1) % this.capacity;
        if (this.count < this.capacity)
            this.count++;
    }
    toArray() {
        if (this.count === 0)
            return [];
        if (this.count < this.capacity) {
            return this.buffer.slice(0, this.count);
        }
        return [...this.buffer.slice(this.head), ...this.buffer.slice(0, this.head)];
    }
    clear() {
        this.buffer = Array.from({ length: this.capacity });
        this.head = 0;
        this.count = 0;
    }
    get length() {
        return this.count;
    }
}
exports.RingBuffer = RingBuffer;
RingBuffer.__type = ['T', 'buffer', 'head', function () { return 0; }, 'count', function () { return 0; }, 'capacity', 'constructor', 'item', 'push', 'toArray', 'clear', 'RingBuffer', 'b!Pe"!-JF3";\'3#;>$\'3%;>&P\'2\';"0(Pe"!2)$0*Pe"!F0+P$0,!5w-'];
const __ΩDevConsoleErrorInfo = ['name', 'message', 'stack', 0, 'cause', 'DevConsoleErrorInfo', 'P&4!&4"&4#8n$4%8&"LMw&y'];
exports.__ΩDevConsoleErrorInfo = __ΩDevConsoleErrorInfo;
const __ΩDevConsoleHttpEntry = ['id', 'timestamp', 'method', 'url', 'remoteAddress', () => __ΩRecord, 'requestHeaders', 'requestBody', 'statusCode', () => __ΩRecord, 'responseHeaders', 'responseBody', 'durationMs', () => __ΩDevConsoleErrorInfo, 'error', 'DevConsoleHttpEntry', 'P&4!\'4"&4#&4$&4%&P&&F-Jo&#4\'P&,J4(\'4)&P&&F-Jo*#4+P&,J4,\'4-n.4/8Mw0y'];
exports.__ΩDevConsoleHttpEntry = __ΩDevConsoleHttpEntry;
const __ΩDevConsoleSrpcMessage = ['id', 'timestamp', 'streamId', 'clientId', "inbound", "outbound", 'direction', 'messageType', 'isReply', () => __ΩRecord, 'data', 'error', 'isUserError', 'DevConsoleSrpcMessage', 'P&4!\'4"&4#&4$P.%.&J4\'&4()4)&"o*#4+&4,8)4-8Mw.y'];
exports.__ΩDevConsoleSrpcMessage = __ΩDevConsoleSrpcMessage;
const __ΩDevConsoleSrpcConnection = ['streamId', 'clientId', 'clientStreamId', 'appVersion', 'address', 'connectedAt', 'lastPingAt', () => __ΩRecord, 'meta', 'messageCount', 'DevConsoleSrpcConnection', 'P&4!&4"&4#&4$&4%\'4&\'4\'&"o(#4)\'4*Mw+y'];
exports.__ΩDevConsoleSrpcConnection = __ΩDevConsoleSrpcConnection;
const __ΩDevConsoleSrpcDisconnection = ['streamId', 'clientId', 'disconnectedAt', 'cause', 'DevConsoleSrpcDisconnection', 'P&4!&4"\'4#&4$Mw%y'];
exports.__ΩDevConsoleSrpcDisconnection = __ΩDevConsoleSrpcDisconnection;
const __ΩDevConsoleDatabaseQueryEntry = ['id', 'timestamp', 'sql', 'params', "running", "ok", "error", 'status', 'durationMs', 'error', 'DevConsoleDatabaseQueryEntry', 'P&4!\'4"&4#"F4$P.%.&.\'J4(\'4)8&4*8Mw+y'];
exports.__ΩDevConsoleDatabaseQueryEntry = __ΩDevConsoleDatabaseQueryEntry;
const __ΩDevConsoleMutexEntry = ['id', 'key', "pending", "acquired", "released", "error", "failed", 'status', 'startedAt', 'acquiredAt', 'releasedAt', 'durationMs', 'waitDurationMs', 'waited', 'error', 'DevConsoleMutexEntry', 'P&4!&4"P.#.$.%.&.\'J4(\'4)\'4*8\'4+8\'4,8\'4-8)4.8&4/8Mw0y'];
exports.__ΩDevConsoleMutexEntry = __ΩDevConsoleMutexEntry;
class DevConsoleStore {
    constructor() {
        this.httpEntries = (RingBuffer.Ω = [[() => __ΩDevConsoleHttpEntry, 'n!']], new RingBuffer(500));
        this.srpcMessages = (RingBuffer.Ω = [[() => __ΩDevConsoleSrpcMessage, 'n!']], new RingBuffer(500));
        this.srpcConnections = (Map.Ω = [['&'], [() => __ΩDevConsoleSrpcConnection, 'n!']], new Map());
        this.srpcDisconnected = (RingBuffer.Ω = [[() => __ΩDevConsoleSrpcDisconnection, 'n!']], new RingBuffer(50));
        this.dbQueries = (RingBuffer.Ω = [[() => __ΩDevConsoleDatabaseQueryEntry, 'n!']], new RingBuffer(500));
        this.mutexEntries = (RingBuffer.Ω = [[() => __ΩDevConsoleMutexEntry, 'n!']], new RingBuffer(200));
        this.activeMutexes = (Map.Ω = [['&'], [() => __ΩDevConsoleMutexEntry, 'n!']], new Map());
        this.startedAt = Date.now();
    }
    addHttpEntry(entry) {
        this.httpEntries.push(entry);
        this.onEvent?.('http:entry', entry);
    }
    addSrpcMessage(msg) {
        this.srpcMessages.push(msg);
        this.onEvent?.('srpc:message', msg);
    }
    addSrpcConnection(conn) {
        this.srpcConnections.set(conn.streamId, conn);
        this.onEvent?.('srpc:connection', conn);
    }
    removeSrpcConnection(streamId, clientId, cause) {
        this.srpcConnections.delete(streamId);
        const disc = { streamId, clientId, disconnectedAt: Date.now(), cause };
        this.srpcDisconnected.push(disc);
        this.onEvent?.('srpc:disconnection', disc);
    }
    clearHttpEntries() {
        this.httpEntries.clear();
    }
    clearSrpcMessages() {
        this.srpcMessages.clear();
        this.srpcDisconnected.clear();
    }
    addDatabaseQuery(entry) {
        this.dbQueries.push(entry);
        this.onEvent?.('db:query', entry);
    }
    completeDatabaseQuery(entry) {
        this.onEvent?.('db:query:complete', entry);
    }
    clearDatabaseQueries() {
        this.dbQueries.clear();
    }
    addMutexPending(entry) {
        this.mutexEntries.push(entry);
        this.activeMutexes.set(entry.id, entry);
        this.onEvent?.('mutex:pending', entry);
    }
    updateMutexAcquired(id, waited) {
        const entry = this.activeMutexes.get(id);
        if (entry) {
            entry.status = 'acquired';
            entry.acquiredAt = Date.now();
            entry.waitDurationMs = entry.acquiredAt - entry.startedAt;
            entry.waited = waited;
            this.onEvent?.('mutex:acquired', entry);
        }
    }
    updateMutexReleased(id) {
        const entry = this.activeMutexes.get(id);
        if (entry) {
            entry.status = 'released';
            entry.releasedAt = Date.now();
            entry.durationMs = entry.releasedAt - (entry.acquiredAt ?? entry.startedAt);
            this.activeMutexes.delete(id);
            this.onEvent?.('mutex:released', entry);
        }
    }
    updateMutexError(id, err) {
        const entry = this.activeMutexes.get(id);
        if (entry) {
            entry.status = 'error';
            entry.releasedAt = Date.now();
            entry.durationMs = entry.releasedAt - (entry.acquiredAt ?? entry.startedAt);
            entry.error = err instanceof Error ? err.message : String(err);
            this.activeMutexes.delete(id);
            this.onEvent?.('mutex:error', entry);
        }
    }
    addMutexFailed(entry) {
        this.mutexEntries.push(entry);
        this.onEvent?.('mutex:failed', entry);
    }
    static init() {
        if (!this.instance) {
            this.instance = new DevConsoleStore();
        }
        return this.instance;
    }
    static get() {
        return this.instance;
    }
}
exports.DevConsoleStore = DevConsoleStore;
DevConsoleStore.instance = null;
DevConsoleStore.__type = [() => DevConsoleStore, 'instance', function () { return null; }, 'httpEntries', function () { return (RingBuffer.Ω = [[() => __ΩDevConsoleHttpEntry, 'n!']], new RingBuffer(500)); }, 'srpcMessages', function () { return (RingBuffer.Ω = [[() => __ΩDevConsoleSrpcMessage, 'n!']], new RingBuffer(500)); }, 'srpcConnections', function () { return (Map.Ω = [['&'], [() => __ΩDevConsoleSrpcConnection, 'n!']], new Map()); }, 'srpcDisconnected', function () { return (RingBuffer.Ω = [[() => __ΩDevConsoleSrpcDisconnection, 'n!']], new RingBuffer(50)); }, 'dbQueries', function () { return (RingBuffer.Ω = [[() => __ΩDevConsoleDatabaseQueryEntry, 'n!']], new RingBuffer(500)); }, 'mutexEntries', function () { return (RingBuffer.Ω = [[() => __ΩDevConsoleMutexEntry, 'n!']], new RingBuffer(200)); }, 'activeMutexes', function () { return (Map.Ω = [['&'], [() => __ΩDevConsoleMutexEntry, 'n!']], new Map()); }, 'startedAt', function () { return Date.now(); }, 'type', 'data', '', 'onEvent', () => __ΩDevConsoleHttpEntry, 'entry', 'addHttpEntry', () => __ΩDevConsoleSrpcMessage, 'msg', 'addSrpcMessage', () => __ΩDevConsoleSrpcConnection, 'conn', 'addSrpcConnection', 'streamId', 'clientId', 'cause', 'removeSrpcConnection', 'clearHttpEntries', 'clearSrpcMessages', () => __ΩDevConsoleDatabaseQueryEntry, 'addDatabaseQuery', () => __ΩDevConsoleDatabaseQueryEntry, 'completeDatabaseQuery', 'clearDatabaseQueries', () => __ΩDevConsoleMutexEntry, 'addMutexPending', 'id', 'waited', 'updateMutexAcquired', 'updateMutexReleased', 'err', 'updateMutexError', () => __ΩDevConsoleMutexEntry, 'addMutexFailed', () => DevConsoleStore, 'init', () => DevConsoleStore, 'get', 'DevConsoleStore', 'PP7!,J3";s>#!3$9>%!3&9>\'!3(9>)!3*9>+!3,9>-!3.9>/!309>1!329>3P&24#25$/6378Pn829"0:Pn;2<"0=Pn>2?"0@P&2A&2B&2C"0DP"0EP"0FPnG29"0HPnI29"0JP"0KPnL29"0MP&2N)2O"0PP&2N"0QP&2N#2R"0SPnT29"0UPP7V0WsPPP7X,J0Ys5wZ'];
//# sourceMappingURL=devconsole.store.js.map