"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const errors_1 = require("@ledgerhq/errors");
const deviceAccess_1 = require("./deviceAccess");
const getDeviceInfo_1 = __importDefault(require("./getDeviceInfo"));
const types_live_1 = require("@ledgerhq/types-live");
const appSupportsQuitApp_1 = __importDefault(require("../appSupportsQuitApp"));
const quitApp_1 = __importDefault(require("./quitApp"));
const getAppAndVersion_1 = __importDefault(require("./getAppAndVersion"));
const isDashboardName_1 = require("./isDashboardName");
const attemptToQuitApp = (transport, appAndVersion) => appAndVersion && (0, appSupportsQuitApp_1.default)(appAndVersion)
    ? (0, rxjs_1.from)((0, quitApp_1.default)(transport)).pipe((0, operators_1.concatMap)(() => (0, rxjs_1.of)({
        type: "unresponsiveDevice",
    })), (0, operators_1.catchError)(e => (0, rxjs_1.throwError)(() => e)))
    : (0, rxjs_1.of)({
        type: "appDetected",
    });
function unistallLanguage({ deviceId, language, }) {
    const sub = (0, deviceAccess_1.withDevice)(deviceId)(transport => new rxjs_1.Observable(subscriber => {
        const timeoutSub = (0, rxjs_1.of)({
            type: "unresponsiveDevice",
        })
            .pipe((0, operators_1.delay)(1000))
            .subscribe(e => subscriber.next(e));
        const sub = (0, rxjs_1.from)((0, getDeviceInfo_1.default)(transport))
            .pipe((0, operators_1.mergeMap)(async (_) => {
            timeoutSub.unsubscribe();
            const id = types_live_1.languageIds[language];
            await transport.send(0xe0, 0x33, id, 0x00, undefined, [0x9000, 0x5501]);
            subscriber.next({
                type: "languageUninstalled",
            });
            subscriber.complete();
        }), (0, operators_1.catchError)((e) => {
            if (e instanceof errors_1.DeviceOnDashboardExpected ||
                (e &&
                    e instanceof errors_1.TransportStatusError &&
                    [0x6e00, 0x6d00, 0x6e01, 0x6d01, 0x6d02].includes(e.statusCode))) {
                const quitAppObservable = (0, rxjs_1.from)((0, getAppAndVersion_1.default)(transport)).pipe((0, operators_1.concatMap)(appAndVersion => {
                    return !(0, isDashboardName_1.isDashboardName)(appAndVersion.name)
                        ? attemptToQuitApp(transport, appAndVersion)
                        : (0, rxjs_1.of)({
                            type: "appDetected",
                        });
                }));
                quitAppObservable.subscribe(event => subscriber.next(event), error => subscriber.error(error));
            }
            subscriber.error(e);
            return rxjs_1.EMPTY;
        }))
            .subscribe();
        return () => {
            timeoutSub.unsubscribe();
            sub.unsubscribe();
        };
    }));
    return sub;
}
exports.default = unistallLanguage;
//# sourceMappingURL=uninstallLanguage.js.map