// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
import { PlayerPresence } from "@foxglove/studio-base/players/types";
import { FoxgloveWebSocketAdapter } from "./adapter.js";
import { FoxgloveConnection } from "./foxglove-connection.js";
import { WebRtcBridge } from "./webrtc/bridge.js";
import { ConfigStore } from "../utils/config-store";
export class VisualizerConnection extends FoxgloveConnection {
    #url = null;
    #config = null;
    #adapter;
    constructor({ type, data, errorCallback, debugLogs, messageHandlerVersion }) {
        super(type === "ws" ? data.connectionUrl : data.config.clientId);
        let worker;
        if (type === "ws") {
            this.#url = data.connectionUrl;
            worker = new Worker(new URL("./ws/worker", import.meta.url), { type: "module" });
        }
        else {
            this.#config = data.config;
            worker = new WebRtcBridge(data.automaticTokenRefresh);
        }
        ConfigStore.getInstance().connectionConfigValue = { debugLogs: debugLogs ?? false, messageHandlerVersion: messageHandlerVersion ?? 'v1' };
        ConfigStore.writeToLocalStorage(); // We need this to propagate the config to non-lib code
        console.info("[VisualizerConnection] - Opening connection with following configuration", ConfigStore.getInstance().connectionConfigValue);
        this.#open(worker, errorCallback);
    }
    close() {
        this.#adapter?.close();
    }
    on(name, listener) {
        this.#adapter?.on(name, listener);
    }
    off(name, listener) {
        this.#adapter?.off(name, listener);
    }
    async request(args) {
        const response = await this.#adapter?.request(args);
        return response ?? null;
    }
    subscribe(args) {
        this.#adapter?.subscribe(args);
        for (const topic of args.topics) {
            this.subscribedTopics.set(topic, topic);
        }
    }
    unsubscribe(args) {
        this.#adapter?.unsubscribe(args);
    }
    // eslint-disable-next-line no-restricted-syntax
    get connected() {
        return Boolean(this.#adapter?.readyState === WebSocket.OPEN);
    }
    #open = (worker, errorCallback) => {
        this.#adapter = new FoxgloveWebSocketAdapter({ wsUrl: this.#url, config: this.#config }, worker);
        this.#adapter.addEventListener("open", () => {
            this.playerPresence = PlayerPresence.PRESENT;
            this.emitState();
        });
        this.#adapter.addEventListener("close", () => {
            this.playerPresence = PlayerPresence.NOT_PRESENT;
            this.emitState();
        });
        this.#adapter.addEventListener("error", (_event) => {
            this.messageEvents = [];
            this.playerPresence = PlayerPresence.ERROR;
            this.emitState();
        });
        this.#adapter.addEventListener("message", (event) => {
            this.onMessagePacket.bind(this)(event, errorCallback);
        });
    };
}
//# sourceMappingURL=connection.js.map