"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTaskBaselineCommand = createTaskBaselineCommand;
const errors_1 = require("../../../lib/errors");
const output_1 = require("../../../lib/output");
const task_cycle_runtime_attribution_1 = require("../../../lib/planning/task-cycle-runtime-attribution");
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 createTaskBaselineCommand() {
    const { Command } = await import("commander");
    const command = new Command("baseline");
    command
        .description("Capture or validate one post-readiness, pre-write runtime-attribution baseline on a task definition.")
        .requiredOption("--task-definition <path>", "Repository-relative task-definition.md path.")
        .requiredOption("--task-cycle-id <identity>", "Stable identity for the authorized task implementation cycle.")
        .requiredOption("--checkpoint-id <identity>", "Stable identity for the successful readiness checkpoint.")
        .option("--resume", "Reuse only an existing baseline with the exact task-cycle and checkpoint identities.")
        .option("--adopt <path::rationale>", "Explicitly adopt a dirty declared target with a concrete rationale. Repeat per adopted path.", collectValue, [])
        .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.");
        }
        const repositoryContext = (0, runtime_contract_1.createRepositoryContext)((0, runtime_contract_1.detectRepositoryRoot)(), false);
        const options = commandInstance.opts();
        const result = (0, task_cycle_runtime_attribution_1.captureTaskCycleRuntimeBaseline)(repositoryContext.root, {
            taskDefinitionPath: options.taskDefinition,
            taskCycleId: options.taskCycleId,
            checkpointId: options.checkpointId,
            resume: options.resume === true,
            adoptions: options.adopt
        });
        const persistedArtifactLicenseReminder = (0, persisted_artifact_reminder_1.applyPlanningCommandPersistedArtifactReminderWrites)({
            repositoryContext,
            commandWriteSummary: result.commandWriteSummary,
            format
        });
        (0, output_1.emitSuccess)({
            command: "task baseline",
            format,
            text: [
                "Task runtime-attribution baseline",
                "",
                `Task definition: ${result.taskDefinitionPath}`,
                `Action: ${result.action}`,
                `Baseline ID: ${result.baseline.baselineId}`,
                `Task-cycle ID: ${result.baseline.taskCycleId}`,
                `Checkpoint ID: ${result.baseline.checkpointId}`,
                `Declared targets: ${result.baseline.declaredTargets.length}`,
                `Write applied: ${result.writeApplied ? "yes" : "no"}`
            ].join("\n"),
            data: {
                repositoryRoot: repositoryContext.root,
                detectionSource: repositoryContext.detectionSource,
                taskDefinitionPath: result.taskDefinitionPath,
                action: result.action,
                changed: result.changed,
                writeApplied: result.writeApplied,
                baseline: result.baseline,
                commandWriteSummary: persistedArtifactLicenseReminder.commandWriteSummary,
                persistedArtifactLicenseReminder
            }
        });
    });
    return command;
}
function collectValue(value, previous) {
    previous.push(value);
    return previous;
}
