"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const event_target_shim_1 = require("event-target-shim");
const react_native_1 = require("react-native");
const EventEmitter_1 = require("./EventEmitter");
const Logger_1 = __importDefault(require("./Logger"));
const RTCUtil_1 = require("./RTCUtil");
const log = new Logger_1.default('pc');
const { WebRTCModule } = react_native_1.NativeModules;
const MEDIA_STREAM_TRACK_EVENTS = ['ended', 'mute', 'unmute'];
class MediaStreamTrack extends event_target_shim_1.defineCustomEventTarget(...MEDIA_STREAM_TRACK_EVENTS) {
    constructor(info) {
        super();
        this.label = '';
        this._constraints = info.constraints || {};
        this._enabled = info.enabled;
        this._settings = info.settings || {};
        this._muted = false;
        this._peerConnectionId = info.peerConnectionId;
        this._readyState = info.readyState;
        this.id = info.id;
        this.kind = info.kind;
        this.remote = info.remote;
        if (!this.remote) {
            this._registerEvents();
        }
    }
    get enabled() {
        return this._enabled;
    }
    set enabled(enabled) {
        if (enabled === this._enabled) {
            return;
        }
        WebRTCModule.mediaStreamTrackSetEnabled(this.id, !this._enabled);
        this._enabled = !this._enabled;
    }
    get muted() {
        return this._muted;
    }
    get readyState() {
        return this._readyState;
    }
    stop() {
        WebRTCModule.mediaStreamTrackSetEnabled(this.id, false);
        this._readyState = 'ended';
        // TODO: save some stopped flag?
    }
    /**
     * Private / custom API for switching the cameras on the fly, without the
     * need for adding / removing tracks or doing any SDP renegotiation.
     *
     * This is how the reference application (AppRTCMobile) implements camera
     * switching.
     */
    _switchCamera() {
        if (this.remote) {
            throw new Error('Not implemented for remote tracks');
        }
        if (this.kind !== 'video') {
            throw new Error('Only implemented for video tracks');
        }
        WebRTCModule.mediaStreamTrackSwitchCamera(this.id);
    }
    _setVideoEffect(name) {
        if (this.remote) {
            throw new Error('Not implemented for remote tracks');
        }
        if (this.kind !== 'video') {
            throw new Error('Only implemented for video tracks');
        }
        WebRTCModule.mediaStreamTrackSetVideoEffect(this.id, name);
    }
    /**
     * Internal function which is used to set the muted state on remote tracks and
     * emit the mute / unmute event.
     *
     * @param muted Whether the track should be marked as muted / unmuted.
     */
    _setMutedInternal(muted) {
        if (!this.remote) {
            throw new Error('Track is not remote!');
        }
        this._muted = muted;
        this.dispatchEvent(new event_target_shim_1.Event(muted ? 'mute' : 'unmute'));
    }
    applyConstraints() {
        throw new Error('Not implemented.');
    }
    clone() {
        throw new Error('Not implemented.');
    }
    getCapabilities() {
        throw new Error('Not implemented.');
    }
    getConstraints() {
        return RTCUtil_1.deepClone(this._constraints);
    }
    getSettings() {
        return RTCUtil_1.deepClone(this._settings);
    }
    _registerEvents() {
        EventEmitter_1.addListener(this, 'mediaStreamTrackEnded', (ev) => {
            if (ev.trackId !== this.id || this._readyState === 'ended') {
                return;
            }
            log.debug(`${this.id} mediaStreamTrackEnded`);
            this._readyState = 'ended';
            // @ts-ignore
            this.dispatchEvent(new event_target_shim_1.Event('ended'));
        });
    }
    release() {
        EventEmitter_1.removeListener(this);
        WebRTCModule.mediaStreamTrackRelease(this.id);
    }
}
exports.default = MediaStreamTrack;
//# sourceMappingURL=MediaStreamTrack.js.map