"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_contract_1 = require("../../../lib/config/config-contract");
const runtime_contract_1 = require("../../../lib/runtime/runtime-contract");
const DEFAULT_MAX_RESULTS = 10;
const MAX_RESULTS = 50;
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("--max-results <count>", `Maximum candidates from 1 to ${MAX_RESULTS}.`, String(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 > MAX_RESULTS) {
            throw (0, errors_1.createUsageError)(`Work candidate limit must be an integer from 1 to ${MAX_RESULTS}.`, [
                "Use --max-results <count>. No selection state was written."
            ]);
        }
        const repositoryContext = resolveRepositoryContext(commandInstance);
        const resolvedConfig = (0, config_contract_1.resolveDayShiftConfig)({ repositoryContext });
        const result = (0, planning_navigation_1.discoverPlanningWorkCandidates)({
            repositoryRoot: repositoryContext.root,
            rootPath: options.root ?? resolvedConfig.values.planning_root,
            limit: maxResults
        });
        (0, output_1.emitSuccess)({
            command: "work candidates",
            format,
            text: (0, planning_navigation_1.renderPlanningWorkCandidatesResult)(result),
            data: {
                repositoryRoot: repositoryContext.root,
                detectionSource: repositoryContext.detectionSource,
                ...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);
}
