"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDataModel = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const constants_1 = require("@ledgerhq/coin-aptos/constants");
const config_1 = require("./config");
const currencies_1 = require("./currencies");
/**
 * @memberof DataModel
 */
function createDataModel(schema) {
    const { migrations, encode, decode } = schema;
    const version = migrations.length;
    async function decodeModel(raw) {
        let { data } = raw;
        const { currencyId, freshAddressPath } = data;
        const currency = (0, currencies_1.findCryptoCurrencyById)(currencyId);
        // Set 'change' and 'address_index' levels to be hardened for Aptos derivation path
        if (currencyId === "aptos" &&
            freshAddressPath.match(constants_1.APTOS_NON_HARDENED_DERIVATION_PATH_REGEX)) {
            data.freshAddressPath = freshAddressPath
                .split("/")
                .map(value => (value.endsWith("'") ? value : value + "'"))
                .join("/");
        }
        if (currencyId == "crypto_org" && !data.cosmosResources) {
            data.cosmosResources = {
                delegations: [],
                redelegations: [],
                unbondings: [],
                delegatedBalance: new bignumber_js_1.default(0),
                pendingRewardsBalance: new bignumber_js_1.default(0),
                unbondingBalance: new bignumber_js_1.default(0),
                withdrawAddress: data.freshAddress,
                sequence: 0,
            };
        }
        if (currency && currency.family == "evm" && !(0, config_1.getCurrencyConfiguration)(currency).showNfts) {
            if (Array.isArray(data.operations)) {
                data.operations = data.operations.filter(tx => !("nftOperations" in tx));
            }
        }
        for (let i = raw.version; i < version; i++) {
            data = migrations[i](data);
        }
        data = await decode(data);
        return data;
    }
    function encodeModel(model) {
        const data = encode(model);
        return {
            data,
            version,
        };
    }
    return Object.freeze({
        version,
        decode: decodeModel,
        encode: encodeModel,
    });
}
exports.createDataModel = createDataModel;
//# sourceMappingURL=DataModel.js.map