import ChainwatchAccountManager from "./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;
};
export const getSupportedChainsAccounts = (userId, chainwatchBaseUrl, supportedChains) => {
    return Promise.all(supportedChains.map(supportedChain => {
        const accountManager = new ChainwatchAccountManager(chainwatchBaseUrl, userId, supportedChain);
        return accountManager.getChainwatchAccount();
    }));
};
export 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(chainwatchBaseUrl, userId, network);
            await accountManager.setupChainwatchAccount();
            await Promise.all([
                accountManager.registerNewAccountsAddresses(accounts.newAccounts),
                accountManager.removeAccountsAddresses(accounts.removedAccounts),
            ]);
        }
    }
};
export const deleteUserChainwatchAccounts = async (userId, chainwatchBaseUrl, supportedChains) => {
    for (const supportedChain of supportedChains) {
        const accountManager = new ChainwatchAccountManager(chainwatchBaseUrl, userId, supportedChain);
        const chainwatchAccount = await accountManager.getChainwatchAccount();
        if (chainwatchAccount) {
            await accountManager.removeChainwatchAccount();
        }
    }
};
//# sourceMappingURL=index.js.map