import { quitApp } from "../commands/quitApp";
import { withDevice } from "../../hw/deviceAccess";
import { from, Observable, of } from "rxjs";
import { switchMap, catchError } from "rxjs/operators";
import { sharedLogicTaskWrapper } from "./core";
import { getLatestFirmwareForDeviceUseCase } from "../../device/use-cases/getLatestFirmwareForDeviceUseCase";
function internalGetLatestFirmwareTask({ deviceId, deviceInfo, }) {
    return new Observable(subscriber => {
        return withDevice(deviceId)(transport => quitApp(transport).pipe(switchMap(() => {
            return from(getLatestFirmwareForDeviceUseCase(deviceInfo));
        }), switchMap(firmwareUpdateContext => {
            if (firmwareUpdateContext) {
                return of({
                    type: "data",
                    firmwareUpdateContext,
                });
            }
            else {
                return of({
                    type: "taskError",
                    error: "FirmwareUpToDate",
                });
            }
        }), catchError(() => {
            return of({
                type: "taskError",
                error: "FailedToRetrieveFirmwareUpdateInfo",
            });
        }))).subscribe(subscriber);
    });
}
export const getLatestFirmwareTask = sharedLogicTaskWrapper(internalGetLatestFirmwareTask);
//# sourceMappingURL=getLatestFirmware.js.map