/*
 * Copyright (c) 2025, Salesforce, Inc.
 * SPDX-License-Identifier: Apache-2
 * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
 */
/**
 * Runner that manages cartridge providers and transformers.
 *
 * Collects providers from plugins and orchestrates discovery across
 * all sources with proper priority ordering and deduplication.
 */
export class CartridgeProviderRunner {
    providers = [];
    transformers = [];
    logger;
    constructor(logger) {
        this.logger = logger;
    }
    /**
     * Add providers and transformers to the runner.
     */
    addProviders(providers) {
        this.providers.push(...providers);
    }
    /**
     * Add transformers to the runner.
     */
    addTransformers(transformers) {
        this.transformers.push(...transformers);
    }
    /**
     * Get count of registered providers.
     */
    get providerCount() {
        return this.providers.length;
    }
    /**
     * Get count of registered transformers.
     */
    get transformerCount() {
        return this.transformers.length;
    }
    /**
     * Find cartridges using all registered providers and apply transformers.
     *
     * @param defaultCartridges - Cartridges from default discovery (SDK findCartridges)
     * @param options - Discovery options
     * @returns Final list of cartridge mappings
     */
    async findCartridges(defaultCartridges, options) {
        let cartridges = [];
        // Run 'before' providers first
        const beforeProviders = this.providers.filter((p) => p.priority === 'before');
        for (const provider of beforeProviders) {
            try {
                this.logger?.debug(`Running cartridge provider: ${provider.name} (before)`);
                const found = await provider.findCartridges(options);
                cartridges.push(...found);
                this.logger?.debug(`Provider ${provider.name} found ${found.length} cartridge(s)`);
            }
            catch (error) {
                this.logger?.debug(`Provider ${provider.name} failed: ${error instanceof Error ? error.message : String(error)}`);
            }
        }
        // Add default cartridges
        cartridges.push(...defaultCartridges);
        // Run 'after' providers
        const afterProviders = this.providers.filter((p) => p.priority === 'after');
        for (const provider of afterProviders) {
            try {
                this.logger?.debug(`Running cartridge provider: ${provider.name} (after)`);
                const found = await provider.findCartridges(options);
                cartridges.push(...found);
                this.logger?.debug(`Provider ${provider.name} found ${found.length} cartridge(s)`);
            }
            catch (error) {
                this.logger?.debug(`Provider ${provider.name} failed: ${error instanceof Error ? error.message : String(error)}`);
            }
        }
        // Deduplicate by name (first wins)
        const seen = new Set();
        cartridges = cartridges.filter((c) => {
            if (seen.has(c.name)) {
                return false;
            }
            seen.add(c.name);
            return true;
        });
        // Apply transformers in order
        for (const transformer of this.transformers) {
            try {
                this.logger?.debug(`Running cartridge transformer: ${transformer.name}`);
                cartridges = await transformer.transform(cartridges, options);
                this.logger?.debug(`Transformer ${transformer.name} returned ${cartridges.length} cartridge(s)`);
            }
            catch (error) {
                this.logger?.debug(`Transformer ${transformer.name} failed: ${error instanceof Error ? error.message : String(error)}`);
            }
        }
        return cartridges;
    }
}
//# sourceMappingURL=cartridge-providers.js.map