"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeListener = exports.addListener = exports.setupNativeEvents = void 0;
const react_native_1 = require("react-native");
// @ts-ignore
const EventEmitter_1 = __importDefault(require("react-native/Libraries/vendor/emitter/EventEmitter"));
const { WebRTCModule } = react_native_1.NativeModules;
// This emitter is going to be used to listen to all the native events (once) and then
// re-emit them on a JS-only emitter.
const nativeEmitter = new react_native_1.NativeEventEmitter(WebRTCModule);
const NATIVE_EVENTS = [
    'peerConnectionSignalingStateChanged',
    'peerConnectionStateChanged',
    'peerConnectionOnRenegotiationNeeded',
    'peerConnectionIceConnectionChanged',
    'peerConnectionIceGatheringChanged',
    'peerConnectionGotICECandidate',
    'peerConnectionDidOpenDataChannel',
    'peerConnectionOnRemoveTrack',
    'peerConnectionOnTrack',
    'dataChannelStateChanged',
    'dataChannelReceiveMessage',
    'dataChannelDidChangeBufferedAmount',
    'mediaStreamTrackMuteChanged',
    'mediaStreamTrackEnded',
];
const eventEmitter = new EventEmitter_1.default();
function setupNativeEvents() {
    for (const eventName of NATIVE_EVENTS) {
        nativeEmitter.addListener(eventName, (...args) => {
            eventEmitter.emit(eventName, ...args);
        });
    }
}
exports.setupNativeEvents = setupNativeEvents;
const _subscriptions = new Map();
function addListener(listener, eventName, eventHandler) {
    var _a;
    if (!NATIVE_EVENTS.includes(eventName)) {
        throw new Error(`Invalid event: ${eventName}`);
    }
    if (!_subscriptions.has(listener)) {
        _subscriptions.set(listener, []);
    }
    (_a = _subscriptions.get(listener)) === null || _a === void 0 ? void 0 : _a.push(eventEmitter.addListener(eventName, eventHandler));
}
exports.addListener = addListener;
function removeListener(listener) {
    var _a;
    (_a = _subscriptions.get(listener)) === null || _a === void 0 ? void 0 : _a.forEach(sub => {
        sub.remove();
    });
    _subscriptions.delete(listener);
}
exports.removeListener = removeListener;
//# sourceMappingURL=EventEmitter.js.map