"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSpecArchiveCommand = createSpecArchiveCommand;
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 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 createSpecArchiveCommand() {
    const { Command } = await import("commander");
    const command = new Command("archive");
    command
        .description("Archive an active spec entry without removing it from the registry.")
        .argument("<id>", "Registered spec id to archive.")
        .action((entryId, ...actionArgs) => {
        const commandInstance = actionArgs.at(-1);
        const format = (0, output_1.resolveOutputFormat)((0, global_format_1.getGlobalFormat)(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 currentIndex = (0, spec_registry_1.parseSpecIndexDocumentText)(node_fs_1.default.readFileSync(specIndexPaths.absolutePath, "utf8"));
        const archiveResult = (0, spec_registry_1.applySpecRegistryArchive)({
            entryId,
            currentDocument: currentIndex
        });
        if (!archiveResult.ok) {
            throw new errors_1.RuntimeError({
                message: "Spec archive validation failed.",
                code: "SPEC_ARCHIVE_VALIDATION_FAILED",
                exitCode: errors_1.RuntimeExitCode.ERROR,
                details: archiveResult.findings.map((finding) => finding.message),
                findings: [...archiveResult.findings]
            });
        }
        node_fs_1.default.writeFileSync(specIndexPaths.absolutePath, (0, spec_registry_1.renderSpecIndexDocument)(archiveResult.document), "utf8");
        (0, output_1.emitSuccess)({
            command: "spec archive",
            format,
            text: renderSpecArchiveWriteText(archiveResult.entry.id),
            data: {
                repositoryRoot: repositoryContext.root,
                detectionSource: repositoryContext.detectionSource,
                specIndexPath: specIndexPaths.relativePath,
                entry: archiveResult.entry
            }
        });
    });
    return command;
}
function renderSpecArchiveWriteText(entryId) {
    return [
        "Archived spec entry.",
        `Entry id: ${entryId}`
    ].join("\n");
}
