"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatReportForConsole = exports.formatAppCandidate = exports.formatTime = exports.formatError = void 0;
const groupBy_1 = __importDefault(require("lodash/groupBy"));
const formatters_1 = require("@ledgerhq/coin-framework/bot/formatters");
Object.defineProperty(exports, "formatError", { enumerable: true, get: function () { return formatters_1.formatError; } });
const account_1 = require("../account");
const transaction_1 = require("../transaction");
const currencies_1 = require("../currencies");
const formatTimeMinSec = (t) => {
    const totalsecs = Math.round(t / 1000);
    const min = Math.floor(totalsecs / 60);
    const sec = totalsecs - min * 60;
    if (!sec)
        return `${min}min`;
    return `${min}min ${sec}s`;
};
const formatTime = (t) => !t
    ? "N/A"
    : t > 3000
        ? t > 100000
            ? formatTimeMinSec(t)
            : `${Math.round(t / 100) / 10}s`
        : `${t < 5 ? t.toFixed(2) : t.toFixed(0)}ms`;
exports.formatTime = formatTime;
const formatDt = (from, to) => (from && to ? (0, exports.formatTime)(to - from) : "?");
function formatAppCandidate(appCandidate) {
    return `${appCandidate.appName} ${appCandidate.appVersion} on ${appCandidate.model} ${appCandidate.firmware}`;
}
exports.formatAppCandidate = formatAppCandidate;
function formatReportForConsole({ resyncAccountsDuration, appCandidate, account, maxSpendable, unavailableMutationReasons, mutation, mutationTime, destination, transaction, statusTime, status, recoveredFromTransactionStatus, signedOperation, signedTime, optimisticOperation, broadcastedTime, operation, confirmedTime, finalAccount, finalDestination, finalDestinationOperation, testDestinationDuration, testDuration, error, errorTime, }) {
    let str = "";
    str += `necessary accounts resynced in ${(0, exports.formatTime)(resyncAccountsDuration)}\n`;
    str += `▬ ${formatAppCandidate(appCandidate)}\n`;
    if (account) {
        str += `→ FROM ${(0, account_1.formatAccount)(account, "basic")}\n`;
    }
    if (account && maxSpendable) {
        str += `max spendable ~${(0, currencies_1.formatCurrencyUnit)(account.currency.units[0], maxSpendable)}\n`;
    }
    if (unavailableMutationReasons) {
        let detail = "?";
        if (account && !account.used) {
            detail = "account is empty";
        }
        else {
            const byErrorMessage = (0, groupBy_1.default)(unavailableMutationReasons, r => r.error.message);
            const keys = Object.keys(byErrorMessage);
            if (keys.length === 1) {
                detail = keys[0];
            }
            else {
                detail = unavailableMutationReasons
                    .map(({ mutation, error }) => mutation.name + ": " + error.message)
                    .join(", ");
            }
        }
        str += `🤷‍♂️ couldn't find a mutation to do! (${detail})\n`;
    }
    if (mutation) {
        str += `★ using mutation '${mutation.name}'\n`;
    }
    if (destination) {
        str += `→ TO ${(0, account_1.formatAccount)(destination, "head")}\n`;
    }
    if (transaction && account) {
        str += `✔️ transaction ${(0, transaction_1.formatTransaction)(transaction, account)}\n`;
    }
    if (status && transaction && account) {
        str += `STATUS (${formatDt(mutationTime, statusTime)})${(0, transaction_1.formatTransactionStatus)(transaction, status, account)}\n`;
    }
    if (recoveredFromTransactionStatus && account) {
        str += `\n⚠️ recovered from transaction ${(0, transaction_1.formatTransaction)(recoveredFromTransactionStatus.transaction, account)}\nof status ${(0, transaction_1.formatTransactionStatus)(recoveredFromTransactionStatus.transaction, recoveredFromTransactionStatus.status, account)}\n\n`.replace(/\n/g, "\n  ");
    }
    if (signedOperation) {
        str += `✔️ has been signed! (${formatDt(statusTime, signedTime)}) ${!optimisticOperation ? JSON.stringify((0, transaction_1.toSignedOperationRaw)(signedOperation)) : ""}\n`;
    }
    if (optimisticOperation) {
        str += `✔️ broadcasted! (${formatDt(signedTime, broadcastedTime)}) optimistic operation: ${(0, account_1.formatOperation)(account)(optimisticOperation)}\n`;
    }
    if (operation) {
        str += `✔️ operation confirmed (${formatDt(broadcastedTime, confirmedTime)}): ${(0, account_1.formatOperation)(finalAccount || account)(operation)}\n`;
    }
    if (finalAccount) {
        str += `✔️ ${(0, account_1.formatAccount)(finalAccount, "basic")}`;
    }
    if (testDuration) {
        str += `(in ${(0, exports.formatTime)(testDuration)})\n`;
    }
    if (finalDestination && finalDestinationOperation) {
        str += `✔️ destination operation ${(0, account_1.formatOperation)(finalDestination)(finalDestinationOperation)}\n`;
    }
    if (testDestinationDuration) {
        str += `(in ${(0, exports.formatTime)(testDestinationDuration)})\n`;
    }
    if (error) {
        str += `⚠️ ${(0, formatters_1.formatError)(error, true)}\n`;
        if (mutationTime && errorTime) {
            str += `(totally spent ${(0, exports.formatTime)(errorTime - mutationTime)} – ends at ${new Date().toISOString()})`;
        }
    }
    return str;
}
exports.formatReportForConsole = formatReportForConsole;
//# sourceMappingURL=formatters.js.map