"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPlanningNextActionCommand = createPlanningNextActionCommand;
const errors_1 = require("../../../lib/errors");
const output_1 = require("../../../lib/output");
const planning_navigation_1 = require("../../../lib/planning/planning-navigation");
const runtime_contract_1 = require("../../../lib/runtime/runtime-contract");
const global_format_1 = require("../global-format");
async function createPlanningNextActionCommand() {
    const { Command } = await import('commander');
    const command = new Command('next-action');
    command
        .description('Suggest the most likely valid next planning or review step, prioritizing parent-contract refinement blockers, for one selected artifact.')
        .requiredOption('--artifact <path>', 'Repository-relative planning artifact path to inspect.')
        .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 = resolveRepositoryContext(commandInstance);
        const options = commandInstance.opts();
        const result = (0, planning_navigation_1.suggestPlanningNextAction)({
            repositoryRoot: repositoryContext.root,
            artifactPath: options.artifact,
        });
        (0, output_1.emitSuccess)({
            command: 'planning next-action',
            format,
            text: (0, planning_navigation_1.renderPlanningNextActionResult)(result),
            data: {
                repositoryRoot: repositoryContext.root,
                detectionSource: repositoryContext.detectionSource,
                artifactPath: result.artifactPath,
                artifactType: result.artifactType,
                recommendation: result.recommendation,
                reason: result.reason,
                commandSuggestion: result.command,
                optionalReviewCommand: result.optionalReviewCommand ?? null,
                topologyEvidence: result.topologyEvidence ?? null,
                workflowPath: result.workflowPath,
                promptPath: result.promptPath,
                promptResolution: result.promptResolution,
                scaffoldRefinementFindings: result.scaffoldRefinementFindings ?? [],
                workEvidence: result.workEvidence ?? null,
            },
        });
    });
    return command;
}
function resolveRepositoryContext(commandInstance) {
    const rootCommand = (commandInstance?.parent?.parent ?? commandInstance);
    if (rootCommand?.configRepositoryContext) {
        return rootCommand.configRepositoryContext;
    }
    return (0, runtime_contract_1.createRepositoryContext)((0, runtime_contract_1.detectRepositoryRoot)(), false);
}
