/**
 * Schema vocabulary — the single source of "which keys exist + their exact
 * (canonical) names + the valid type tokens", DERIVED from the composed
 * `SPECVERSE-SCHEMA.json` (the same artifact the validator uses).
 *
 * Serializers consult this instead of hand-maintaining parallel key/section
 * lists (`knownSections`, `knownProps`, `normalizeTypeForSpec`'s token set),
 * which drift from the schema — that drift is TODO #77 (`import:` vs `imports:`)
 * and #86 (invalid type tokens). With the vocabulary sourced from the schema,
 * the only way to change it is to change the schema (R12) — there is no
 * hand-list to forget or perve. This is DP1=(c) of proposal
 * docs/proposals/in-progress/2026-05-26-CANONICAL-SPECLY-SERIALIZER.md.
 */
import { createRequire } from 'module';
import * as fs from 'fs';
import * as path from 'path';
let _cached = null;
/**
 * Load the composed `@specverse/entities/schema/SPECVERSE-SCHEMA.json` — the
 * same artifact `SpecVerseParserEngine` validates against. Mirrors
 * `loadDefaultSchema()` in `parser/index.ts`.
 */
export function loadComposedSchema() {
    const req = createRequire(import.meta.url);
    const entitiesPkg = req.resolve('@specverse/entities/package.json');
    const schemaPath = path.join(path.dirname(entitiesPkg), 'schema', 'SPECVERSE-SCHEMA.json');
    return JSON.parse(fs.readFileSync(schemaPath, 'utf8'));
}
function keysOf(def) {
    return new Set(Object.keys(def?.properties ?? {}));
}
function enumOf(def) {
    return new Set(Array.isArray(def?.enum) ? def.enum.map((v) => String(v)) : []);
}
/**
 * Derive the SpecVerse key + type vocabulary from a composed schema (defaults
 * to the installed `SPECVERSE-SCHEMA.json`). Cached when using the default.
 */
export function schemaVocabulary(schema) {
    if (!schema && _cached)
        return _cached;
    let s = schema;
    if (!s) {
        // Graceful: if the composed schema can't be resolved (unusual runtime
        // context), return an empty vocabulary rather than throwing — callers fall
        // back to their explicit handling. Cached so we don't retry the bad load.
        try {
            s = loadComposedSchema();
        }
        catch {
            s = null;
        }
    }
    const D = s?.$defs ?? {};
    const cc = D.ComponentsContainer;
    const compDef = cc?.patternProperties ? Object.values(cc.patternProperties)[0] : undefined;
    const prim = D.PrimitiveDefinition?.properties ?? {};
    const vocab = {
        componentKeys: keysOf(compDef),
        executableKeys: keysOf(D.ExecutableProperties),
        customActionKeys: keysOf(D.CustomActionProperties),
        baseTypeTokens: enumOf(prim.baseType),
        formatTokens: enumOf(prim.validation?.properties?.format),
    };
    if (!schema)
        _cached = vocab;
    return vocab;
}
//# sourceMappingURL=schema-vocabulary.js.map