/**
 * Phase 2 instruction builder (report output)
 *
 * Builds the instruction for the report output phase.
 * Assembles template variables and renders a single complete template.
 */
import { buildGitRules } from './instruction-context.js';
import { replaceTemplatePlaceholders } from './escape.js';
import { isOutputContractItem, renderReportContext, renderReportOutputInstruction } from './InstructionBuilder.js';
import { loadTemplate } from '../../../shared/prompts/index.js';
/**
 * Builds Phase 2 (report output) instructions.
 *
 * Renders a single complete template with all variables.
 */
export class ReportInstructionBuilder {
    step;
    context;
    constructor(step, context) {
        this.step = step;
        this.context = context;
    }
    build() {
        if (!this.step.outputContracts || this.step.outputContracts.length === 0) {
            throw new Error(`ReportInstructionBuilder called for step "${this.step.name}" which has no output contracts`);
        }
        const language = this.context.language ?? 'en';
        const gitRules = buildGitRules(this.step.allowGitCommit, language, 'phase2');
        const hasGitRules = gitRules.length > 0;
        let reportContext;
        if (this.context.targetFile) {
            reportContext = `- Report Directory: ${this.context.reportDir}/\n- Report File: ${this.context.reportDir}/${this.context.targetFile}`;
        }
        else {
            reportContext = renderReportContext(this.step.outputContracts, this.context.reportDir);
        }
        let reportOutput = '';
        let hasReportOutput = false;
        const instrContext = {
            task: '',
            iteration: 0,
            maxSteps: 0,
            stepIteration: this.context.stepIteration,
            cwd: this.context.cwd,
            projectCwd: this.context.cwd,
            userInputs: [],
            reportDir: this.context.reportDir,
            language,
        };
        const targetContract = this.context.targetFile
            ? this.step.outputContracts.find((entry) => entry.name === this.context.targetFile)
            : this.step.outputContracts[0];
        if (targetContract && isOutputContractItem(targetContract) && targetContract.order) {
            reportOutput = replaceTemplatePlaceholders(targetContract.order.trimEnd(), this.step, instrContext);
            hasReportOutput = true;
        }
        else if (!this.context.targetFile) {
            const output = renderReportOutputInstruction(this.step, instrContext, language);
            if (output) {
                reportOutput = output;
                hasReportOutput = true;
            }
        }
        let outputContract = '';
        let hasOutputContract = false;
        if (targetContract && isOutputContractItem(targetContract) && targetContract.format) {
            outputContract = replaceTemplatePlaceholders(targetContract.format.trimEnd(), this.step, instrContext);
            hasOutputContract = true;
        }
        return loadTemplate('perform_phase2_message', language, {
            workingDirectory: this.context.cwd,
            hasGitRules,
            gitRules,
            reportContext,
            hasLastResponse: this.context.lastResponse != null && this.context.lastResponse.trim().length > 0,
            lastResponse: this.context.lastResponse ?? '',
            hasReportOutput,
            reportOutput,
            hasOutputContract,
            outputContract,
        });
    }
}
//# sourceMappingURL=ReportInstructionBuilder.js.map