// 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.
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
export const isWindows11 = () => __awaiter(void 0, void 0, void 0, function* () {
    if (!navigator.userAgentData) {
        console.warn('unable to determine if windows11');
        return;
    }
    if (navigator.userAgentData.platform === "Windows") {
        const userAgentData = yield 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;
    }
});
export const doBasicWebrtcSession = (iceServers) => __awaiter(void 0, void 0, void 0, function* () {
    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 = yield pc1.createOffer();
    pc1.setLocalDescription(offer);
    pc2.setRemoteDescription(offer);
    const answer = yield pc2.createAnswer();
    pc2.setLocalDescription(answer);
    pc1.setRemoteDescription(answer);
});
export function setupWebrtcForWindows11(iceServers) {
    return __awaiter(this, void 0, void 0, function* () {
        const isWindows11Value = yield isWindows11();
        // assume windows11 if indeterminate
        if (isWindows11Value === true || typeof isWindows11Value === 'undefined') {
            yield doBasicWebrtcSession(iceServers);
        }
    });
}
//# sourceMappingURL=windows11-first-session-hack.js.map