"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Enums = void 0;
const decorators_1 = require("../decorators");
const condition_evaluator_1 = require("../condition-evaluator");
const enum_utils_1 = require("./enum-utils");
/**
 * Enums step - filters enum values based on conditions.
 * If multiple @enums decorators exist on the same property, the first matching condition wins.
 */
class Enums {
    constructor() {
        this.name = 'enums';
    }
    /**
     * Applies enum filtering to all properties in the definition.
     *
     * @param target - The class prototype containing decorator metadata
     * @param definition - The schema definition to modify
     * @param context - Runtime context for condition evaluation
     */
    init(target, definition, context) {
        if (!definition?.properties || !target) {
            return;
        }
        const evaluator = new condition_evaluator_1.ConditionEvaluator(context);
        for (const propertyName in definition.properties) {
            const property = definition.properties[propertyName];
            const enumsMetadataList = Reflect.getMetadata(decorators_1.metadataKeys.enums, target, propertyName);
            if (!enumsMetadataList?.length) {
                continue;
            }
            // Find first matching condition (first match wins)
            for (const enumsMetadata of enumsMetadataList) {
                const { passed } = evaluator.evaluate(enumsMetadata);
                if (passed) {
                    const currentEnumValues = (0, enum_utils_1.resolveEnumFromProperty)(property, context);
                    if (currentEnumValues) {
                        // Filter to only allowed values that exist in original enum
                        const filteredValues = enumsMetadata.allowedValues.filter((v) => currentEnumValues.includes(v));
                        (0, enum_utils_1.applyFilteredEnumToProperty)(property, filteredValues, context);
                    }
                    break; // First match wins, stop processing
                }
            }
        }
    }
}
exports.Enums = Enums;
//# sourceMappingURL=Enums.js.map