"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.TraceReplayState = void 0;
class TraceReplayState {
    constructor() {
        this.eventsBySlug = new Map();
    }
    getActiveSlug() {
        return this.activeSlug;
    }
    loadEvents(slug, events) {
        this.eventsBySlug.set(slug, events);
        this.activeSlug = slug;
    }
    getEvents(slug) {
        return this.eventsBySlug.get(slug) ?? [];
    }
    activateSlug(slug) {
        this.activeSlug = slug;
    }
    clearEvents(slug) {
        this.eventsBySlug.delete(slug);
        if (this.activeSlug === slug) {
            this.activeSlug = undefined;
        }
        if (this.sharedTraceSlug === slug) {
            this.sharedTraceIndex = undefined;
            this.sharedIdRemapper = undefined;
            this.sharedTraceSlug = undefined;
        }
    }
    preserveWriterEvents(slug, writerEvents) {
        const existing = this.eventsBySlug.get(slug) ?? [];
        existing.push(...writerEvents);
        this.eventsBySlug.set(slug, existing);
    }
    getEventsWithWriterFallback(slug, activeWriterEntries) {
        const fromWriters = [];
        for (const entry of activeWriterEntries) {
            if (entry.traceSlug === slug && entry.tracing.traceWriter) {
                fromWriters.push(...entry.tracing.traceWriter.getEvents());
            }
        }
        if (fromWriters.length > 0)
            return fromWriters;
        return this.eventsBySlug.get(slug) ?? [];
    }
    async configureReplayMode(tracing) {
        if (process.env.E2E_TESTS !== 'true') {
            return;
        }
        const { TraceIndex: TI, IdRemapper: IR, TraceWriter: TW } = await Promise.resolve().then(() => __importStar(require('@n8n/instance-ai')));
        const slug = this.activeSlug;
        const events = slug ? this.eventsBySlug.get(slug) : undefined;
        if (events && events.length > 0) {
            if (this.sharedTraceSlug !== slug || !this.sharedTraceIndex) {
                this.sharedTraceIndex = new TI(events);
                this.sharedIdRemapper = new IR();
                this.sharedTraceSlug = slug;
            }
            tracing.replayMode = 'replay';
            tracing.traceIndex = this.sharedTraceIndex;
            tracing.idRemapper = this.sharedIdRemapper;
        }
        else {
            tracing.replayMode = 'record';
            tracing.traceWriter = new TW('recording');
        }
    }
}
exports.TraceReplayState = TraceReplayState;
//# sourceMappingURL=trace-replay-state.js.map