"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getWalletAPITransactionSignFlowInfos = exports.currencyToWalletAPICurrency = exports.accountToWalletAPIAccount = exports.setWalletApiIdForAccountId = exports.getWalletApiIdFromAccountId = exports.getAccountIdFromWalletAccountId = void 0;
const uuid_1 = require("uuid");
const store_1 = require("@ledgerhq/live-wallet/store");
const walletApiAdapter_1 = __importDefault(require("../generated/walletApiAdapter"));
const account_1 = require("../account");
const constants_1 = require("./constants");
// The namespace is a randomly generated uuid v4 from https://www.uuidgenerator.net/
const NAMESPACE = "c3c78073-6844-409e-9e75-171ab4c7f9a2";
const uuidToAccountId = new Map();
const getAccountIdFromWalletAccountId = (walletAccountId) => uuidToAccountId.get(walletAccountId);
exports.getAccountIdFromWalletAccountId = getAccountIdFromWalletAccountId;
const getWalletApiIdFromAccountId = (id) => (0, uuid_1.v5)(id, NAMESPACE);
exports.getWalletApiIdFromAccountId = getWalletApiIdFromAccountId;
const setWalletApiIdForAccountId = (accountId) => {
    const walletApiId = (0, exports.getWalletApiIdFromAccountId)(accountId);
    uuidToAccountId.set(walletApiId, accountId);
};
exports.setWalletApiIdForAccountId = setWalletApiIdForAccountId;
function accountToWalletAPIAccount(walletState, account, parentAccount) {
    const walletApiId = (0, exports.getWalletApiIdFromAccountId)(account.id);
    uuidToAccountId.set(walletApiId, account.id);
    if ((0, account_1.isTokenAccount)(account)) {
        if (!parentAccount) {
            throw new Error("No 'parentAccount' account provided for token account");
        }
        const parentWalletApiId = (0, exports.getWalletApiIdFromAccountId)(parentAccount.id);
        uuidToAccountId.set(parentWalletApiId, parentAccount.id);
        const parentAccountName = (0, store_1.accountNameWithDefaultSelector)(walletState, parentAccount);
        return {
            id: walletApiId,
            parentAccountId: parentWalletApiId,
            balance: account.balance,
            address: parentAccount.freshAddress,
            blockHeight: parentAccount.blockHeight,
            lastSyncDate: parentAccount.lastSyncDate,
            name: `${parentAccountName} (${account.token.ticker})`,
            currency: account.token.id,
            spendableBalance: account.spendableBalance,
        };
    }
    const name = (0, store_1.accountNameWithDefaultSelector)(walletState, account);
    return {
        id: walletApiId,
        name,
        address: account.freshAddress,
        currency: account.currency.id,
        balance: account.balance,
        spendableBalance: account.spendableBalance,
        blockHeight: account.blockHeight,
        lastSyncDate: account.lastSyncDate,
    };
}
exports.accountToWalletAPIAccount = accountToWalletAPIAccount;
function currencyToWalletAPICurrency(currency) {
    if (currency.type === "TokenCurrency") {
        return {
            type: "TokenCurrency",
            standard: "ERC20",
            id: currency.id,
            ticker: currency.ticker,
            contract: currency.contractAddress,
            name: currency.name,
            parent: currency.parentCurrency.id,
            color: currency.parentCurrency.color,
            decimals: currency.units[0].magnitude,
        };
    }
    return {
        type: "CryptoCurrency",
        id: currency.id,
        ticker: currency.ticker,
        name: currency.name,
        family: constants_1.FAMILIES_MAPPING_LL_TO_WAPI[currency.family] ?? currency.family,
        color: currency.color,
        decimals: currency.units[0].magnitude,
    };
}
exports.currencyToWalletAPICurrency = currencyToWalletAPICurrency;
const getWalletAPITransactionSignFlowInfos = ({ walletApiTransaction, account }) => {
    const liveFamily = constants_1.FAMILIES_MAPPING_WAPI_TO_LL[walletApiTransaction.family] ?? walletApiTransaction.family;
    const familyModule = walletApiAdapter_1.default[liveFamily];
    if (familyModule) {
        return familyModule.getWalletAPITransactionSignFlowInfos({ walletApiTransaction, account });
    }
    /**
     * If we don't have an explicit implementation for this family, we fallback
     * to just returning the transaction as is
     * This is not ideal and could lead to unforseen issues since we can't make
     * sure that what is received from the WalletAPI is compatible with the
     * Ledger Live implementation of the family
     * Not having an explicit WalletAPI adapter for a family should be considered
     * an error and thorw an exception
     */
    return {
        canEditFees: false,
        liveTx: { ...walletApiTransaction },
        hasFeesProvided: false,
    };
};
exports.getWalletAPITransactionSignFlowInfos = getWalletAPITransactionSignFlowInfos;
//# sourceMappingURL=converters.js.map