"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PendingChangesAdapter = void 0;
const crypto_1 = require("crypto");
const NOOP_TELEMETRY = {
    enqueued: () => undefined,
    duplicateSuppressed: () => undefined,
};
class PendingChangesAdapter {
    enqueued = new Map();
    bridge;
    telemetry;
    constructor(bridge, telemetry = NOOP_TELEMETRY) {
        this.bridge = bridge;
        this.telemetry = telemetry;
    }
    /**
     * Enqueue a chosen variant into the existing pending-changes channel.
     * Idempotent on `{sourceSessionId, variantId}`: a repeated submit with the
     * same key returns `enqueued: false, duplicate: true` and does not re-call
     * the bridge.
     */
    enqueue(envelope) {
        const key = idempotencyKey(envelope);
        if (this.enqueued.has(key)) {
            this.telemetry.duplicateSuppressed(envelope);
            return { enqueued: false, duplicate: true };
        }
        const variantItem = toVariantChangeItem(envelope);
        const request = {
            changes: [variantItem],
            sourceFiles: [],
            traceId: (0, crypto_1.randomUUID)(),
            agentRunId: (0, crypto_1.randomUUID)(),
        };
        this.bridge.queueChangeRequest(request);
        this.enqueued.set(key, envelope);
        this.telemetry.enqueued(envelope);
        return { enqueued: true, duplicate: false };
    }
    /**
     * Test/inspect helper — count of unique variants successfully enqueued.
     */
    size() {
        return this.enqueued.size;
    }
}
exports.PendingChangesAdapter = PendingChangesAdapter;
function idempotencyKey(envelope) {
    return `${envelope.sourceSessionId}|${envelope.variantId}`;
}
function toVariantChangeItem(envelope) {
    const base = {
        kind: 'variant',
        sourceSessionId: envelope.sourceSessionId,
        variantId: envelope.variantId,
        variantLabel: envelope.variantLabel,
        destinationPath: envelope.destinationPath,
        changedFilesCount: envelope.changedFilesCount,
    };
    if (envelope.payload.kind === 'diff') {
        return {
            ...base,
            payload: {
                kind: 'diff',
                diff: envelope.payload.diff,
                target: envelope.payload.target,
            },
        };
    }
    if (envelope.payload.kind === 'diff-applied') {
        return {
            ...base,
            payload: {
                kind: 'diff-applied',
                diff: envelope.payload.diff,
                target: envelope.payload.target,
                note: envelope.payload.note,
            },
        };
    }
    if (envelope.payload.kind === 'project-created') {
        return {
            ...base,
            payload: {
                kind: 'project-created',
                note: envelope.payload.note,
            },
        };
    }
    throw new Error(`Refusing to enqueue server-internal payload kind: ${envelope.payload.kind}`);
}
//# sourceMappingURL=pendingChangesAdapter.js.map