Source: lib/detect.js

var Promise = require('bluebird/js/release/bluebird');
var DetectRTC = require('detectrtc');

/**
 * Detection module
 * @class
 * @classdesc Detect
 * @return {Promise<ListDetections>}
 * @example
 * usecases.detect()
 *     .then((detections) => {
 *         console.log(detections.detectBrowserCompatibility); // true
 *     })
 */
var detect = function() {

    /**
     * Returns a detailed DetectRTC object with local browser features
     * @return {object} DetectRTC object
     * @example
     * detections.getDetectionInfo();
     */
    var getDetectionInfo = function() {
        return DetectRTC;
    };

    /**
     * Detects if the browser is or not compatible with VideoRTC.js
     * @param  {object} browsers
     * @return {boolean}
     * @example
     * detections.detectBrowserCompatibility({chrome: true, firefox: true, opera: false, safari: false});
     */
    var detectBrowserCompatibility = function(obj) {
        var browsers = obj || { chrome: true, firefox: true, opera: false, safari: false };
        if((!DetectRTC.isWebRTCSupported)) return false;
        else if(DetectRTC.browser.isChrome && browsers.chrome === true) return true;
        else if(DetectRTC.browser.isFirefox && browsers.firefox === true) return true;
        else if(DetectRTC.browser.isOpera && browsers.opera === true) return true;
        else if(DetectRTC.browser.isSafari && browsers.safari === true) return true;
        else return false;
    };

    /**
     * Detects information about the local cameras connected
     * @return {object} {hasCamera: Boolean, videoDevices: Array, domainCameraPermissions: Boolean}
     * @example
     * detections.detectCamera(); // {hasCamera: true, videoDevices: [], domainCameraPermissions: true}
     */
    var detectCamera = function() {
        return {
            hasCamera: DetectRTC.hasWebcam,
            videoDevices: DetectRTC.videoInputDevices,
            domainCameraPermissions: DetectRTC.isWebsiteHasWebcamPermissions
        };
    };

    /**
     * Detects information about the local microphones connected
     * @return {object} {hasMicrophone: Boolean, audioInputDevices: Array, domainMicrophonePermissions: Boolean}
     * @example
     * detections.detectMicrophone(); // {hasMicrophone: true, audioInputDevices: [], domainMicrophonePermissions: true}
     */
    var detectMicrophone = function() {
        return {
            hasMicrophone: DetectRTC.hasMicrophone,
            audioInputDevices: DetectRTC.audioInputDevices,
            domainMicrophonePermissions: DetectRTC.isWebsiteHasMicrophonePermissions
        };
    };

    /**
     * Detects information about the speakers connected
     * @return {object} {hasSpeakers: Boolean, audioOutputDevices: Array}
     * @example
     * detections.detectSpeakers(); // {hasSpeakers: true, audioOutputDevices: []}
     */
    var detectSpeakers = function() {
        return {
            hasSpeakers: DetectRTC.hasSpeakers,
            audioOutputDevices: DetectRTC.audioOutputDevices
        };
    };

    return new Promise(function (resolve) {
        DetectRTC.load(function() {
            resolve({
                getDetectionInfo: getDetectionInfo,
                detectBrowserCompatibility: detectBrowserCompatibility,
                detectCamera: detectCamera,
                detectMicrophone: detectMicrophone,
                detectSpeakers: detectSpeakers
            })
        });
    });

};

exports.detect = detect;