"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLatestFirmwareTask = void 0;
const quitApp_1 = require("../commands/quitApp");
const deviceAccess_1 = require("../../hw/deviceAccess");
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const core_1 = require("./core");
const getLatestFirmwareForDeviceUseCase_1 = require("../../device/use-cases/getLatestFirmwareForDeviceUseCase");
function internalGetLatestFirmwareTask({ deviceId, deviceInfo, }) {
    return new rxjs_1.Observable(subscriber => {
        return (0, deviceAccess_1.withDevice)(deviceId)(transport => (0, quitApp_1.quitApp)(transport).pipe((0, operators_1.switchMap)(() => {
            return (0, rxjs_1.from)((0, getLatestFirmwareForDeviceUseCase_1.getLatestFirmwareForDeviceUseCase)(deviceInfo));
        }), (0, operators_1.switchMap)(firmwareUpdateContext => {
            if (firmwareUpdateContext) {
                return (0, rxjs_1.of)({
                    type: "data",
                    firmwareUpdateContext,
                });
            }
            else {
                return (0, rxjs_1.of)({
                    type: "taskError",
                    error: "FirmwareUpToDate",
                });
            }
        }), (0, operators_1.catchError)(() => {
            return (0, rxjs_1.of)({
                type: "taskError",
                error: "FailedToRetrieveFirmwareUpdateInfo",
            });
        }))).subscribe(subscriber);
    });
}
exports.getLatestFirmwareTask = (0, core_1.sharedLogicTaskWrapper)(internalGetLatestFirmwareTask);
//# sourceMappingURL=getLatestFirmware.js.map