"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable no-bitwise */
const errors_1 = require("@ledgerhq/errors");
const logs_1 = require("@ledgerhq/logs");
const getVersionUseCase_1 = require("../device/use-cases/getVersionUseCase");
const isDevFirmware_1 = __importDefault(require("./isDevFirmware"));
const getAppAndVersion_1 = __importDefault(require("./getAppAndVersion"));
const provider_1 = require("../manager/provider");
const isDashboardName_1 = require("./isDashboardName");
const errors_2 = require("../errors");
const ManagerAllowedFlag = 0x08;
const PinValidatedFlag = 0x80;
async function default_1(transport) {
    const tracer = new logs_1.LocalTracer("hw", {
        ...transport.getTraceContext(),
        function: "getDeviceInfo",
    });
    tracer.trace("Starting get device info");
    const probablyOnDashboard = await (0, getAppAndVersion_1.default)(transport)
        .then(({ name }) => (0, isDashboardName_1.isDashboardName)(name))
        .catch(e => {
        tracer.trace(`Error from getAppAndVersion: ${e}`, { error: e });
        if (e instanceof errors_1.TransportStatusError) {
            if (e.statusCode === 0x6e00) {
                return true;
            }
            if (e.statusCode === 0x6d00) {
                return false;
            }
        }
        throw e;
    });
    if (!probablyOnDashboard) {
        tracer.trace(`Device not on dashboard`);
        throw new errors_1.DeviceOnDashboardExpected();
    }
    const res = await (0, getVersionUseCase_1.getVersion)(transport).catch(e => {
        tracer.trace(`Error from getVersion: ${e}`, { error: e });
        if (e instanceof errors_1.TransportStatusError) {
            if (e.statusCode === 0x6d06 || e.statusCode === 0x6d07) {
                throw new errors_2.DeviceNotOnboarded();
            }
        }
        throw e;
    });
    const { isBootloader, rawVersion, targetId, seVersion, seTargetId, mcuBlVersion, mcuVersion, mcuTargetId, bootloaderVersion, hardwareVersion, languageId, charonState, flags, } = res;
    const isOSU = rawVersion.includes("-osu");
    const version = rawVersion.replace("-osu", "");
    const m = rawVersion.match(/([0-9]+.[0-9]+(.[0-9]+){0,1})?(-(.*))?/);
    const [, majMin, , , postDash] = m || [];
    const providerName = provider_1.PROVIDERS[postDash] ? postDash : null;
    const flag = flags.length > 0 ? flags[0] : 0;
    const managerAllowed = !!(flag & ManagerAllowedFlag);
    const pinValidated = !!(flag & PinValidatedFlag);
    let isRecoveryMode = false;
    let onboarded = true;
    if (flags.length === 4) {
        // Nb Since LNS+ unseeded devices are visible + extra flags
        isRecoveryMode = !!(flags[0] & 0x01);
        onboarded = !!(flags[0] & 0x04);
    }
    (0, logs_1.log)("hw", "deviceInfo: se@" +
        version +
        " mcu@" +
        mcuVersion +
        (isOSU ? " (osu)" : isBootloader ? " (bootloader)" : ""));
    const hasDevFirmware = (0, isDevFirmware_1.default)(seVersion);
    return {
        version,
        mcuVersion,
        seVersion,
        mcuBlVersion,
        majMin,
        providerName: providerName || null,
        targetId,
        hasDevFirmware,
        seTargetId,
        mcuTargetId,
        isOSU,
        isBootloader,
        isRecoveryMode,
        managerAllowed,
        pinValidated,
        onboarded,
        bootloaderVersion,
        hardwareVersion,
        languageId,
        charonState,
        seFlags: flags,
    };
}
exports.default = default_1;
//# sourceMappingURL=getDeviceInfo.js.map