"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteUserChainwatchAccounts = exports.updateTransactionsAlertsAddresses = exports.getSupportedChainsAccounts = void 0;
const ChainwatchAccountManager_1 = __importDefault(require("./ChainwatchAccountManager"));
const formatAccountsByCurrencies = (newAccounts, removedAccounts) => {
    const accountsByCurrencies = {};
    for (const newAccount of newAccounts) {
        if (!accountsByCurrencies[newAccount.currency.id]) {
            accountsByCurrencies[newAccount.currency.id] = { newAccounts: [], removedAccounts: [] };
        }
        accountsByCurrencies[newAccount.currency.id].newAccounts.push(newAccount);
    }
    for (const removedAccount of removedAccounts) {
        if (!accountsByCurrencies[removedAccount.currency.id]) {
            accountsByCurrencies[removedAccount.currency.id] = { newAccounts: [], removedAccounts: [] };
        }
        accountsByCurrencies[removedAccount.currency.id].removedAccounts.push(removedAccount);
    }
    return accountsByCurrencies;
};
const getSupportedChainsAccounts = (userId, chainwatchBaseUrl, supportedChains) => {
    return Promise.all(supportedChains.map(supportedChain => {
        const accountManager = new ChainwatchAccountManager_1.default(chainwatchBaseUrl, userId, supportedChain);
        return accountManager.getChainwatchAccount();
    }));
};
exports.getSupportedChainsAccounts = getSupportedChainsAccounts;
const updateTransactionsAlertsAddresses = async (userId, chainwatchBaseUrl, supportedChains, newAccounts, removedAccounts) => {
    const accountsByCurrencies = formatAccountsByCurrencies(newAccounts, removedAccounts);
    for (const [currencyId, accounts] of Object.entries(accountsByCurrencies)) {
        const network = supportedChains.find((chain) => chain.ledgerLiveId === currencyId);
        if (network) {
            const accountManager = new ChainwatchAccountManager_1.default(chainwatchBaseUrl, userId, network);
            await accountManager.setupChainwatchAccount();
            await Promise.all([
                accountManager.registerNewAccountsAddresses(accounts.newAccounts),
                accountManager.removeAccountsAddresses(accounts.removedAccounts),
            ]);
        }
    }
};
exports.updateTransactionsAlertsAddresses = updateTransactionsAlertsAddresses;
const deleteUserChainwatchAccounts = async (userId, chainwatchBaseUrl, supportedChains) => {
    for (const supportedChain of supportedChains) {
        const accountManager = new ChainwatchAccountManager_1.default(chainwatchBaseUrl, userId, supportedChain);
        const chainwatchAccount = await accountManager.getChainwatchAccount();
        if (chainwatchAccount) {
            await accountManager.removeChainwatchAccount();
        }
    }
};
exports.deleteUserChainwatchAccounts = deleteUserChainwatchAccounts;
//# sourceMappingURL=index.js.map