"use strict";
// Hopefully this file is just temporary. Windows 11 currently has an issue where ice stop responding on the first
// webrtc session after a system reboot. The purpose of this file is to force a webrtc connection upon bootstrap
// in order to work around the windows 11 issue.
Object.defineProperty(exports, "__esModule", { value: true });
exports.doBasicWebrtcSession = exports.isWindows11 = void 0;
exports.setupWebrtcForWindows11 = setupWebrtcForWindows11;
const isWindows11 = async () => {
    if (!navigator.userAgentData) {
        console.warn('unable to determine if windows11');
        return;
    }
    if (navigator.userAgentData.platform === "Windows") {
        const userAgentData = await navigator.userAgentData.getHighEntropyValues(["platformVersion"]);
        // in the genesys cloud desktop app, platformVersion is just an empty string
        if (!userAgentData.platformVersion) {
            return;
        }
        const majorPlatformVersion = parseInt(userAgentData.platformVersion.split('.')[0]);
        if (majorPlatformVersion >= 13) {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        return false;
    }
};
exports.isWindows11 = isWindows11;
const doBasicWebrtcSession = async (iceServers) => {
    console.info('running windows11 webrtc hack');
    const pc1 = new RTCPeerConnection({ iceServers });
    const pc2 = new RTCPeerConnection({ iceServers });
    pc1.onicecandidate = (ev) => {
        if (ev.candidate) {
            pc2.addIceCandidate(ev.candidate);
        }
    };
    pc2.onicecandidate = (ev) => {
        if (ev.candidate) {
            pc1.addIceCandidate(ev.candidate);
        }
    };
    pc1.onconnectionstatechange = () => {
        const state = pc1.connectionState;
        if (['connected', 'disconnected', 'failed', 'closed'].includes(state)) {
            pc1.close();
            pc2.close();
        }
    };
    pc1.addTransceiver('audio');
    const offer = await pc1.createOffer();
    pc1.setLocalDescription(offer);
    pc2.setRemoteDescription(offer);
    const answer = await pc2.createAnswer();
    pc2.setLocalDescription(answer);
    pc1.setRemoteDescription(answer);
};
exports.doBasicWebrtcSession = doBasicWebrtcSession;
async function setupWebrtcForWindows11(iceServers) {
    const isWindows11Value = await (0, exports.isWindows11)();
    // assume windows11 if indeterminate
    if (isWindows11Value === true || typeof isWindows11Value === 'undefined') {
        await (0, exports.doBasicWebrtcSession)(iceServers);
    }
}
//# sourceMappingURL=windows11-first-session-hack.js.map