// 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 { MINUTE } from "@luxonis/common-fe-utils";
import { uuidv7 } from "uuidv7";
import { Logger } from "../../utils/logger";
const logger = Logger.getLogger();
const GET_APP_CONNECTION = () => ({ GetAppConnection: {} });
const GET_CONNECTION_REFRESH_INTERVAL = 5 * MINUTE;
export class WebRtcCommsProxy {
    automaticTokenRefresh;
    #commsChannel;
    #refreshIntervalId = null;
    constructor(commsChannel, automaticTokenRefresh) {
        this.automaticTokenRefresh = automaticTokenRefresh;
        this.#commsChannel = commsChannel;
        this.#commsChannel.on("message:json", (event) => {
            this.#handleCommsMessages(event);
        });
        if (automaticTokenRefresh) {
            this.#startRefreshInterval();
        }
    }
    #sendMessageToCommsChannel(message) {
        switch (message) {
            case "getConnection": {
                const typedMessage = GET_APP_CONNECTION();
                const webRtcMessage = JSON.stringify({
                    cid: uuidv7(),
                    data: typedMessage,
                });
                if (webRtcMessage) {
                    this.#commsChannel.sendMessage(webRtcMessage);
                }
                else {
                    logger.error("Failed to serialize GetAppConnectionMessage");
                }
            }
        }
    }
    handleClose() {
        if (this.#refreshIntervalId) {
            clearInterval(this.#refreshIntervalId);
        }
    }
    #handleCommsMessages(message) {
        logger.debug("Incoming message on comms channel - ", message);
        if (this.#isAppConnectionErrorResponse(message)) {
            logger.warn("Received error response for GetAppConnection request - ", message.data.Error);
            return;
        }
        if (this.#isAppConnectionOkResponse(message)) {
            if (this.automaticTokenRefresh) {
                this.automaticTokenRefresh(message.data.Ok.GetAppConnection.token);
            }
            else {
                logger.warn("Automatic token refresh is not enabled but received app connection response.");
            }
            return;
        }
        logger.error("Comms Proxy received unsupported message - ", message);
    }
    #isAppConnectionOkResponse(message) {
        return !!message.data && "Ok" in message.data;
    }
    #isAppConnectionErrorResponse(message) {
        return !!message.data && "Error" in message.data;
    }
    #startRefreshInterval() {
        if (this.#refreshIntervalId) {
            logger.debug("Refresh interval already exists, skipping creation of new interval.");
            return;
        }
        {
            this.#refreshIntervalId = setInterval(() => {
                this.#sendMessageToCommsChannel("getConnection");
                logger.debug('Refreshing WebRTC credentials');
            }, GET_CONNECTION_REFRESH_INTERVAL);
        }
    }
}
//# sourceMappingURL=comms-proxy.js.map