"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLatestFirmwareTask = void 0;
const quitApp_1 = require("../commands/quitApp");
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const core_1 = require("./core");
const getLatestFirmwareForDeviceUseCase_1 = require("../../device/use-cases/getLatestFirmwareForDeviceUseCase");
const core_2 = require("../transports/core");
function internalGetLatestFirmwareTask({ deviceId, deviceName, deviceInfo, }) {
    return new rxjs_1.Observable(subscriber => {
        return (0, core_2.withTransport)(deviceId, deviceName ? { matchDeviceByName: deviceName } : undefined)(({ transportRef }) => (0, quitApp_1.quitApp)(transportRef.current).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