"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCommandsDescribeCommand = createCommandsDescribeCommand;
const errors_1 = require("../../../lib/errors");
const output_1 = require("../../../lib/output");
async function createCommandsDescribeCommand() {
    const { Command } = await import("commander");
    const command = new Command("describe");
    command
        .description("Describe one authoritative command descriptor.")
        .argument("[command...]", "Exact command path, for example: task update")
        .option("--input-json <json>", "Structured request object with a command string array.")
        .action((segments, options, commandInstance) => {
        const format = (0, output_1.resolveOutputFormat)(commandInstance.optsWithGlobals?.().format);
        if (format === null)
            throw (0, errors_1.createUsageError)("Unsupported output format for commands describe.");
        let normalized;
        try {
            if (options.inputJson !== undefined) {
                if (segments.length > 0)
                    throw new Error("--input-json cannot be combined with command arguments.");
                const input = (0, output_1.parseStructuredCommandInput)(options.inputJson, ["command"]);
                normalized = normalizeDescribeRequest((0, output_1.requireStringArray)(input.command, "command", true), "structured");
            }
            else {
                normalized = normalizeDescribeRequest((0, output_1.requireStringArray)(segments, "command", true), "shell");
            }
        }
        catch (error) {
            throw (0, errors_1.createUsageError)(error instanceof Error ? error.message : "Invalid commands describe request.");
        }
        const commandName = normalized.request.command.join(" ");
        const descriptor = (0, output_1.getRegisteredCommandDescriptor)(commandName);
        if (descriptor === null) {
            throw (0, errors_1.createUsageError)(`Unknown command descriptor '${commandName}'.`, [
                `Recovery: run day-shift commands search ${JSON.stringify(commandName)} --format json.`
            ]);
        }
        const inspection = (0, output_1.projectCommandDescriptorInspection)(descriptor);
        (0, output_1.emitSuccess)({
            command: "commands describe",
            format,
            data: { descriptor: inspection, executable: inspection.availability === "callable", readOnly: true, ...normalized },
            text: [
                `${inspection.command}: ${inspection.summary}`,
                `Availability: ${inspection.availability}; capability: ${inspection.capability}; authorization: ${inspection.authorization}; writeCapable: ${inspection.writeCapable}`,
                `Example: ${inspection.examples[0]?.argv.join(" ") ?? "None (inspection only)."}`,
                (0, output_1.renderTerminalBoundary)({ kind: "read-only" })
            ].join("\n")
        });
    });
    return command;
}
function normalizeDescribeRequest(command, inputStyle) {
    const segments = Object.freeze([...command]);
    return {
        request: Object.freeze({ command: segments }),
        requestArgv: Object.freeze(["day-shift", "commands", "describe", ...segments]),
        inputStyle
    };
}
