"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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 MediaStream_1 = __importDefault(require("./MediaStream"));
const MediaStreamError_1 = __importDefault(require("./MediaStreamError"));
const Permissions_1 = __importDefault(require("./Permissions"));
const RTCUtil = __importStar(require("./RTCUtil"));
const { WebRTCModule } = react_native_1.NativeModules;
function getUserMedia(constraints = {}) {
    // According to
    // https://www.w3.org/TR/mediacapture-streams/#dom-mediadevices-getusermedia,
    // the constraints argument is a dictionary of type MediaStreamConstraints.
    if (typeof constraints !== 'object') {
        return Promise.reject(new TypeError('constraints is not a dictionary'));
    }
    if ((typeof constraints.audio === 'undefined' || !constraints.audio) &&
        (typeof constraints.video === 'undefined' || !constraints.video)) {
        return Promise.reject(new TypeError('audio and/or video is required'));
    }
    // Normalize constraints.
    constraints = RTCUtil.normalizeConstraints(constraints);
    // Request required permissions
    const reqPermissions = [];
    if (constraints.audio) {
        reqPermissions.push(Permissions_1.default.request({ name: 'microphone' }));
    }
    else {
        reqPermissions.push(Promise.resolve(false));
    }
    if (constraints.video) {
        reqPermissions.push(Permissions_1.default.request({ name: 'camera' }));
    }
    else {
        reqPermissions.push(Promise.resolve(false));
    }
    return new Promise((resolve, reject) => {
        Promise.all(reqPermissions).then(results => {
            const [audioPerm, videoPerm] = results;
            // Check permission results and remove unneeded permissions.
            if (!audioPerm && !videoPerm) {
                // https://www.w3.org/TR/mediacapture-streams/#dom-mediadevices-getusermedia
                // step 4
                const error = {
                    message: 'Permission denied.',
                    name: 'SecurityError'
                };
                reject(new MediaStreamError_1.default(error));
                return;
            }
            audioPerm || delete constraints.audio;
            videoPerm || delete constraints.video;
            const success = (id, tracks) => {
                // Store initial constraints.
                for (const trackInfo of tracks) {
                    const c = constraints[trackInfo.kind];
                    if (typeof c === 'object') {
                        trackInfo.constraints = RTCUtil.deepClone(c);
                    }
                }
                const info = {
                    streamId: id,
                    streamReactTag: id,
                    tracks
                };
                resolve(new MediaStream_1.default(info));
            };
            const failure = (type, message) => {
                let error;
                switch (type) {
                    case 'TypeError':
                        error = new TypeError(message);
                        break;
                }
                if (!error) {
                    error = new MediaStreamError_1.default({ message, name: type });
                }
                reject(error);
            };
            WebRTCModule.getUserMedia(constraints, success, failure);
        });
    });
}
exports.default = getUserMedia;
//# sourceMappingURL=getUserMedia.js.map