"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toAccountRaw = exports.fromAccountRaw = exports.fromOperationRaw = exports.toOperationRaw = exports.toBalanceHistoryRaw = void 0;
const memoize_1 = __importDefault(require("lodash/memoize"));
const currencies_1 = require("../currencies");
const index_1 = require("@ledgerhq/coin-framework/account/index");
const index_2 = require("@ledgerhq/coin-framework/serialization/index");
const bridge_1 = require("../bridge");
const impl_1 = require("../bridge/impl");
function toBalanceHistoryRaw(b) {
    return b.map(({ date, value }) => [date.toISOString(), value.toString()]);
}
exports.toBalanceHistoryRaw = toBalanceHistoryRaw;
const toOperationRaw = (operation, preserveSubOperation) => {
    let toOperationRaw;
    if (operation.extra) {
        const family = inferFamilyFromAccountId(operation.accountId);
        if (family) {
            const bridge = (0, impl_1.getAccountBridgeByFamily)(family, operation.accountId);
            toOperationRaw = bridge.toOperationExtraRaw;
        }
    }
    return (0, index_2.toOperationRaw)(operation, preserveSubOperation, toOperationRaw);
};
exports.toOperationRaw = toOperationRaw;
const fromOperationRaw = (operationRaw, accountId, subAccounts) => {
    let fromOperationRaw;
    if (operationRaw.extra) {
        const family = inferFamilyFromAccountId(operationRaw.accountId);
        if (family) {
            const bridge = (0, impl_1.getAccountBridgeByFamily)(family, accountId);
            fromOperationRaw = bridge.fromOperationExtraRaw;
        }
    }
    return (0, index_2.fromOperationRaw)(operationRaw, accountId, subAccounts, fromOperationRaw);
};
exports.fromOperationRaw = fromOperationRaw;
async function fromAccountRaw(rawAccount) {
    const currency = (0, currencies_1.getCryptoCurrencyById)(rawAccount.currencyId);
    const bridge = (0, impl_1.getAccountBridgeByFamily)(currency.family, rawAccount.id);
    return await (0, index_2.fromAccountRaw)(rawAccount, {
        assignFromAccountRaw: bridge.assignFromAccountRaw,
        assignFromTokenAccountRaw: bridge.assignFromTokenAccountRaw,
        fromOperationExtraRaw: bridge.fromOperationExtraRaw,
    });
}
exports.fromAccountRaw = fromAccountRaw;
function toAccountRaw(account, userData) {
    const bridge = (0, bridge_1.getAccountBridge)(account);
    const commonAccountRaw = (0, index_2.toAccountRaw)(account, {
        assignToAccountRaw: bridge.assignToAccountRaw,
        assignToTokenAccountRaw: bridge.assignToTokenAccountRaw,
        toOperationExtraRaw: bridge.toOperationExtraRaw,
    });
    // extend with user data fields
    if (userData) {
        commonAccountRaw.name = userData.name;
        commonAccountRaw.starred = userData.starredIds.includes(commonAccountRaw.id);
        for (const tokenAccount of commonAccountRaw.subAccounts || []) {
            tokenAccount.starred = userData.starredIds.includes(tokenAccount.id);
        }
    }
    return commonAccountRaw;
}
exports.toAccountRaw = toAccountRaw;
const inferFamilyFromAccountId = (0, memoize_1.default)(accountId => {
    try {
        const { currencyId } = (0, index_1.decodeAccountId)(accountId);
        return (0, currencies_1.getCryptoCurrencyById)(currencyId).family;
    }
    catch {
        return null;
    }
});
//# sourceMappingURL=serialization.js.map