"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mockDeviceWithAPDUs = exports.releaseMockDevice = void 0;
const invariant_1 = __importDefault(require("invariant"));
const hw_transport_mocker_1 = require("@ledgerhq/hw-transport-mocker");
const hw_1 = require("../../hw");
let idCounter = 0;
const transports = {};
const recordStores = {};
function releaseMockDevice(id) {
    const store = recordStores[id];
    (0, invariant_1.default)(store, "MockDevice does not exist (%s)", id);
    try {
        store.ensureQueueEmpty();
    }
    catch (e) {
        e && console.error(e.message);
        throw e;
    }
    finally {
        delete recordStores[id];
        delete transports[id];
    }
}
exports.releaseMockDevice = releaseMockDevice;
async function mockDeviceWithAPDUs(apdus, opts) {
    const id = `mock:${++idCounter}`;
    const store = hw_transport_mocker_1.RecordStore.fromString(apdus, opts);
    recordStores[id] = store;
    transports[id] = await (0, hw_transport_mocker_1.openTransportReplayer)(store);
    return id;
}
exports.mockDeviceWithAPDUs = mockDeviceWithAPDUs;
(0, hw_1.registerTransportModule)({
    id: "mock",
    open: id => {
        if (id in transports) {
            const Tr = transports[id];
            return Tr;
        }
    },
    disconnect: () => Promise.resolve(),
});
//# sourceMappingURL=mockDevice.js.map