// engine/event-schema-validator/change-classifier.ts — Schema change classification (SPEC-031)
import { asStringArray, asPropertiesMap } from './helpers.js';
/**
 * Compare two JSON Schema objects and classify the kind of change.
 *
 * Rules (conservative — any ambiguity is treated as breaking):
 * - New optional field added → additive
 * - Field removed → breaking
 * - Field type changed → breaking
 * - New required field → breaking (consumers may not send it)
 * - Constraint tightened (enum shrinks, minLength grows, etc.) → breaking
 * - Field added with `deprecated` marker → deprecation
 */
export function classifyChange(oldSchema, newSchema) {
    const oldRequired = asStringArray(oldSchema.required);
    const newRequired = asStringArray(newSchema.required);
    const oldProps = asPropertiesMap(oldSchema.properties);
    const newProps = asPropertiesMap(newSchema.properties);
    for (const field of oldRequired) {
        if (!newRequired.includes(field)) {
            return 'breaking';
        }
    }
    for (const field of newRequired) {
        if (!oldRequired.includes(field)) {
            return 'breaking';
        }
    }
    for (const field of Object.keys(oldProps)) {
        if (!(field in newProps)) {
            return 'breaking';
        }
    }
    for (const [field, newProp] of Object.entries(newProps)) {
        const oldProp = oldProps[field];
        if (!oldProp) {
            if (newProp.deprecated === true) {
                return 'deprecation';
            }
            continue;
        }
        if (oldProp.type !== newProp.type) {
            return 'breaking';
        }
        const oldEnum = oldProp.enum;
        const newEnum = newProp.enum;
        if (oldEnum !== undefined || newEnum !== undefined) {
            if (JSON.stringify(oldEnum) !== JSON.stringify(newEnum)) {
                return 'breaking';
            }
        }
        if (!oldProp.deprecated && newProp.deprecated === true) {
            return 'deprecation';
        }
    }
    return 'additive';
}
//# sourceMappingURL=change-classifier.js.map