"use strict";
/**
 * Copyright: (c) Myia SAS 2026.
 * This file and its contents are licensed under the AGPLv3 License.
 * Please see the LICENSE file at the root of this repository
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.NetworkCoalescer = void 0;
exports.deriveSource = deriveSource;
class NetworkCoalescer {
    constructor() {
        this.idsByRequest = new WeakMap();
        this.pending = new Map();
        this.nextId = 1;
    }
    /** Test-only escape hatch. */
    size() {
        return this.pending.size;
    }
    has(request) {
        return this.idsByRequest.has(request);
    }
    /**
     * Resolve (and assign on first use) the per-test id we attribute
     * to this request. Stable across every event Playwright fires for
     * the same Request instance.
     */
    idFor(request) {
        let id = this.idsByRequest.get(request);
        if (id === undefined) {
            id = `req-${this.nextId++}`;
            this.idsByRequest.set(request, id);
        }
        return id;
    }
    /**
     * Register a new (non-redirect) request. The seed `NetworkRecord`
     * carries everything that is known at issue time. The session
     * fills in `t` / `statementSeq` / step path / page url before
     * calling — keeping the coalescer free of clock/recorder deps.
     */
    begin(seed) {
        this.pending.set(seed.requestId, seed);
    }
    /**
     * Apply a partial update keyed by the request's `requestId`. Used
     * by the session as `response` / `requestfinished` /
     * `requestfailed` events arrive. Silently ignores updates for
     * unknown ids — happens when `urlFilter` dropped the request at
     * `request` time but a later event still fires.
     */
    update(requestId, patch) {
        const existing = this.pending.get(requestId);
        if (!existing)
            return;
        Object.assign(existing, patch);
    }
    /**
     * Mark an exchange as complete and pop it out for emission. The
     * caller writes the record to disk and decides what to do with
     * any buffered body. Returns `undefined` for unknown ids.
     */
    finalize(requestId) {
        const record = this.pending.get(requestId);
        if (!record)
            return undefined;
        this.pending.delete(requestId);
        // Strip the WeakMap key — consumers see the public schema only.
        const { __request, ...publicRecord } = record;
        void __request;
        return publicRecord;
    }
    /**
     * At teardown: emit anything still pending so the trace file
     * reflects requests that never finished (e.g. a fetch that
     * outlived the test and was cancelled by `context.close()`).
     * Empties the map.
     */
    drain() {
        const out = [];
        for (const [id, record] of this.pending) {
            const { __request, ...publicRecord } = record;
            void __request;
            out.push(publicRecord);
            this.pending.delete(id);
        }
        return out;
    }
}
exports.NetworkCoalescer = NetworkCoalescer;
function deriveSource(request, contextSource) {
    // The session passes a hint that says where it observed the event;
    // the coalescer just records it on the seed.
    void request;
    return contextSource;
}
