/**
 * Template escaping and placeholder replacement utilities
 *
 * Used by instruction builders to process resolved instruction content.
 *
 * escapeTemplateChars is re-exported from faceted-prompting.
 * replaceTemplatePlaceholders is TAKT-specific and stays here.
 */
import { resolveWorkflowStateReference } from '../state/workflow-state-access.js';
import { escapeTemplateChars } from 'faceted-prompting';
export { escapeTemplateChars } from 'faceted-prompting';
/**
 * Replace supported placeholders in the resolved instruction body.
 */
export function replaceTemplatePlaceholders(template, step, context) {
    let result = template;
    result = result.replace(/\{(context|structured|effect):([^}]+)\}/g, (_match, root, ref) => {
        if (!context.workflowState) {
            throw new Error(`Workflow state is required for "{${root}:${ref}}" interpolation`);
        }
        const value = resolveWorkflowStateReference(`${root}.${ref.replace(/:/g, '.')}`, context.workflowState);
        if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
            return escapeTemplateChars(String(value));
        }
        if (root !== 'context') {
            throw new Error(`Instruction interpolation requires scalar value for "${root}:${ref}"`);
        }
        return escapeTemplateChars(JSON.stringify(value, null, 2));
    });
    // Replace {task}
    result = result.replace(/\{task\}/g, escapeTemplateChars(context.task));
    // Replace {iteration}, {max_steps}, and {step_iteration}
    result = result.replace(/\{iteration\}/g, String(context.iteration));
    result = result.replace(/\{max_steps\}/g, String(context.maxSteps));
    result = result.replace(/\{step_iteration\}/g, String(context.stepIteration));
    // Replace {previous_response}
    if (step.passPreviousResponse) {
        if (context.previousResponseText !== undefined) {
            result = result.replace(/\{previous_response\}/g, escapeTemplateChars(context.previousResponseText));
        }
        else if (context.previousOutput) {
            result = result.replace(/\{previous_response\}/g, escapeTemplateChars(context.previousOutput.content));
        }
        else {
            result = result.replace(/\{previous_response\}/g, '');
        }
    }
    // Replace {user_inputs}
    const userInputsStr = context.userInputs.join('\n');
    result = result.replace(/\{user_inputs\}/g, escapeTemplateChars(userInputsStr));
    // Replace {report_dir}
    if (context.reportDir) {
        result = result.replace(/\{report_dir\}/g, context.reportDir);
    }
    result = result.replace(/\{current_report\}/g, context.currentReport ?? '');
    result = result.replace(/\{previous_report\}/g, context.previousReport ?? '');
    result = result.replace(/\{report_history\}/g, context.reportHistory ?? '');
    result = result.replace(/\{peer_reports\}/g, context.peerReports ?? '');
    // Replace {report:filename} with reportDir/filename
    if (context.reportDir) {
        result = result.replace(/\{report:([^}]+)\}/g, (_match, filename) => {
            return `${context.reportDir}/${filename}`;
        });
    }
    return result;
}
//# sourceMappingURL=escape.js.map