"use strict";
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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_native_1 = require("react-native");
const RTCRtpCapabilities_1 = require("./RTCRtpCapabilities");
const RTCRtpSendParameters_1 = __importDefault(require("./RTCRtpSendParameters"));
const { WebRTCModule } = react_native_1.NativeModules;
class RTCRtpSender {
    constructor(info) {
        this._track = null;
        this._peerConnectionId = info.peerConnectionId;
        this._id = info.id;
        this._rtpParameters = new RTCRtpSendParameters_1.default(info.rtpParameters);
        if (info.track) {
            this._track = info.track;
        }
    }
    replaceTrack(track) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                yield WebRTCModule.senderReplaceTrack(this._peerConnectionId, this._id, track ? track.id : null);
            }
            catch (e) {
                return;
            }
            this._track = track;
        });
    }
    static getCapabilities(kind) {
        if (kind === 'audio') {
            return RTCRtpCapabilities_1.DEFAULT_AUDIO_CAPABILITIES;
        }
        if (!RTCRtpCapabilities_1.senderCapabilities) {
            throw new Error('sender Capabilities are null');
        }
        return RTCRtpCapabilities_1.senderCapabilities;
    }
    getParameters() {
        return this._rtpParameters;
    }
    setParameters(parameters) {
        return __awaiter(this, void 0, void 0, function* () {
            // This allows us to get rid of private "underscore properties"
            const _params = JSON.parse(JSON.stringify(parameters));
            const newParameters = yield WebRTCModule.senderSetParameters(this._peerConnectionId, this._id, _params);
            this._rtpParameters = new RTCRtpSendParameters_1.default(newParameters);
        });
    }
    getStats() {
        return WebRTCModule.senderGetStats(this._peerConnectionId, this._id).then(data => 
        /* On both Android and iOS it is faster to construct a single
        JSON string representing the Map of StatsReports and have it
        pass through the React Native bridge rather than the Map of
        StatsReports. While the implementations do try to be faster in
        general, the stress is on being faster to pass through the React
        Native bridge which is a bottleneck that tends to be visible in
        the UI when there is congestion involving UI-related passing.
        */
        new Map(JSON.parse(data)));
    }
    get track() {
        return this._track;
    }
    get id() {
        return this._id;
    }
}
exports.default = RTCRtpSender;
//# sourceMappingURL=RTCRtpSender.js.map