import { quitApp } from "../commands/quitApp";
import { from, Observable, of } from "rxjs";
import { switchMap, catchError } from "rxjs/operators";
import { sharedLogicTaskWrapper } from "./core";
import { getLatestFirmwareForDeviceUseCase } from "../../device/use-cases/getLatestFirmwareForDeviceUseCase";
import { withTransport } from "../transports/core";
function internalGetLatestFirmwareTask({ deviceId, deviceName, deviceInfo, }) {
    return new Observable(subscriber => {
        return withTransport(deviceId, deviceName ? { matchDeviceByName: deviceName } : undefined)(({ transportRef }) => quitApp(transportRef.current).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