"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.accountsOpToCSV = void 0;
const bignumber_js_1 = require("bignumber.js");
const currencies_1 = require("./currencies");
const account_1 = require("./account");
const operation_1 = require("./operation");
const logic_1 = require("@ledgerhq/live-countervalues/logic");
const accountName_1 = require("@ledgerhq/live-wallet/accountName");
const store_1 = require("@ledgerhq/live-wallet/store");
const newLine = "\r\n";
const fields = [
    {
        title: "Operation Date",
        cell: (_account, _parentAccount, op) => op.date.toISOString(),
    },
    {
        title: "Status",
        cell: (_account, _parentAccount, op) => {
            return op.hasFailed ? "Failed" : "Confirmed";
        },
    },
    {
        title: "Currency Ticker",
        cell: account => (0, account_1.getAccountCurrency)(account).ticker,
    },
    {
        title: "Operation Type",
        cell: (_account, _parentAccount, op) => op.type,
    },
    {
        title: "Operation Amount",
        cell: (account, parentAccount, op) => (0, currencies_1.formatCurrencyUnit)((0, account_1.getAccountCurrency)(account).units[0], op.value, {
            disableRounding: true,
            useGrouping: false,
        }),
    },
    {
        title: "Operation Fees",
        cell: (account, parentAccount, op) => "TokenAccount" === account.type
            ? ""
            : (0, currencies_1.formatCurrencyUnit)((0, account_1.getAccountCurrency)(account).units[0], op.fee, {
                disableRounding: true,
                useGrouping: false,
            }),
    },
    {
        title: "Operation Hash",
        cell: (_account, _parentAccount, op) => op.hash,
    },
    {
        title: "Account Name",
        cell: (account, parentAccount, _op, _counterValueCurrency, _countervalueState, walletState) => {
            const main = (0, account_1.getMainAccount)(account, parentAccount);
            return walletState
                ? (0, store_1.accountNameWithDefaultSelector)(walletState, main)
                : (0, accountName_1.getDefaultAccountName)(main);
        },
    },
    {
        title: "Account xpub",
        cell: (account, parentAccount) => {
            const main = (0, account_1.getMainAccount)(account, parentAccount);
            return main.xpub || main.freshAddress;
        },
    },
    {
        title: "Countervalue Ticker",
        cell: (account, parentAccount, op, countervalueCurrency) => {
            return countervalueCurrency?.ticker ?? "";
        },
    },
    {
        title: "Countervalue at Operation Date",
        cell: (account, parentAccount, op, counterValueCurrency, countervalueState) => {
            const value = counterValueCurrency && countervalueState
                ? (0, logic_1.calculate)(countervalueState, {
                    from: (0, account_1.getAccountCurrency)(account),
                    to: counterValueCurrency,
                    value: op.value.toNumber(),
                    disableRounding: true,
                    date: op.date,
                })
                : null;
            return value && counterValueCurrency
                ? (0, currencies_1.formatCurrencyUnit)(counterValueCurrency.units[0], new bignumber_js_1.BigNumber(value), {
                    disableRounding: true,
                    useGrouping: false,
                })
                : "";
        },
    },
    {
        title: "Countervalue at CSV Export",
        cell: (account, parentAccount, op, counterValueCurrency, countervalueState) => {
            const value = counterValueCurrency && countervalueState
                ? (0, logic_1.calculate)(countervalueState, {
                    from: (0, account_1.getAccountCurrency)(account),
                    to: counterValueCurrency,
                    value: op.value.toNumber(),
                    disableRounding: true,
                })
                : null;
            return value && counterValueCurrency
                ? (0, currencies_1.formatCurrencyUnit)(counterValueCurrency.units[0], new bignumber_js_1.BigNumber(value), {
                    disableRounding: true,
                    useGrouping: false,
                })
                : "";
        },
    },
];
const accountRows = (account, parentAccount, counterValueCurrency, countervalueState, walletState) => account.operations
    .reduce((ops, op) => ops.concat((0, operation_1.flattenOperationWithInternalsAndNfts)(op)), [])
    .map(operation => fields.map(field => field.cell(account, parentAccount, operation, counterValueCurrency, countervalueState, walletState)));
const accountsRows = (accounts, counterValueCurrency, countervalueState, walletState) => {
    return (0, account_1.flattenAccounts)(accounts).reduce((all, account) => {
        const parentAccount = account.type !== "Account" ? accounts.find(a => a.id === account.parentId) : null;
        return all.concat(accountRows(account, parentAccount, counterValueCurrency, countervalueState, walletState));
    }, []);
};
const mapRowValue = (row) => {
    const rowWithoutNewlines = row.map(value => {
        if (value) {
            return value.replace(/[,\n\r]/g, "");
        }
        return value;
    });
    return rowWithoutNewlines.join(",");
};
const accountsOpToCSV = (accounts, counterValueCurrency, countervalueState, // cvs state required for countervalues export
walletState) => {
    const header = fields.map(field => field.title).join(",") + newLine;
    const accountsRowsRes = accountsRows(accounts, counterValueCurrency, countervalueState, walletState);
    const rows = accountsRowsRes.map(mapRowValue).join(newLine);
    return header + rows;
};
exports.accountsOpToCSV = accountsOpToCSV;
//# sourceMappingURL=csvExport.js.map