"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createImplementationSummaryBuildCommand = createImplementationSummaryBuildCommand;
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 createImplementationSummaryBuildCommand() {
    const { Command } = await import("commander");
    const command = new Command("build");
    command
        .description("Refresh the paired implementation-summary artifact at its canonical task-local destination.")
        .requiredOption("--task-definition <path>", "Repository-relative path to the task-definition.md file that owns the paired implementation summary.")
        .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.buildImplementationSummaryArtifact)(repositoryContext.root, {
            taskDefinitionPath: options.taskDefinition,
            expectedTaskRevisions: (0, output_1.getSelectedLifecycleRunArtifactRevisionBindings)()
        });
        const persistedArtifactLicenseReminder = (0, persisted_artifact_reminder_1.applyPlanningCommandPersistedArtifactReminderWrites)({
            repositoryContext,
            commandWriteSummary: result.commandWriteSummary,
            format
        });
        (0, output_1.emitSuccess)({
            command: "implementation-summary build",
            format,
            text: (0, implementation_summary_reconciliation_1.renderImplementationSummaryBuildResult)(result),
            data: {
                repositoryRoot: repositoryContext.root,
                detectionSource: repositoryContext.detectionSource,
                taskDefinitionPath: result.taskDefinitionPath,
                implementationSummaryPath: result.implementationSummaryPath,
                canonicalDestination: result.canonicalDestination,
                reviewHandoff: result.reviewHandoff,
                action: result.action,
                changed: result.changed,
                writeApplied: result.writeApplied,
                commandWriteSummary: persistedArtifactLicenseReminder.commandWriteSummary,
                persistedArtifactLicenseReminder,
                taskScopedChangeReport: result.taskScopedChangeReport,
                candidatePaths: result.changedPathAuthoring.candidatePaths,
                candidateMarkdown: result.changedPathAuthoring.renderedMarkdown,
                excludedPaths: result.changedPathAuthoring.excludedPaths,
                changedPathAuthoring: result.changedPathAuthoring,
                jsonOutputBoundary: {
                    stdoutDocumentCount: 1,
                    diagnostics: "structured-envelope-or-stderr",
                    unrelatedPathDetailsIncluded: result.changedPathAuthoring.excludedPaths.some((entry) => entry.reason === "outside-declared-attempt-scope")
                }
            }
        });
    });
    return command;
}
