/**
 * Registry for HTTP middleware providers.
 *
 * The registry collects middleware from multiple providers and returns
 * them in registration order when requested by client factories.
 *
 * ## Usage Modes
 *
 * **SDK Mode**: Register providers directly via `register()`:
 * ```typescript
 * globalMiddlewareRegistry.register(myProvider);
 * ```
 *
 * **CLI Mode**: Providers are collected via the `b2c:http-middleware` hook
 * and registered during command initialization.
 */
export class MiddlewareRegistry {
    providers = [];
    /**
     * Registers a middleware provider.
     *
     * Providers are called in registration order when middleware is requested.
     *
     * @param provider - The provider to register
     */
    register(provider) {
        this.providers.push(provider);
    }
    /**
     * Unregisters a middleware provider by name.
     *
     * @param name - The name of the provider to remove
     * @returns true if a provider was removed, false if not found
     */
    unregister(name) {
        const index = this.providers.findIndex((p) => p.name === name);
        if (index >= 0) {
            this.providers.splice(index, 1);
            return true;
        }
        return false;
    }
    /**
     * Collects middleware from all providers for a specific client type.
     *
     * @param clientType - The type of client requesting middleware
     * @returns Array of middleware in registration order
     */
    getMiddleware(clientType) {
        const middleware = [];
        for (const provider of this.providers) {
            const m = provider.getMiddleware(clientType);
            if (m) {
                middleware.push(m);
            }
        }
        return middleware;
    }
    /**
     * Clears all registered providers.
     *
     * Primarily useful for testing.
     */
    clear() {
        this.providers = [];
    }
    /**
     * Returns the number of registered providers.
     */
    get size() {
        return this.providers.length;
    }
    /**
     * Returns the names of all registered providers.
     */
    getProviderNames() {
        return this.providers.map((p) => p.name);
    }
}
/**
 * Global middleware registry instance.
 *
 * This is the default registry used by all B2C SDK clients. Register
 * middleware providers here to have them applied automatically.
 *
 * @example
 * ```typescript
 * import { globalMiddlewareRegistry } from '@salesforce/b2c-tooling-sdk/clients';
 *
 * globalMiddlewareRegistry.register({
 *   name: 'request-logger',
 *   getMiddleware() {
 *     return {
 *       onRequest({ request }) {
 *         console.log(`Request: ${request.method} ${request.url}`);
 *         return request;
 *       },
 *     };
 *   },
 * });
 * ```
 */
export const globalMiddlewareRegistry = new MiddlewareRegistry();
//# sourceMappingURL=middleware-registry.js.map