"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchNextFirmware = void 0;
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const api_1 = __importDefault(require("../manager/api"));
const getDeviceInfo_1 = __importDefault(require("./getDeviceInfo"));
const manager_1 = require("../manager");
const fetchNextFirmware = (deviceInfo) => (0, rxjs_1.from)(api_1.default.getDeviceVersion(deviceInfo.targetId, (0, manager_1.getProviderId)(deviceInfo))).pipe((0, operators_1.mergeMap)(device => (0, rxjs_1.from)(api_1.default.getCurrentOSU({
    deviceId: device.id,
    version: deviceInfo.version,
    provider: (0, manager_1.getProviderId)(deviceInfo),
}))), (0, operators_1.mergeMap)(firmware => (0, rxjs_1.from)(api_1.default.getFinalFirmwareById(firmware.next_se_firmware_final_version))));
exports.fetchNextFirmware = fetchNextFirmware;
exports.default = (transport) => (0, rxjs_1.from)((0, getDeviceInfo_1.default)(transport)).pipe((0, operators_1.mergeMap)(deviceInfo => (0, exports.fetchNextFirmware)(deviceInfo).pipe((0, operators_1.mergeMap)(nextFirmware => (0, rxjs_1.concat)((0, rxjs_1.of)({
    type: "install",
    step: "firmware",
}), api_1.default.install(transport, "firmware", {
    targetId: deviceInfo.targetId,
    firmware: nextFirmware.firmware,
    firmwareKey: nextFirmware.firmware_key,
    perso: nextFirmware.perso,
}))))));
//# sourceMappingURL=installFinalFirmware.js.map