"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderReconciliationDocument = renderReconciliationDocument;
exports.renderTaskReconciliationDocument = renderTaskReconciliationDocument;
// Responsibility-focused implementation extracted from the public compatibility facade.
const node_fs_1 = __importDefault(require("node:fs"));
const errors_1 = require("../../errors");
const path_resolution_1 = require("../../templates/path-resolution");
const template_rendering_1 = require("../implementation-summary/template-rendering");
function renderReconciliationDocument(repositoryRoot, context, options) {
    const templatePath = (0, path_resolution_1.resolveWorkspaceTemplateAbsolutePath)({
        repositoryRoot,
        templatePath: ".day-shift/templates/milestone-reconciliation.md"
    });
    if (!node_fs_1.default.existsSync(templatePath)) {
        throw (0, errors_1.createUsageError)("Milestone reconciliation template is missing.", ["Expected template at .day-shift/templates/milestone-reconciliation.md"]);
    }
    let text = node_fs_1.default.readFileSync(templatePath, "utf8");
    text = (0, template_rendering_1.serializeWorkflowManualExceptionField)(text, "reconciliation", options.metadata);
    text = (0, template_rendering_1.replaceSpecPlaceholders)(text, context.upstreamSpecs);
    const replacements = {
        "{{slice}}": context.slice,
        "{{phase}}": context.phase,
        "{{milestone}}": context.milestone,
        "{{planning_depth}}": context.planningDepth,
        "{{workflow_provenance}}": options.metadata.workflowProvenance,
        "{{workflow_cli_command}}": options.metadata.workflowCliCommand,
        "{{slice_overview}}": context.sliceOverviewPath,
        "{{phase_overview}}": context.phaseOverviewPath,
        "{{milestone_overview}}": options.milestoneOverviewPath,
        "{{milestone_title}}": context.title,
        "reconciliation_status: pending": `reconciliation_status: ${options.metadata.reconciliationStatus}`,
        "status: draft": `status: ${options.metadata.status}`,
        "{{review_status}}": options.sections["Review Status"],
        "{{milestone_intent}}": options.sections["Milestone Intent"],
        "{{completed_task_summaries}}": options.sections["Completed Task Summaries"],
        "{{acceptance_criteria_review}}": options.sections["Acceptance Criteria Review"],
        "{{planned_work}}": options.sections["Planned Work"],
        "{{resolved_findings}}": options.sections["Resolved Findings"],
        "{{discovered_work}}": options.sections["Discovered Work"],
        "{{backlog_work}}": options.sections["Backlog Work"],
        "{{accepted_residual_work}}": options.sections["Accepted Residual Work"],
        "{{validation_review}}": options.sections["Validation Review"],
        "{{follow_up_tasks}}": options.sections["Follow-Up Tasks"],
        "{{review_concerns}}": options.sections["Review Concerns"],
        "{{final_notes}}": options.sections["Final Notes"]
    };
    return (0, template_rendering_1.replaceAll)(text, replacements).replace(/^None accepted yet\.$/m, options.sections["Accepted Residual Work"]);
}
function renderTaskReconciliationDocument(repositoryRoot, context, options) {
    const templatePath = (0, path_resolution_1.resolveWorkspaceTemplateAbsolutePath)({
        repositoryRoot,
        templatePath: ".day-shift/templates/reconciliation.md"
    });
    if (!node_fs_1.default.existsSync(templatePath)) {
        throw (0, errors_1.createUsageError)("Task-level reconciliation template is missing.", ["Expected template at .day-shift/templates/reconciliation.md"]);
    }
    let text = node_fs_1.default.readFileSync(templatePath, "utf8");
    text = (0, template_rendering_1.serializeWorkflowManualExceptionField)(text, "reconciliation", options.metadata);
    text = (0, template_rendering_1.replaceSpecPlaceholders)(text, context.upstreamSpecs);
    const replacements = {
        "{{slice}}": context.slice,
        "{{phase}}": context.phase ?? "",
        "{{milestone}}": context.milestone ?? "",
        "{{task}}": context.task,
        "{{planning_depth}}": context.planningDepth,
        "{{workflow_provenance}}": options.metadata.workflowProvenance,
        "{{workflow_cli_command}}": options.metadata.workflowCliCommand,
        "{{slice_overview}}": context.sliceOverviewPath,
        "{{phase_overview}}": context.phaseOverviewPath ?? "",
        "{{milestone_overview}}": context.milestoneOverviewPath ?? "",
        "{{task_definition}}": options.taskDefinitionPath,
        "{{implementation_summary}}": options.implementationSummaryPath,
        "{{task_title}}": context.title,
        "reconciliation_status: pending": `reconciliation_status: ${options.metadata.reconciliationStatus}`,
        "status: draft": `status: ${options.metadata.status}`,
        "{{review_status}}": options.sections["Review Status"],
        "{{original_intent}}": options.sections["Original Intent"],
        "{{completed_work}}": options.sections["Completed Work"],
        "{{acceptance_criteria_review}}": options.sections["Acceptance Criteria Review"],
        "{{planned_work}}": options.sections["Planned Work"],
        "{{resolved_findings}}": options.sections["Resolved Findings"],
        "{{discovered_work}}": options.sections["Discovered Work"],
        "{{backlog_work}}": options.sections["Backlog Work"],
        "{{accepted_residual_work}}": options.sections["Accepted Residual Work"],
        "{{validation_review}}": options.sections["Validation Review"],
        "{{follow_up_tasks}}": options.sections["Follow-Up Tasks"],
        "{{review_concerns}}": options.sections["Review Concerns"],
        "{{final_notes}}": options.sections["Final Notes"]
    };
    return (0, template_rendering_1.replaceAll)(text, replacements);
}
