"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createReconciliationBuildCommand = createReconciliationBuildCommand;
const errors_1 = require("../../../lib/errors");
const implementation_summary_reconciliation_1 = require("../../../lib/planning/implementation-summary-reconciliation");
const output_1 = require("../../../lib/output");
const runtime_contract_1 = require("../../../lib/runtime/runtime-contract");
const global_format_1 = require("../global-format");
const persisted_artifact_reminder_1 = require("../persisted-artifact-reminder");
async function createReconciliationBuildCommand() {
    const { Command } = await import("commander");
    const command = new Command("build");
    command
        .description("Refresh a reconciliation artifact at its canonical destination; milestone rollup is the default, task-local output is an explicit exception.")
        .option("--milestone-overview <path>", "Repository-relative path to the milestone overview that owns the paired reconciliation artifact.")
        .option("--task-definition <path>", "Repository-relative task-definition.md path for an explicit task-level reconciliation exception.")
        .option("--rationale <text>", "Required rationale when using --task-definition for an explicit task-level reconciliation exception.")
        .action((_, commandInstance) => {
        const format = (0, output_1.resolveOutputFormat)((0, global_format_1.getGlobalFormat)(commandInstance));
        if (format === null) {
            throw (0, errors_1.createUsageError)("Output format must be one of: text, json.", [`Received: ${String((0, global_format_1.getGlobalFormat)(commandInstance))}`]);
        }
        const repositoryContext = (0, runtime_contract_1.createRepositoryContext)((0, runtime_contract_1.detectRepositoryRoot)(), false);
        const options = commandInstance.opts();
        const result = (0, implementation_summary_reconciliation_1.buildReconciliationArtifact)(repositoryContext.root, {
            milestoneOverviewPath: options.milestoneOverview,
            taskDefinitionPath: options.taskDefinition,
            taskReconciliationRationale: options.rationale
        });
        const persistedArtifactLicenseReminder = (0, persisted_artifact_reminder_1.applyPlanningCommandPersistedArtifactReminderWrites)({
            repositoryContext,
            commandWriteSummary: result.commandWriteSummary,
            format
        });
        (0, output_1.emitSuccess)({
            command: "reconciliation build",
            format,
            text: (0, implementation_summary_reconciliation_1.renderReconciliationBuildResult)(result),
            data: {
                repositoryRoot: repositoryContext.root,
                detectionSource: repositoryContext.detectionSource,
                mode: result.mode,
                milestoneOverviewPath: result.milestoneOverviewPath,
                taskDefinitionPath: result.taskDefinitionPath,
                reconciliationPath: result.reconciliationPath,
                canonicalDestination: result.canonicalDestination,
                reviewHandoff: result.reviewHandoff,
                action: result.action,
                changed: result.changed,
                writeApplied: result.writeApplied,
                commandWriteSummary: persistedArtifactLicenseReminder.commandWriteSummary,
                persistedArtifactLicenseReminder,
                completedTaskSummaries: result.completedTaskSummaries,
                incompleteTaskSummaries: result.incompleteTaskSummaries
            }
        });
    });
    return command;
}
