"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCommandsInventoryCommand = createCommandsInventoryCommand;
const output_1 = require("../../../lib/output");
const errors_1 = require("../../../lib/errors");
function resolveRootCommand(commandInstance) {
    let root = commandInstance;
    while (root.parent !== null && root.parent !== undefined) {
        root = root.parent;
    }
    return root;
}
async function createCommandsInventoryCommand() {
    const { Command } = await import('commander');
    const command = new Command('inventory');
    command
        .description('Report a machine-readable inventory of callable commands and capability metadata.')
        .action((_, commandInstance) => {
        const requestedFormat = commandInstance.optsWithGlobals?.().format;
        const format = (0, output_1.resolveOutputFormat)(requestedFormat);
        if (format === null) {
            throw (0, errors_1.createUnsupportedOutputFormatError)({
                received: requestedFormat,
                commandBoundary: 'commands inventory output format',
            });
        }
        const inventory = (0, output_1.buildCommandInventory)(resolveRootCommand(commandInstance));
        const findings = (0, output_1.createCommandInventoryFindings)(inventory.coverage);
        (0, output_1.emitSuccess)({
            command: 'commands inventory',
            format,
            text: ({ data: projected, page }) => renderCommandInventoryText(projected, page?.lane),
            data: inventory,
            findings: [...findings],
        });
    });
    return command;
}
function renderCommandInventoryText(inventory, pagedLane) {
    const coverage = inventory.coverage;
    const findingEntries = [
        {
            label: 'Callable commands missing capability metadata',
            lines: [
                coverage.callableMissingCapability.length === 0 ? 'None.' : coverage.callableMissingCapability.join(', '),
            ],
        },
        {
            label: 'Registry-only capability entries',
            lines: [coverage.registryOnly.length === 0 ? 'None.' : coverage.registryOnly.join(', ')],
        },
        {
            label: 'Mixed-posture families',
            lines: [
                coverage.mixedPostureFamilies.length === 0
                    ? 'None.'
                    : coverage.mixedPostureFamilies.map((entry) => entry.command).join(', '),
            ],
        },
    ];
    return [
        (0, output_1.renderTerminalSection)({
            heading: 'Command inventory',
            headingStyle: 'title',
            fields: [
                { label: 'Schema version', value: inventory.schemaVersion },
                { label: 'Callable commands', value: coverage.callableCount },
                { label: 'Capability registry entries', value: coverage.registryCount },
                { label: 'Inventory entries', value: coverage.mergedCount },
                { label: 'Generated from', value: inventory.generatedFrom.join(', ') },
            ],
        }),
        (0, output_1.renderTerminalCompactList)({
            heading: 'Coverage findings',
            entries: findingEntries,
        }),
        ...(pagedLane === 'entries'
            ? [
                (0, output_1.renderTerminalCompactList)({
                    heading: 'Command entries',
                    entries: inventory.entries.map((entry) => ({
                        label: entry.command,
                        fields: [
                            { label: 'Availability', value: entry.availability },
                            { label: 'Callable', value: entry.callable },
                            { label: 'Capability', value: entry.capability },
                            { label: 'Mutation posture', value: entry.mutationPosture },
                        ],
                        lines: [entry.description],
                    })),
                }),
            ]
            : []),
        (0, output_1.renderTerminalBoundary)({ kind: 'read-only' }),
        inventory.noWriteBoundary,
    ].join('\n\n');
}
