"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVersion = void 0;
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const parseGetVersionResponse_1 = require("../../device/use-cases/parseGetVersionResponse");
function getVersion({ transport }) {
    return new rxjs_1.Observable(subscriber => {
        // TODO: defines actual value
        const oldTimeout = transport.unresponsiveTimeout;
        transport.setExchangeUnresponsiveTimeout(1000);
        const unresponsiveCallback = () => {
            // Needs to push a value and not an error to allow the command to continue once
            // the device is not unresponsive anymore. Pushing an error would stop the command.
            subscriber.next({ type: "unresponsive" });
        };
        transport.on("unresponsive", unresponsiveCallback);
        return (0, rxjs_1.from)(transport.send(...parseGetVersionResponse_1.GET_VERSION_APDU))
            .pipe((0, operators_1.switchMap)((value) => {
            transport.off("unresponsive", unresponsiveCallback);
            const firmwareInfo = (0, parseGetVersionResponse_1.parseGetVersionResponse)(value);
            return (0, rxjs_1.of)({
                type: "data",
                firmwareInfo,
            });
        }), (0, operators_1.finalize)(() => {
            // Cleans the unresponsive timeout on complete and error
            // Note: careful if this command is called sequentially several time (in a `concat`)
            // as the beginning of the next command could happen before this cleaning `finalize` is called.
            transport.setExchangeUnresponsiveTimeout(oldTimeout);
        }))
            .subscribe(subscriber);
    });
}
exports.getVersion = getVersion;
//# sourceMappingURL=getVersion.js.map