"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSpecListCommand = createSpecListCommand;
const node_fs_1 = __importDefault(require("node:fs"));
const errors_1 = require("../../../lib/errors");
const output_1 = require("../../../lib/output");
const global_format_1 = require("../global-format");
const include_archived_1 = require("../include-archived");
const spec_status_filter_1 = require("../spec-status-filter");
const runtime_contract_1 = require("../../../lib/runtime/runtime-contract");
const spec_registry_1 = require("../../../lib/spec-registry");
const path_resolution_1 = require("../../../lib/spec-registry/path-resolution");
async function createSpecListCommand() {
    const { Command } = await import("commander");
    const command = new Command("list");
    command
        .description("List lifecycle-visible spec registry entries with explicit status evidence.")
        .option("--status <status>", "Filter by lifecycle status. Repeat to select multiple statuses.", spec_status_filter_1.collectSpecStatus, [])
        .option("--include-archived", "Include archived registry entries in list output.")
        .action((_, commandInstance) => {
        const format = (0, output_1.resolveOutputFormat)((0, global_format_1.getGlobalFormat)(commandInstance));
        const includeArchived = (0, include_archived_1.getIncludeArchived)(commandInstance);
        const statuses = (0, spec_status_filter_1.getRequestedSpecStatuses)(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 = (0, runtime_contract_1.createRepositoryContext)((0, runtime_contract_1.detectRepositoryRoot)(), false);
        const specIndexPaths = (0, path_resolution_1.resolveSpecIndexPaths)(repositoryContext);
        const document = (0, spec_registry_1.parseSpecIndexDocumentText)(node_fs_1.default.readFileSync(specIndexPaths.absolutePath, "utf8"));
        const visibility = (0, spec_registry_1.selectVisibleSpecRegistryEntries)(document, { includeArchived, statuses });
        const summaries = (0, spec_registry_1.mapSpecListEntrySummaries)(visibility.entries);
        const activeEntries = document.entries.filter((entry) => entry.status === spec_registry_1.DEFAULT_SPEC_LIST_STATUS).length;
        const data = {
            repositoryRoot: repositoryContext.root,
            detectionSource: repositoryContext.detectionSource,
            specIndexPath: specIndexPaths.relativePath,
            totalEntries: document.entries.length,
            activeEntries,
            includeArchived,
            requestedStatuses: visibility.requestedStatuses,
            appliedStatuses: visibility.appliedStatuses,
            availableStatusCounts: visibility.availableStatusCounts,
            excludedStatusCounts: visibility.excludedStatusCounts,
            selectedEntries: visibility.selectedEntries,
            excludedEntries: visibility.excludedEntries,
            resultEntries: summaries.length,
            listedEntries: summaries.length,
            entries: summaries
        };
        (0, output_1.emitSuccess)({
            command: "spec list",
            format,
            text: ({ data: projected }) => (0, spec_registry_1.renderDefaultSpecListText)(projected.entries, { includeArchived, statuses }, visibility),
            data
        });
    });
    return command;
}
