import { Subject } from "rxjs";
import { withDevice } from "./deviceAccess";
export default function openTransportAsSubject({ deviceId, }) {
    const subject = new Subject();
    withDevice(deviceId)(transport => {
        // The input part of the bidirectional communication
        subject.subscribe({
            next: e => {
                /**
                 * Receiving an event from the ipc bridge allows us to pass a msg
                 * into an ongoing withDevice job. Allowing to exchange messages with the
                 * transport exposed from the job.
                 */
                if (e?.type === "input-frame") {
                    subject.next({ type: "device-request", data: e.apduHex });
                    // TODO important avoiding collisions, also types will be broken
                    transport
                        .exchange(Buffer.from(e.apduHex, "hex"))
                        .then(response => subject.next({
                        type: "device-response",
                        data: response.toString("hex"),
                    }))
                        .catch(error => subject.next({ type: "error", error }));
                }
            },
        });
        return subject;
    }).subscribe();
    return subject;
}
//# sourceMappingURL=openTransportAsSubject.js.map