/**
 * `olam worldspec schema --version 1 [--out <path>]` — emit a composite
 * JSON Schema (Draft 2019-09) for IDE integration (VS Code yaml.schemas,
 * etc). Single-source-of-truth: derived from WorldspecSchema via
 * zod-to-json-schema, so v1.x evolution stays in sync without drift.
 *
 * Defaults to stdout; pass --out to write a file.
 */
import { writeFileSync } from 'node:fs';
import { resolve as resolvePath } from 'node:path';
import { zodToJsonSchema } from 'zod-to-json-schema';
import { WorldspecSchema } from '@olam/core/src/worldspec/index.js';
import { EXIT_GENERIC_ERROR } from '../../exit-codes.js';
import { printError, printSuccess } from '../../output.js';
export function registerWorldspecSchema(parent) {
    parent
        .command('schema')
        .description('Emit composite JSON Schema for the worldspec (IDE / yaml.schemas)')
        .requiredOption('--version <n>', 'Schema version to emit (currently only 1 is supported)')
        .option('--out <path>', 'Write JSON Schema to this path instead of stdout')
        .action((opts) => {
        if (opts.version !== '1') {
            printError(`unsupported schema version "${opts.version}"; only "1" is shipped in this CLI`);
            process.exit(EXIT_GENERIC_ERROR);
        }
        // zod-to-json-schema target — explicit Draft 2019-09 (matches
        // VS Code yaml.schemas + most IDE consumers in the wild;
        // unevaluatedProperties from Draft 2020-12 is not yet broadly
        // supported by JSON-Schema-aware editors). When IDE coverage of
        // 2020-12 catches up, re-evaluate the upgrade. MEDIUM-fix (A3 CP3
        // audit 2026-05-09): aligned header comment + code + test
        // expectation on Draft 2019-09 (was inconsistent across all three).
        const schema = zodToJsonSchema(WorldspecSchema, {
            name: 'Worldspec',
            target: 'jsonSchema2019-09',
            $refStrategy: 'root',
        });
        const json = JSON.stringify(schema, null, 2);
        if (opts.out) {
            const absPath = resolvePath(process.cwd(), opts.out);
            try {
                writeFileSync(absPath, json + '\n', 'utf8');
            }
            catch (err) {
                printError(`failed to write ${absPath}: ${err.message}`);
                process.exit(EXIT_GENERIC_ERROR);
            }
            printSuccess(`wrote JSON Schema (v${opts.version}) to ${absPath}`);
            process.exit(0);
        }
        process.stdout.write(json + '\n');
        process.exit(0);
    });
}
//# sourceMappingURL=schema.js.map