import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { createContext } from '../context.js';
import { writeOutput } from '../output.js';
export function runCatalog(args) {
    const ctx = createContext(args);
    try {
        const catalog = ctx.registry.catalog(ctx.reader, ctx.db);
        writeOutput({ ...catalog, claude4spec: readPackageVersion() }, args);
    }
    finally {
        ctx.close();
    }
}
function readPackageVersion() {
    try {
        const here = path.dirname(fileURLToPath(import.meta.url));
        // dist/bin/c4s/commands → packageRoot, or src/bin/c4s/commands → packageRoot
        const roots = [
            path.resolve(here, '..', '..', '..', '..'),
            path.resolve(here, '..', '..', '..'),
        ];
        for (const root of roots) {
            const pkgPath = path.join(root, 'package.json');
            if (fs.existsSync(pkgPath)) {
                const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
                if (pkg.version)
                    return pkg.version;
            }
        }
    }
    catch {
        /* ignore */
    }
    return 'unknown';
}
//# sourceMappingURL=catalog.js.map