"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateFirmwareAction = exports.initialState = void 0;
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const updateFirmware_1 = require("../tasks/updateFirmware");
const getDeviceInfo_1 = require("../tasks/getDeviceInfo");
const core_1 = require("./core");
const getLatestFirmware_1 = require("../tasks/getLatestFirmware");
exports.initialState = {
    step: "preparingUpdate",
    progress: 0,
    ...core_1.initialSharedActionState,
};
/**
 * Device Action used to update the firmware installed on a Ledger device
 * @param deviceId the transport id of the device to be updated
 * @returns an Observables that emits the evolution of the state machine of the firmware installation
 */
function updateFirmwareAction({ deviceId, deviceName, }) {
    return (0, rxjs_1.concat)((0, rxjs_1.of)(exports.initialState), (0, getDeviceInfo_1.getDeviceInfoTask)({ deviceId, deviceName }).pipe((0, operators_1.switchMap)(event => {
        if (event.type !== "data") {
            return (0, rxjs_1.of)(event);
        }
        const { deviceInfo } = event;
        return (0, getLatestFirmware_1.getLatestFirmwareTask)({ deviceId, deviceName, deviceInfo });
    }), (0, operators_1.switchMap)(event => {
        if (event.type !== "data") {
            return (0, rxjs_1.of)(event);
        }
        else {
            return (0, updateFirmware_1.updateFirmwareTask)({
                deviceId,
                deviceName,
                updateContext: event.firmwareUpdateContext,
            });
        }
    }), (0, operators_1.scan)((currentState, event) => {
        switch (event.type) {
            case "taskError":
                return {
                    ...currentState,
                    error: {
                        type: "UpdateFirmwareError",
                        name: event.error,
                    },
                };
            case "installingOsu":
            case "flashingMcu":
            case "flashingBootloader":
                return {
                    ...exports.initialState,
                    step: event.type,
                    progress: event.progress,
                };
            case "allowSecureChannelRequested":
            case "allowSecureChannelDenied":
            case "installOsuDevicePermissionRequested":
            case "installOsuDevicePermissionGranted":
            case "installOsuDevicePermissionDenied":
                return { ...exports.initialState, step: event.type };
            case "firmwareUpdateCompleted":
                return {
                    ...exports.initialState,
                    step: event.type,
                    updatedDeviceInfo: event.updatedDeviceInfo,
                };
            default:
                return {
                    ...currentState,
                    ...(0, core_1.sharedReducer)({
                        event,
                    }),
                };
        }
    }, exports.initialState)));
}
exports.updateFirmwareAction = updateFirmwareAction;
//# sourceMappingURL=updateFirmware.js.map