import { getCurrencyForAccount, } from "@ledgerhq/types-live";
import { accountToWalletAPIAccount, getWalletAPITransactionSignFlowInfos, getAccountIdFromWalletAccountId, } from "./converters";
import { isTokenAccount, isAccount, getMainAccount, makeEmptyTokenAccount, getParentAccount, } from "../account/index";
import { prepareMessageToSign } from "../hw/signMessage/index";
import { getAccountBridge } from "../bridge";
import { getCryptoAssetsStore } from "@ledgerhq/cryptoassets/state";
import { getWalletAccount } from "@ledgerhq/coin-bitcoin/wallet-btc/index";
export function translateContent(content, locale = "en") {
    if (!content || typeof content === "string")
        return content;
    return content[locale] || content.en;
}
export async function receiveOnAccountLogic(walletState, { manifest, accounts, tracking }, walletAccountId, uiNavigation, tokenCurrency) {
    tracking.receiveRequested(manifest);
    const accountId = getAccountIdFromWalletAccountId(walletAccountId);
    if (!accountId) {
        tracking.receiveFail(manifest);
        throw new Error(`accountId ${walletAccountId} unknown`);
    }
    const account = accounts.find(account => account.id === accountId);
    if (!account) {
        tracking.receiveFail(manifest);
        throw new Error("Account required");
    }
    const parentAccount = getParentAccount(account, accounts);
    const mainAccount = getMainAccount(account, parentAccount);
    const currency = tokenCurrency ? await getCryptoAssetsStore().findTokenById(tokenCurrency) : null;
    const receivingAccount = currency ? makeEmptyTokenAccount(mainAccount, currency) : account;
    const accountAddress = accountToWalletAPIAccount(walletState, account, parentAccount).address;
    return uiNavigation(receivingAccount, parentAccount, accountAddress);
}
export async function signTransactionLogic({ manifest, accounts, tracking }, walletAccountId, transaction, uiNavigation, tokenCurrency, isEmbeddedSwap, partner) {
    tracking.signTransactionRequested(manifest, isEmbeddedSwap, partner);
    if (!transaction) {
        tracking.signTransactionFail(manifest, isEmbeddedSwap, partner);
        throw new Error("Transaction required");
    }
    const accountId = getAccountIdFromWalletAccountId(walletAccountId);
    if (!accountId) {
        tracking.signTransactionFail(manifest, isEmbeddedSwap, partner);
        throw new Error(`accountId ${walletAccountId} unknown`);
    }
    const account = accounts.find(account => account.id === accountId);
    if (!account) {
        tracking.signTransactionFail(manifest, isEmbeddedSwap, partner);
        throw new Error("Account required");
    }
    const parentAccount = getParentAccount(account, accounts);
    const accountFamily = isTokenAccount(account)
        ? parentAccount?.currency.family
        : account.currency.family;
    const mainAccount = getMainAccount(account, parentAccount);
    const currency = tokenCurrency ? await getCryptoAssetsStore().findTokenById(tokenCurrency) : null;
    const signerAccount = currency ? makeEmptyTokenAccount(mainAccount, currency) : account;
    const { canEditFees, liveTx, hasFeesProvided } = getWalletAPITransactionSignFlowInfos({
        walletApiTransaction: transaction,
        account: mainAccount,
    });
    if (accountFamily !== liveTx.family) {
        throw new Error(`Account and transaction must be from the same family. Account family: ${accountFamily}, Transaction family: ${liveTx.family}`);
    }
    return uiNavigation(signerAccount, parentAccount, {
        canEditFees,
        liveTx,
        hasFeesProvided,
    });
}
export function signRawTransactionLogic({ manifest, accounts, tracking }, walletAccountId, transaction, uiNavigation) {
    tracking.signRawTransactionRequested(manifest);
    if (!transaction) {
        tracking.signRawTransactionFail(manifest);
        throw new Error("Transaction required");
    }
    const accountId = getAccountIdFromWalletAccountId(walletAccountId);
    if (!accountId) {
        tracking.signRawTransactionFail(manifest);
        throw new Error(`accountId ${walletAccountId} unknown`);
    }
    const account = accounts.find(account => account.id === accountId);
    if (!account) {
        tracking.signRawTransactionFail(manifest);
        throw new Error("Account required");
    }
    const parentAccount = getParentAccount(account, accounts);
    return uiNavigation(account, parentAccount, transaction);
}
export async function broadcastTransactionLogic({ manifest, accounts, tracking }, walletAccountId, signedOperation, uiNavigation, tokenCurrency) {
    if (!signedOperation) {
        tracking.broadcastFail(manifest);
        throw new Error("Transaction required");
    }
    const accountId = getAccountIdFromWalletAccountId(walletAccountId);
    if (!accountId) {
        tracking.broadcastFail(manifest);
        throw new Error(`accountId ${walletAccountId} unknown`);
    }
    const account = accounts.find(account => account.id === accountId);
    if (!account) {
        tracking.broadcastFail(manifest);
        throw new Error("Account required");
    }
    const currency = tokenCurrency ? await getCryptoAssetsStore().findTokenById(tokenCurrency) : null;
    const parentAccount = getParentAccount(account, accounts);
    const mainAccount = getMainAccount(account, parentAccount);
    const signerAccount = currency ? makeEmptyTokenAccount(mainAccount, currency) : account;
    return uiNavigation(signerAccount, parentAccount, signedOperation);
}
export function signMessageLogic({ manifest, accounts, tracking }, walletAccountId, message, uiNavigation) {
    tracking.signMessageRequested(manifest);
    const accountId = getAccountIdFromWalletAccountId(walletAccountId);
    if (!accountId) {
        tracking.signMessageFail(manifest);
        return Promise.reject(new Error(`accountId ${walletAccountId} unknown`));
    }
    const account = accounts.find(account => account.id === accountId);
    if (account === undefined) {
        tracking.signMessageFail(manifest);
        return Promise.reject(new Error("account not found"));
    }
    let formattedMessage;
    try {
        if (isAccount(account)) {
            formattedMessage = prepareMessageToSign(account, message);
        }
        else {
            throw new Error("account provided should be the main one");
        }
    }
    catch (error) {
        tracking.signMessageFail(manifest);
        return Promise.reject(error);
    }
    return uiNavigation(account, formattedMessage);
}
export const bitcoinFamilyAccountGetAddressLogic = ({ manifest, accounts, tracking }, walletAccountId, derivationPath) => {
    tracking.bitcoinFamilyAccountAddressRequested(manifest);
    const accountId = getAccountIdFromWalletAccountId(walletAccountId);
    if (!accountId) {
        tracking.bitcoinFamilyAccountAddressFail(manifest);
        return Promise.reject(new Error(`accountId ${walletAccountId} unknown`));
    }
    const account = accounts.find(account => account.id === accountId);
    if (account === undefined) {
        tracking.bitcoinFamilyAccountAddressFail(manifest);
        return Promise.reject(new Error("account not found"));
    }
    if (!isAccount(account) || account.currency.family !== "bitcoin") {
        tracking.bitcoinFamilyAccountAddressFail(manifest);
        return Promise.reject(new Error("account requested is not a bitcoin family account"));
    }
    if (derivationPath) {
        const path = derivationPath.split("/");
        const accountNumber = Number(path[0]);
        const index = Number(path[1]);
        if (Number.isNaN(accountNumber) || Number.isNaN(index)) {
            tracking.bitcoinFamilyAccountAddressFail(manifest);
            return Promise.reject(new Error("Invalid derivationPath"));
        }
        const walletAccount = getWalletAccount(account);
        const address = walletAccount.xpub.crypto.getAddress(walletAccount.xpub.derivationMode, walletAccount.xpub.xpub, accountNumber, index);
        tracking.bitcoinFamilyAccountAddressSuccess(manifest);
        return Promise.resolve(address);
    }
    tracking.bitcoinFamilyAccountAddressSuccess(manifest);
    return Promise.resolve(account.freshAddress);
};
function getRelativePath(path) {
    const splitPath = path.split("'/");
    return splitPath[splitPath.length - 1];
}
export const bitcoinFamilyAccountGetPublicKeyLogic = async ({ manifest, accounts, tracking }, walletAccountId, derivationPath) => {
    tracking.bitcoinFamilyAccountPublicKeyRequested(manifest);
    const accountId = getAccountIdFromWalletAccountId(walletAccountId);
    if (!accountId) {
        tracking.bitcoinFamilyAccountPublicKeyFail(manifest);
        return Promise.reject(new Error(`accountId ${walletAccountId} unknown`));
    }
    const account = accounts.find(account => account.id === accountId);
    if (account === undefined) {
        tracking.bitcoinFamilyAccountPublicKeyFail(manifest);
        return Promise.reject(new Error("account not found"));
    }
    if (!isAccount(account) || account.currency.family !== "bitcoin") {
        tracking.bitcoinFamilyAccountPublicKeyFail(manifest);
        return Promise.reject(new Error("account requested is not a bitcoin family account"));
    }
    const path = derivationPath?.split("/") ?? getRelativePath(account.freshAddressPath).split("/");
    const accountNumber = Number(path[0]);
    const index = Number(path[1]);
    if (Number.isNaN(accountNumber) || Number.isNaN(index)) {
        tracking.bitcoinFamilyAccountPublicKeyFail(manifest);
        return Promise.reject(new Error("Invalid derivationPath"));
    }
    const walletAccount = getWalletAccount(account);
    const publicKey = await walletAccount.xpub.crypto.getPubkeyAt(walletAccount.xpub.xpub, accountNumber, index);
    tracking.bitcoinFamilyAccountPublicKeySuccess(manifest);
    return publicKey.toString("hex");
};
export const bitcoinFamilyAccountGetXPubLogic = ({ manifest, accounts, tracking }, walletAccountId) => {
    tracking.bitcoinFamilyAccountXpubRequested(manifest);
    const accountId = getAccountIdFromWalletAccountId(walletAccountId);
    if (!accountId) {
        tracking.bitcoinFamilyAccountXpubFail(manifest);
        return Promise.reject(new Error(`accountId ${walletAccountId} unknown`));
    }
    const account = accounts.find(account => account.id === accountId);
    if (account === undefined) {
        tracking.bitcoinFamilyAccountXpubFail(manifest);
        return Promise.reject(new Error("account not found"));
    }
    if (!isAccount(account) || account.currency.family !== "bitcoin") {
        tracking.bitcoinFamilyAccountXpubFail(manifest);
        return Promise.reject(new Error("account requested is not a bitcoin family account"));
    }
    if (!account.xpub) {
        tracking.bitcoinFamilyAccountXpubFail(manifest);
        return Promise.reject(new Error("account xpub not available"));
    }
    tracking.bitcoinFamilyAccountXpubSuccess(manifest);
    return Promise.resolve(account.xpub);
};
export function startExchangeLogic({ manifest, tracking }, exchangeType, uiNavigation) {
    tracking.startExchangeRequested(manifest);
    return uiNavigation(exchangeType);
}
export async function completeExchangeLogic({ manifest, accounts, tracking }, { provider, fromAccountId, toAccountId, transaction, binaryPayload, signature, feesStrategy, exchangeType, swapId, rate, tokenCurrency, }, uiNavigation) {
    tracking.completeExchangeRequested(manifest);
    const realFromAccountId = getAccountIdFromWalletAccountId(fromAccountId);
    if (!realFromAccountId) {
        throw new Error(`accountId ${fromAccountId} unknown`);
    }
    // Nb get a hold of the actual accounts, and parent accounts
    const fromAccount = accounts.find(a => a.id === realFromAccountId);
    let toAccount;
    if (toAccountId) {
        const realToAccountId = getAccountIdFromWalletAccountId(toAccountId);
        if (!realToAccountId) {
            throw new Error(`accountId ${toAccountId} unknown`);
        }
        toAccount = accounts.find(a => a.id === realToAccountId);
    }
    if (!fromAccount) {
        throw new Error("From account not found");
    }
    if (exchangeType === 0x00 && !toAccount) {
        // if we do a swap, a destination account must be provided
        throw new Error("To account required for swap");
    }
    const fromParentAccount = getParentAccount(fromAccount, accounts);
    const currency = tokenCurrency ? await getCryptoAssetsStore().findTokenById(tokenCurrency) : null;
    const newTokenAccount = currency ? makeEmptyTokenAccount(toAccount, currency) : undefined;
    const toParentAccount = toAccount ? getParentAccount(toAccount, accounts) : undefined;
    const exchange = {
        fromAccount,
        fromParentAccount: fromAccount !== fromParentAccount ? fromParentAccount : undefined,
        fromCurrency: getCurrencyForAccount(fromAccount),
        toAccount: newTokenAccount ? newTokenAccount : toAccount,
        toParentAccount: newTokenAccount ? toAccount : toParentAccount,
        toCurrency: toAccount ? getToCurrency(toAccount, newTokenAccount) : undefined,
    };
    const accountBridge = getAccountBridge(fromAccount, fromParentAccount);
    const mainFromAccount = getMainAccount(fromAccount, fromParentAccount);
    const mainFromAccountFamily = mainFromAccount.currency.family;
    const { liveTx } = getWalletAPITransactionSignFlowInfos({
        walletApiTransaction: transaction,
        account: fromAccount,
    });
    if (liveTx.family !== mainFromAccountFamily) {
        throw new Error(`Account and transaction must be from the same family. Account family: ${mainFromAccountFamily}, Transaction family: ${liveTx.family}`);
    }
    /**
     * 'subAccountId' is used for ETH and it's ERC-20 tokens.
     * This field is ignored for BTC
     */
    const subAccountId = exchange.fromParentAccount ? fromAccount.id : undefined;
    const bridgeTx = accountBridge.createTransaction(fromAccount);
    /**
     * We append the `recipient` to the tx created from `createTransaction`
     * to avoid having userGasLimit reset to null for ETH txs
     * cf. libs/ledger-live-common/src/families/ethereum/updateTransaction.ts
     */
    const tx = accountBridge.updateTransaction({
        ...bridgeTx,
        recipient: liveTx.recipient,
    }, {
        ...liveTx,
        feesStrategy: feesStrategy.toLowerCase(),
        subAccountId,
    });
    return uiNavigation({
        provider,
        exchange,
        transaction: tx,
        binaryPayload,
        signature,
        feesStrategy,
        exchangeType,
        swapId,
        rate,
    });
}
function getToCurrency(account, tokenAccount) {
    return tokenAccount ? getCurrencyForAccount(tokenAccount) : getCurrencyForAccount(account);
}
//# sourceMappingURL=logic.js.map