import { buildPhaseExecutionId } from '../../../shared/utils/phaseExecutionId.js';
function buildExecutionCounterKey(stepName, phase, iteration) {
    return JSON.stringify([stepName, iteration, phase]);
}
function requireIteration(stepName, phase, iteration) {
    if (iteration == null) {
        throw new Error(`Missing iteration for phase execution id: ${stepName}:${phase}`);
    }
    return iteration;
}
export class SessionLoggerPhaseTracker {
    promptsByExecutionId = new Map();
    executionCounters = new Map();
    trackStart(options) {
        const phaseExecutionId = this.resolvePhaseExecutionId(options.stepName, options.phase, options.phaseExecutionId, options.iteration);
        if (options.capturePrompt) {
            this.promptsByExecutionId.set(phaseExecutionId, options.promptParts);
        }
        return phaseExecutionId;
    }
    trackCompletion(options) {
        const phaseExecutionId = this.resolveCompletionPhaseExecutionId(options.stepName, options.phase, options.phaseExecutionId, options.iteration);
        if (!options.requirePrompt) {
            return { phaseExecutionId };
        }
        const promptParts = this.promptsByExecutionId.get(phaseExecutionId);
        if (!promptParts) {
            throw new Error(`Missing debug prompt for ${options.stepName}:${options.phase}:${phaseExecutionId}`);
        }
        this.promptsByExecutionId.delete(phaseExecutionId);
        return { phaseExecutionId, promptParts };
    }
    resolveExistingExecutionId(options) {
        return this.resolveCompletionPhaseExecutionId(options.stepName, options.phase, options.phaseExecutionId, options.iteration);
    }
    resolvePhaseExecutionId(stepName, phase, phaseExecutionId, iteration) {
        if (phaseExecutionId) {
            return phaseExecutionId;
        }
        const resolvedIteration = requireIteration(stepName, phase, iteration);
        const key = buildExecutionCounterKey(stepName, phase, resolvedIteration);
        const current = this.executionCounters.get(key) ?? 0;
        const next = current + 1;
        this.executionCounters.set(key, next);
        return buildPhaseExecutionId({
            step: stepName,
            iteration: resolvedIteration,
            phase,
            sequence: next,
        });
    }
    resolveCompletionPhaseExecutionId(stepName, phase, phaseExecutionId, iteration) {
        if (phaseExecutionId) {
            return phaseExecutionId;
        }
        const resolvedIteration = requireIteration(stepName, phase, iteration);
        const key = buildExecutionCounterKey(stepName, phase, resolvedIteration);
        const current = this.executionCounters.get(key);
        if (current == null) {
            throw new Error(`Missing phase execution id on completion for ${stepName}:${phase}`);
        }
        return buildPhaseExecutionId({
            step: stepName,
            iteration: resolvedIteration,
            phase,
            sequence: current,
        });
    }
}
//# sourceMappingURL=sessionLoggerPhaseTracker.js.map