var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var BlockchainProviderModule_1;
import { v4 as uuidv4 } from 'uuid';
import { Module, Logger } from '@nestjs/common';
import { BlockchainProviderService, NetworkConnectionManager, MempoolConnectionManager, } from '../../core/blockchain-provider';
import { createProvider } from '../../core/blockchain-provider/providers';
let BlockchainProviderModule = BlockchainProviderModule_1 = class BlockchainProviderModule {
    static async forRootAsync(options) {
        const { providers, isGlobal, rateLimits, network } = options;
        const instances = providers.connections.map((conn, i) => createProvider({
            type: providers.type,
            uniqName: `browser_${providers.type}_${i + 1}_${uuidv4()}`,
            httpUrl: conn.httpUrl,
            rateLimits,
            network,
        }));
        return {
            module: BlockchainProviderModule_1,
            global: isGlobal || false,
            providers: [
                {
                    provide: NetworkConnectionManager,
                    useFactory: async () => {
                        const manager = new NetworkConnectionManager({
                            providers: instances,
                            logger: new Logger(NetworkConnectionManager.name),
                        });
                        await manager.initialize();
                        return manager;
                    },
                    inject: [],
                },
                {
                    provide: MempoolConnectionManager,
                    useFactory: async () => {
                        const manager = new MempoolConnectionManager({
                            providers: [],
                            logger: new Logger(MempoolConnectionManager.name),
                        });
                        await manager.initialize();
                        return manager;
                    },
                    inject: [],
                },
                {
                    provide: BlockchainProviderService,
                    useFactory: (networkManager, mempoolManager) => new BlockchainProviderService(networkManager, mempoolManager, network),
                    inject: [NetworkConnectionManager, MempoolConnectionManager],
                },
            ],
            exports: [BlockchainProviderService],
        };
    }
};
BlockchainProviderModule = BlockchainProviderModule_1 = __decorate([
    Module({})
], BlockchainProviderModule);
export { BlockchainProviderModule };
