"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAppAndVersion = void 0;
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const errors_1 = require("../../errors");
function getAppAndVersion(transport) {
    return new rxjs_1.Observable(subscriber => {
        // The device will always respond from this command: either same response than an unlocked device,
        // or 0x5515 if implemented. No unresponsive strategy needed.
        return (0, rxjs_1.from)(transport.send(0xb0, 0x01, 0x00, 0x00))
            .pipe((0, operators_1.switchMap)(result => {
            let i = 0;
            const format = result[i++];
            if (format !== 1) {
                throw new errors_1.GetAppAndVersionUnsupportedFormat("getAppAndVersion: format not supported");
            }
            const nameLength = result[i++];
            const name = result.slice(i, (i += nameLength)).toString("ascii");
            const versionLength = result[i++];
            const version = result.slice(i, (i += versionLength)).toString("ascii");
            const flagLength = result[i++];
            const flags = result.slice(i, (i += flagLength));
            return (0, rxjs_1.of)({
                type: "data",
                appAndVersion: {
                    name,
                    version,
                    flags,
                },
            });
        }))
            .subscribe(subscriber);
    });
}
exports.getAppAndVersion = getAppAndVersion;
//# sourceMappingURL=getAppAndVersion.js.map