"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Modules = exports.BaseModule = void 0;
const logger_1 = __importDefault(require("../logger"));
const chain_state_1 = require("../providers/chain-state");
const libs_1 = require("../providers/libs");
const api_1 = require("../services/api");
const config_1 = require("../services/config");
const event_1 = require("../services/event");
const p2p_1 = require("../services/p2p");
const storage_1 = require("../services/storage");
const verification_1 = require("../services/verification");
class BaseModule {
    constructor(bitcoreServices = { P2P: p2p_1.P2P, Storage: storage_1.Storage, Event: event_1.Event, Api: api_1.Api, Config: config_1.Config, CSP: chain_state_1.ChainStateProvider, Libs: libs_1.Libs, Verification: verification_1.Verification }) {
        this.bitcoreServices = bitcoreServices;
        this.internalServices = new Array();
    }
    async start() {
        for (const service of this.internalServices) {
            await service.start();
        }
    }
    async stop() {
        for (const service of this.internalServices.reverse()) {
            await service.stop();
        }
    }
}
exports.BaseModule = BaseModule;
class ModuleManager extends BaseModule {
    constructor() {
        super(...arguments);
        this.internalServices = new Array();
        // Chain names -> module paths map
        this.DEFAULT_MODULE_PATHS = {
            BTC: './bitcoin',
            ETH: './ethereum',
            MATIC: './matic',
            BCH: './bitcoin-cash',
            DOGE: './dogecoin',
            LTC: './litecoin',
            XRP: './ripple',
            SOL: './solana'
        };
    }
    loadConfigured(params = {}) {
        const chains = params.chain ? [params.chain] : config_1.Config.chains();
        // Auto register known modules from config.chains
        for (const chain of chains) {
            let modulePath = this.DEFAULT_MODULE_PATHS[chain];
            // Register for each
            const networks = params.network ? [params.network] : config_1.Config.networksFor(chain);
            for (const network of networks) {
                const config = config_1.Config.chainConfig({ chain, network });
                modulePath = config.module || modulePath; // custom module path
                if (!modulePath) {
                    logger_1.default.warn(`Module not found for ${chain}:${network}. Did you forget to specify 'module' in the config?`);
                    continue;
                }
                logger_1.default.info(`Registering module for ${chain}:${network}: ${modulePath}`);
                // eslint-disable-next-line @typescript-eslint/no-require-imports
                const ModuleClass = require(modulePath).default || require(modulePath);
                this.internalServices.push(new ModuleClass(this.bitcoreServices, chain, network, config));
            }
        }
    }
}
exports.Modules = new ModuleManager();
//# sourceMappingURL=index.js.map