import { createSnapshotWriter } from '../snapshot-utils.js';
import { normalizeSnapshotStagePayloadWithNative } from '../../router/virtual-router/engine-selection/native-snapshot-hooks.js';
export class SnapshotStageRecorder {
    options;
    writer;
    constructor(options) {
        this.options = options;
        const contextAny = options.context;
        this.writer = createSnapshotWriter({
            requestId: options.context.requestId,
            endpoint: options.endpoint,
            providerKey: typeof options.context.providerId === 'string' ? options.context.providerId : undefined,
            groupRequestId: typeof contextAny.clientRequestId === 'string'
                ? contextAny.clientRequestId
                : typeof contextAny.groupRequestId === 'string'
                    ? contextAny.groupRequestId
                    : undefined
        });
    }
    record(stage, payload) {
        if (!this.writer) {
            return;
        }
        const normalized = normalizeSnapshotStagePayloadWithNative(stage, payload);
        if (!normalized) {
            return;
        }
        try {
            this.writer(stage, normalized);
        }
        catch {
            // ignore snapshot write errors
        }
    }
}
export function createSnapshotRecorder(context, endpoint) {
    return new SnapshotStageRecorder({ context, endpoint });
}
