"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toggleOnboardingEarlyCheckTask = void 0;
const rxjs_1 = require("rxjs");
const logs_1 = require("@ledgerhq/logs");
const core_1 = require("./core");
const core_2 = require("../transports/core");
const toggleOnboardingEarlyCheck_1 = require("../commands/toggleOnboardingEarlyCheck");
const errors_1 = require("@ledgerhq/errors");
function internalToggleOnboardingEarlyCheckTask({ deviceId, deviceName, toggleType, }) {
    const tracer = new logs_1.LocalTracer(core_1.LOG_TYPE, {
        function: "toggleOnboardingEarlyCheckTask",
        toggleType,
    });
    tracer.trace("Starting toggleOnboardingEarlyCheckTask");
    const p2 = toggleType === "enter" ? toggleOnboardingEarlyCheck_1.ToggleTypeP2.EnterChecking : toggleOnboardingEarlyCheck_1.ToggleTypeP2.ExitChecking;
    return new rxjs_1.Observable(subscriber => {
        (0, core_2.withTransport)(deviceId, deviceName ? { matchDeviceByName: deviceName } : undefined)(({ transportRef }) => (0, toggleOnboardingEarlyCheck_1.toggleOnboardingEarlyCheckCmd)({
            transport: transportRef.current,
            p2,
        })).subscribe({
            next: _ => subscriber.next({ type: "success" }),
            error: (error) => {
                if (error instanceof errors_1.TransportStatusError) {
                    tracer.trace("A TransportStatusError error occurred", { error });
                    if (error.statusCode === errors_1.StatusCodes.SECURITY_STATUS_NOT_SATISFIED) {
                        subscriber.next({
                            type: "taskError",
                            error: "DeviceInInvalidState",
                        });
                        return;
                    }
                    else if (error.statusCode === errors_1.StatusCodes.INCORRECT_LENGTH) {
                        subscriber.next({
                            type: "taskError",
                            error: "InternalError",
                        });
                        return;
                    }
                }
                tracer.trace("An unknown error occurred", { error });
                subscriber.next({ type: "taskError", error: "Unknown" });
            },
            complete: () => subscriber.complete(),
        });
    });
}
/**
 * During the onboarding, makes the device enter or exit the early security check steps
 *
 * This task only puts (or moves out) the device to the state/step of the early security check.
 * It does not starts any "security checks".
 *
 * If the device is not in the WELCOME or WELCOME_STEP2 onboarding state, this task will emit
 * a "DeviceInInvalidState" event.
 *
 * @param deviceId The id of the targeted device
 * @param toggleType either "enter" or "exit"
 * @returns An observable that emits a success, error or shared-task event
 */
exports.toggleOnboardingEarlyCheckTask = (0, core_1.sharedLogicTaskWrapper)(internalToggleOnboardingEarlyCheckTask);
//# sourceMappingURL=toggleOnboardingEarlyCheck.js.map