"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAccountBridgeByFamily = exports.getAccountBridge = exports.getCurrencyBridge = void 0;
const index_1 = require("@ledgerhq/coin-framework/sanction/index");
const errors_1 = require("@ledgerhq/errors");
const account_1 = require("../account");
const live_env_1 = require("@ledgerhq/live-env");
const index_2 = require("../account/index");
const js_1 = __importDefault(require("../generated/bridge/js"));
const mock_1 = __importDefault(require("../generated/bridge/mock"));
const accountBridge_1 = require("./generic-alpaca/accountBridge");
const currencyBridge_1 = require("./generic-alpaca/currencyBridge");
const errors_2 = require("@ledgerhq/coin-framework/sanction/errors");
// Removed: stores are now managed globally by @ledgerhq/cryptoassets/cal-client/store
const alpacaized = {
    evm: true,
    xrp: true,
    stellar: true,
    tezos: true,
};
// let accountBridgeInstance: AccountBridge<any> | null = null;
const bridgeCache = {};
const currencyBridgeCache = {};
const getCurrencyBridge = (currency) => {
    if ((0, live_env_1.getEnv)("MOCK")) {
        const mockBridge = mock_1.default[currency.family];
        // TODO Remove once we delete mock bridges tests
        if (mockBridge) {
            if (typeof mockBridge.loadCoinConfig === "function") {
                mockBridge.loadCoinConfig();
            }
            return mockBridge.currencyBridge;
        }
        throw new errors_1.CurrencyNotSupported("no mock implementation available for currency " + currency.id, {
            currencyName: currency.id,
        });
    }
    if (alpacaized[currency.family]) {
        if (!currencyBridgeCache[currency.family]) {
            currencyBridgeCache[currency.family] = (0, currencyBridge_1.getAlpacaCurrencyBridge)(currency.family, "local");
        }
        return currencyBridgeCache[currency.family];
    }
    const jsBridge = js_1.default[currency.family];
    if (jsBridge) {
        return jsBridge.currencyBridge;
    }
    throw new errors_1.CurrencyNotSupported("no implementation available for currency " + currency.id, {
        currencyName: currency.id,
    });
};
exports.getCurrencyBridge = getCurrencyBridge;
const getAccountBridge = (account, parentAccount) => {
    const mainAccount = (0, account_1.getMainAccount)(account, parentAccount);
    const { currency } = mainAccount;
    const supportedError = (0, index_2.checkAccountSupported)(mainAccount);
    if (supportedError) {
        throw supportedError;
    }
    try {
        return getAccountBridgeByFamily(currency.family, mainAccount.id);
    }
    catch {
        throw new errors_1.CurrencyNotSupported("currency not supported " + currency.id, {
            currencyName: currency.id,
        });
    }
};
exports.getAccountBridge = getAccountBridge;
function getAccountBridgeByFamily(family, accountId) {
    if (accountId) {
        const { type } = (0, account_1.decodeAccountId)(accountId);
        if (type === "mock") {
            const mockBridge = mock_1.default[family];
            // TODO Remove once we delete mock bridges tests
            if (mockBridge) {
                if (typeof mockBridge.loadCoinConfig === "function") {
                    mockBridge.loadCoinConfig();
                }
                return wrapAccountBridge(mockBridge.accountBridge);
            }
        }
    }
    if (alpacaized[family]) {
        if (!bridgeCache[family]) {
            bridgeCache[family] = wrapAccountBridge((0, accountBridge_1.getAlpacaAccountBridge)(family, "local"));
        }
        return bridgeCache[family];
    }
    const jsBridge = js_1.default[family];
    if (!jsBridge) {
        throw new errors_1.CurrencyNotSupported("account bridge not found " + family);
    }
    return wrapAccountBridge(jsBridge.accountBridge);
}
exports.getAccountBridgeByFamily = getAccountBridgeByFamily;
// Removed: setup() is no longer needed. The store is now managed globally by @ledgerhq/cryptoassets/cal-client/store.
// Use setupCalClientStore() or setupMockCryptoAssetsStore() from @ledgerhq/cryptoassets/cal-client/test-helpers instead.
function wrapAccountBridge(bridge) {
    return {
        ...bridge,
        getTransactionStatus: async (...args) => {
            const blockchainTransactionStatus = await bridge.getTransactionStatus(...args);
            const account = args[0];
            if (!(0, index_1.isCheckSanctionedAddressEnabled)(account.currency)) {
                return blockchainTransactionStatus;
            }
            const commonTransactionStatus = await commonGetTransactionStatus(...args);
            return mergeResults(blockchainTransactionStatus, commonTransactionStatus);
        },
    };
}
function mergeResults(blockchainTransactionStatus, commonTransactionStatus) {
    const errors = { ...blockchainTransactionStatus.errors, ...commonTransactionStatus.errors };
    const warnings = { ...blockchainTransactionStatus.warnings, ...commonTransactionStatus.warnings };
    return { ...blockchainTransactionStatus, errors, warnings };
}
async function commonGetTransactionStatus(account, transaction) {
    const errors = {};
    const warnings = {};
    let isRecipientSanctioned = false;
    if (transaction.recipient && transaction.recipient !== "") {
        isRecipientSanctioned = await (0, index_1.isAddressSanctioned)(account.currency, transaction.recipient);
        if (isRecipientSanctioned) {
            errors.recipient = new errors_2.AddressesSanctionedError("AddressesSanctionedError", {
                addresses: [transaction.recipient],
            });
        }
    }
    const isSenderSanctioned = await (0, index_1.isAddressSanctioned)(account.currency, account.freshAddress);
    if (isSenderSanctioned) {
        errors.sender = new errors_2.AddressesSanctionedError("AddressesSanctionedError", {
            addresses: [account.freshAddress],
        });
    }
    return { errors, warnings };
}
//# sourceMappingURL=impl.js.map