"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const logs_1 = require("@ledgerhq/logs");
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const errors_1 = require("@ledgerhq/errors");
const getDeviceInfo_1 = __importDefault(require("./getDeviceInfo"));
const installOsuFirmware_1 = __importDefault(require("./installOsuFirmware"));
const deviceAccess_1 = require("./deviceAccess");
const waitEnd = (0, rxjs_1.of)({
    type: "wait",
}).pipe((0, operators_1.delay)(3000));
const checkId = (deviceId, { osu }) => {
    (0, logs_1.log)("hw", "firmwareUpdate-prepare");
    return (0, deviceAccess_1.withDevice)(deviceId)(transport => (0, rxjs_1.from)((0, getDeviceInfo_1.default)(transport))).pipe((0, operators_1.mergeMap)((deviceInfo) => deviceInfo.isBootloader || deviceInfo.isOSU
        ? (0, rxjs_1.throwError)(() => new errors_1.DeviceOnDashboardExpected())
        : (0, rxjs_1.concat)((0, deviceAccess_1.withDevice)(deviceId)(transport => (0, installOsuFirmware_1.default)(transport, deviceInfo.targetId, osu)), waitEnd)), (0, operators_1.filter)(e => e.type === "bulk-progress"), (0, operators_1.map)(e => ({
        progress: e.progress,
        displayedOnDevice: e.index >= e.total - 1,
    })));
};
exports.default = checkId;
//# sourceMappingURL=firmwareUpdate-prepare.js.map