import { Transport } from "@open-rpc/client-js/build/transports/Transport";
import { getNotifications } from "@open-rpc/client-js/src/Request";
export default class MessagePortTransport extends Transport {
    port;
    postMessageID;
    constructor(port) {
        super();
        this.port = port;
        this.postMessageID = `post-message-transport-${Math.random()}`;
    }
    messageHandler = (ev) => {
        console.debug("LSP <<-", ev.data);
        if (!ev.data.result && ev.data.method !== 'textDocument/publishDiagnostics') {
            console.debug(ev.data);
            // this.port.postMessage({ jsonrpc: '2.0', id: ev.data.id, result: null })
        }
        else {
            this.transportRequestManager.resolveResponse(JSON.stringify(ev.data));
        }
    };
    connect() {
        return new Promise(async (resolve) => {
            this.port.addEventListener("message", this.messageHandler);
            resolve();
        });
    }
    async sendData(data) {
        console.debug("LSP ->>", data);
        const prom = this.transportRequestManager.addRequest(data, null);
        const notifications = getNotifications(data);
        if (this.port) {
            this.port.postMessage(data.request);
            this.transportRequestManager.settlePendingRequest(notifications);
        }
        return prom;
    }
    close() { }
}
