// Copyright (c) 2026 Hellmai Ltd
// SPDX-License-Identifier: AGPL-3.0-only
/**
 * Gate Registry
 *
 * WU-1550: Declarative gate registration pattern.
 *
 * Instead of hardcoding gate arrays inside executeGates(), gates are registered
 * via GateRegistry. New gates can be added by calling registry.register()
 * without modifying the core execution code.
 *
 * @module gate-registry
 */
import { createError, ErrorCodes } from '@lumenflow/core';
/**
 * Registry for quality gates.
 *
 * Gates are registered in order and executed sequentially.
 * New gates can be added by calling `register()` without modifying
 * the core executeGates() function.
 */
export class GateRegistry {
    gates = [];
    nameIndex = new Map();
    /**
     * Register a single gate definition.
     *
     * @param gate - Gate definition to register
     * @throws Error if a gate with the same name is already registered
     */
    register(gate) {
        if (this.nameIndex.has(gate.name)) {
            throw createError(ErrorCodes.TOOL_ALREADY_REGISTERED, `Gate "${gate.name}" is already registered`);
        }
        this.nameIndex.set(gate.name, this.gates.length);
        this.gates.push(gate);
    }
    /**
     * Register multiple gate definitions at once.
     *
     * @param gates - Array of gate definitions to register
     */
    registerAll(gates) {
        for (const gate of gates) {
            this.register(gate);
        }
    }
    /**
     * Get all registered gates in insertion order.
     *
     * @returns Copy of the gates array
     */
    getAll() {
        return [...this.gates];
    }
    /**
     * Get a gate by name.
     *
     * @param name - Gate name to look up
     * @returns Gate definition or undefined if not found
     */
    get(name) {
        const index = this.nameIndex.get(name);
        if (index === undefined)
            return undefined;
        return this.gates[index];
    }
    /**
     * Check if a gate with the given name is registered.
     *
     * @param name - Gate name to check
     * @returns true if the gate exists
     */
    has(name) {
        return this.nameIndex.has(name);
    }
    /**
     * Remove all registered gates.
     */
    clear() {
        this.gates.length = 0;
        this.nameIndex.clear();
    }
}
//# sourceMappingURL=gate-registry.js.map