// Copyright (c) 2026 Hellmai Ltd
// SPDX-License-Identifier: AGPL-3.0-only
import { createError, ErrorCodes } from '@lumenflow/core';
/**
 * Registry for wu-done validators.
 *
 * Validators are registered with a phase (preflight or completion) and
 * executed in insertion order within each phase.
 */
export class ValidatorRegistry {
    validators = [];
    nameIndex = new Map();
    /**
     * Register a single validator definition.
     *
     * @param validator - Validator definition to register
     * @throws Error if a validator with the same name is already registered
     */
    register(validator) {
        if (this.nameIndex.has(validator.name)) {
            throw createError(ErrorCodes.TOOL_ALREADY_REGISTERED, `Validator "${validator.name}" is already registered`);
        }
        this.nameIndex.set(validator.name, this.validators.length);
        this.validators.push(validator);
    }
    /**
     * Get validators for a specific phase, in insertion order.
     *
     * @param phase - Phase to filter by
     * @returns Copy of the validators for the given phase
     */
    getByPhase(phase) {
        return this.validators.filter((v) => v.phase === phase).map((v) => ({ ...v }));
    }
    /**
     * Get all registered validators across all phases.
     *
     * @returns Copy of all validators
     */
    getAll() {
        return [...this.validators];
    }
    /**
     * Check if a validator with the given name is registered.
     *
     * @param name - Validator name to check
     * @returns true if the validator exists
     */
    has(name) {
        return this.nameIndex.has(name);
    }
    /**
     * Get all phases that have at least one registered validator.
     *
     * @returns Array of phase names
     */
    getPhases() {
        const phases = new Set();
        for (const v of this.validators) {
            phases.add(v.phase);
        }
        return [...phases];
    }
    /**
     * Remove all registered validators.
     */
    clear() {
        this.validators.length = 0;
        this.nameIndex.clear();
    }
}
//# sourceMappingURL=validator-registry.js.map