"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrepareConnectManagerEventMapper = void 0;
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const device_management_kit_1 = require("@ledgerhq/device-management-kit");
const errors_1 = require("@ledgerhq/errors");
class PrepareConnectManagerEventMapper {
    events;
    eventSubject = new rxjs_1.Subject();
    constructor(events) {
        this.events = events;
    }
    map() {
        const cancelAction = this.events.cancel;
        const unsubscribe = new rxjs_1.Subject();
        // Subscribe to device action events
        this.events.observable
            .pipe((0, operators_1.tap)(event => this.handleEvent(event)), (0, rxjs_1.takeUntil)(unsubscribe), (0, operators_1.catchError)(error => this.handleError(error)))
            .subscribe();
        return new rxjs_1.Observable(observer => {
            const sub = this.eventSubject.subscribe(observer);
            return () => {
                sub.unsubscribe();
                cancelAction();
                unsubscribe.next();
            };
        });
    }
    handleEvent(event) {
        switch (event.status) {
            case device_management_kit_1.DeviceActionStatus.Pending:
                this.handlePendingEvent(event.intermediateValue);
                break;
            case device_management_kit_1.DeviceActionStatus.Completed:
                this.handleCompletedEvent(event.output);
                break;
            case device_management_kit_1.DeviceActionStatus.Error:
                this.handleErrorEvent(event.error);
                break;
            case device_management_kit_1.DeviceActionStatus.NotStarted:
            case device_management_kit_1.DeviceActionStatus.Stopped:
                this.eventSubject.error(new Error("Unexpected device action status"));
                break;
        }
    }
    handlePendingEvent(intermediateValue) {
        switch (intermediateValue.requiredUserInteraction) {
            case device_management_kit_1.UserInteractionRequired.UnlockDevice:
                this.eventSubject.next({ type: "lockedDevice" });
                break;
            case device_management_kit_1.UserInteractionRequired.None:
                // Nothing to do
                break;
        }
    }
    handleCompletedEvent(_output) {
        this.eventSubject.complete();
    }
    handleErrorEvent(error) {
        if (error instanceof device_management_kit_1.DeviceLockedError) {
            this.eventSubject.next({ type: "lockedDevice" });
            this.eventSubject.complete();
        }
        else if ("_tag" in error && error._tag === "WebHidSendReportError") {
            this.eventSubject.error(new errors_1.DisconnectedDevice());
        }
        else {
            this.eventSubject.error(error);
        }
    }
    handleError(error) {
        this.eventSubject.error(error);
        return (0, rxjs_1.of)();
    }
}
exports.PrepareConnectManagerEventMapper = PrepareConnectManagerEventMapper;
//# sourceMappingURL=connectManagerEventMapper.js.map