"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createWorkCandidatesCommand = createWorkCandidatesCommand;
const errors_1 = require("../../../lib/errors");
const output_1 = require("../../../lib/output");
const planning_navigation_1 = require("../../../lib/planning/planning-navigation");
const config_store_1 = require("../../../lib/config/config-store");
const cli_constants_1 = require("../../../lib/config/cli-constants");
const runtime_contract_1 = require("../../../lib/runtime/runtime-contract");
async function createWorkCandidatesCommand() {
    const { Command } = await import('commander');
    const command = new Command('candidates');
    command
        .description('Report bounded advisory candidates for explicit planning-work selection without writing state.')
        .option('--root <path>', 'Repository-relative planning root or subdirectory to inspect.')
        .option('--project <id>', 'Report the view for one declared monorepo project.')
        .option('--max-results <count>', `Maximum candidates from 1 to ${cli_constants_1.WORK_CANDIDATES_MAX_RESULTS}.`, String(cli_constants_1.WORK_CANDIDATES_DEFAULT_MAX_RESULTS))
        .action((options, commandInstance) => {
        const format = (0, output_1.resolveOutputFormat)(commandInstance.optsWithGlobals?.().format);
        if (format === null) {
            throw (0, errors_1.createUsageError)('Output format must be one of: text, json.');
        }
        const maxResults = Number(options.maxResults);
        if (!Number.isInteger(maxResults) || maxResults < 1 || maxResults > cli_constants_1.WORK_CANDIDATES_MAX_RESULTS) {
            throw (0, errors_1.createUsageError)(`Work candidate limit must be an integer from 1 to ${cli_constants_1.WORK_CANDIDATES_MAX_RESULTS}.`, [
                'Use --max-results <count>. No selection state was written.',
            ]);
        }
        const repositoryContext = resolveRepositoryContext(commandInstance);
        const resolvedConfig = (0, config_store_1.readResolvedConfig)(repositoryContext, {
            projectId: options.project,
            currentDirectory: process.cwd(),
        }).resolved;
        const result = (0, planning_navigation_1.discoverPlanningWorkCandidates)({
            repositoryRoot: repositoryContext.root,
            rootPath: options.root ?? resolvedConfig.values.planning_root,
            planningRootPath: resolvedConfig.values.planning_root,
            limit: maxResults,
            projectContext: resolvedConfig.projectContext,
        });
        (0, output_1.emitSuccess)({
            command: 'work candidates',
            format,
            text: (0, planning_navigation_1.renderPlanningWorkCandidatesResult)(result),
            data: {
                repositoryRoot: repositoryContext.root,
                detectionSource: repositoryContext.detectionSource,
                ...(resolvedConfig.projectContext.projects.length > 0
                    ? { projectContext: resolvedConfig.projectContext }
                    : {}),
                ...result,
                noWriteBoundary: 'Candidate evidence is advisory and does not create, update, or clear selection state.',
            },
        });
    });
    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);
}
