"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeAppV2Mock = exports.mockExecWithInstalledContext = exports.mockListAppsResult = exports.parseInstalled = exports.deviceInfo222 = exports.deviceInfo210lo5 = exports.deviceInfo155 = void 0;
const rxjs_1 = require("rxjs");
const errors_1 = require("@ledgerhq/errors");
const polyfill_1 = require("./polyfill");
const currencies_1 = require("../currencies");
const mock_1 = require("@ledgerhq/live-countervalues/mock");
const devices_1 = require("@ledgerhq/devices");
const types_live_1 = require("@ledgerhq/types-live");
exports.deviceInfo155 = {
    version: "1.5.5",
    isBootloader: false,
    isOSU: false,
    managerAllowed: false,
    mcuVersion: "1.7",
    pinValidated: false,
    providerName: null,
    majMin: "1.5",
    targetId: 823132164,
    seFlags: Buffer.alloc(0),
};
exports.deviceInfo210lo5 = {
    bootloaderVersion: "1.16",
    hardwareVersion: 0,
    isBootloader: false,
    isOSU: false,
    isRecoveryMode: false,
    languageId: 0,
    majMin: "2.1",
    managerAllowed: false,
    mcuVersion: "2.30",
    onboarded: true,
    pinValidated: true,
    providerName: null,
    seTargetId: 855638020,
    seVersion: "2.1.0-lo5",
    targetId: 855638020,
    version: "2.1.0-lo5",
    seFlags: Buffer.alloc(0),
};
exports.deviceInfo222 = {
    version: "2.2.2",
    mcuVersion: "2.30",
    seVersion: "2.1.0",
    mcuBlVersion: undefined,
    majMin: "2.1",
    providerName: null,
    targetId: 855638020,
    hasDevFirmware: false,
    seTargetId: 855638020,
    mcuTargetId: undefined,
    isOSU: false,
    isBootloader: false,
    isRecoveryMode: false,
    managerAllowed: true,
    pinValidated: true,
    onboarded: true,
    bootloaderVersion: "1.16",
    hardwareVersion: 0,
    languageId: 0,
    seFlags: Buffer.alloc(0),
};
const firmware155 = {
    id: 24,
    name: "1.5.5",
    version: "1.5.5",
    se_firmware: 2,
    description: "",
    display_name: "",
    notes: "",
    perso: "perso_11",
    firmware: "nanos/1.5.5/fw_1.4.2/upgrade_1.5.5",
    firmware_key: "nanos/1.5.5/fw_1.4.2/upgrade_1.5.5_key",
    hash: "",
    // @ts-expect-error mock extra unecessary data maybe
    distribution_ratio: null,
    exclude_by_default: false,
    osu_versions: [],
    date_creation: "2019-01-08T13:29:35.839258Z",
    date_last_modified: "2019-10-18T16:38:29.745993Z",
    device_versions: [10],
    mcu_versions: [6],
    application_versions: [],
    providers: [1, 4, 7, 9, 11, 12, 13],
    bytes: 20 * 4 * 1024,
};
const parseInstalled = (installedDesc) => installedDesc
    .split(",")
    .filter(Boolean)
    .map(a => {
    const trimmed = a.trim();
    const m = /(.*)\(outdated\)/.exec(trimmed);
    if (m) {
        const name = m[1].trim();
        return {
            name,
            updated: false,
            hash: "hash_" + name,
            blocks: 1,
            version: "0.0.0",
            availableVersion: "1.5.0",
        };
    }
    // Check if the name contains the number of blocks too
    const b = /(.*)_(\d*)blocks/.exec(trimmed);
    const _name = b ? b[1] : trimmed;
    return {
        name: _name,
        updated: true,
        hash: "hash_" + _name,
        blocks: Number(b ? b[2] : 1),
        version: "1.5.0",
        availableVersion: "1.6.0",
    };
});
exports.parseInstalled = parseInstalled;
function mockListAppsResult(appDesc, installedDesc, deviceInfo, deviceModelId, deviceName) {
    const tickersByMarketCap = Object.keys((0, mock_1.getBTCValues)());
    const apps = appDesc
        .split(",")
        .map(a => a.trim())
        .filter(Boolean)
        .map((name, i) => {
        const dependencies = polyfill_1.whitelistDependencies.includes(name) ? [] : (0, polyfill_1.getDependencies)(name);
        const currency = 
        // try to find the "official" currency when possible (2 currencies can have the same manager app and ticker)
        (0, currencies_1.findCryptoCurrency)(c => c.name === name) ||
            // Else take the first one with that manager app
            (0, currencies_1.findCryptoCurrency)(c => c.managerAppName === name);
        const indexOfMarketCap = currency ? tickersByMarketCap.indexOf(currency.ticker) : -1;
        return {
            id: i,
            app: i,
            name,
            displayName: name,
            version: "0.0.0",
            description: null,
            icon: "bitcoin",
            // we use bitcoin icon for all for convenience
            perso: "",
            authorName: "",
            supportURL: "",
            contactURL: "",
            sourceURL: "",
            hash: "hash_" + name,
            firmware: "firmware_" + name,
            bytes: (dependencies.length === 0 ? 10 : 1) * 4 * 1024,
            dependencies,
            warning: "",
            firmware_key: "",
            delete: "",
            delete_key: "",
            dateModified: "",
            currencyId: currency ? currency.id : null,
            indexOfMarketCap,
            isDevTools: false,
        };
    });
    const appByName = {};
    apps.forEach(app => {
        appByName[app.name] = app;
    });
    const installed = (0, exports.parseInstalled)(installedDesc);
    return {
        deviceName: deviceName || "Mock device name",
        appByName,
        appsListNames: apps.map(a => a.name),
        deviceInfo,
        deviceModelId: deviceModelId ||
            (deviceInfo.seTargetId
                ? (0, devices_1.identifyTargetId)(deviceInfo.seTargetId)?.id ?? devices_1.DeviceModelId.nanoS
                : devices_1.DeviceModelId.nanoS),
        firmware: firmware155,
        installed,
        installedAvailable: true,
        customImageBlocks: 0,
        installedLanguagePack: undefined,
    };
}
exports.mockListAppsResult = mockListAppsResult;
const mockExecWithInstalledContext = (installedInitial) => {
    let installed = installedInitial.slice(0);
    return ({ appOp, app }) => {
        if (appOp.name !== app.name) {
            throw new Error("appOp.name must match app.name");
        }
        if ((0, polyfill_1.getDependents)(app.name).some(dep => installed.some(i => i.name === dep))) {
            return (0, rxjs_1.throwError)(() => new errors_1.ManagerAppDepUninstallRequired(""));
        }
        if (appOp.type === "install") {
            const deps = (0, polyfill_1.getDependencies)(app.name);
            deps.forEach(dep => {
                const depInstalled = installed.find(i => i.name === dep);
                if (!depInstalled || !depInstalled.updated) {
                    return (0, rxjs_1.throwError)(() => new errors_1.ManagerAppDepInstallRequired(""));
                }
            });
        }
        switch (appOp.type) {
            case "install":
                if (!installed.some(i => i.name === appOp.name)) {
                    installed = installed.concat({
                        name: appOp.name,
                        updated: true,
                        blocks: 0,
                        hash: "",
                        version: "1.0.0",
                        availableVersion: "1.0.0",
                    });
                }
                break;
            case "uninstall":
                installed = installed.filter(a => a.name !== appOp.name);
                break;
        }
        return (0, rxjs_1.of)({
            progress: 0,
        }, {
            progress: 0.5,
        }, {
            progress: 1,
        });
    };
};
exports.mockExecWithInstalledContext = mockExecWithInstalledContext;
function makeAppV2Mock(props) {
    return {
        versionId: 1,
        versionName: "Bitcoin",
        versionDisplayName: "Bitcoin",
        version: "1.0.0",
        currencyId: "bitcoin",
        description: "Bitcoin app",
        applicationType: types_live_1.AppType.currency,
        dateModified: "2021-01-01",
        icon: "icon",
        authorName: "Ledger",
        supportURL: "https://support.ledger.com",
        contactURL: "https://contact.ledger.com",
        sourceURL: "https://source.ledger.com",
        hash: "hash",
        perso: "perso",
        parentName: null,
        firmware: "firmware",
        firmwareKey: "firmwareKey",
        delete: "delete",
        deleteKey: "deleteKey",
        bytes: 100,
        warning: null,
        isDevTools: false,
        ...props,
    };
}
exports.makeAppV2Mock = makeAppV2Mock;
//# sourceMappingURL=mock.js.map