"use strict";

module.exports = (config) => {

    const options = {
        frameworks: ["polyfill", "mocha"],
        reporters: ["coverage-istanbul"],
        files: ["test/index.js"],
        preprocessors: {
            "test/index.js": ["webpack", "sourcemap"]
        },
        webpack: require("./webpack.config"),
        beforeMiddleware: ["webpackBlocker"]
    };

    if (!process.env.CIRCLECI) {
        options.reporters.push("progress");
    } else {
        options.singleRun = true;
        options.logLevel = config.LOG_ERROR;
        options.reporters.push("junit", "BrowserStack", "saucelabs");
        options.junitReporter = {
            outputDir: process.env.CIRCLE_TEST_REPORTS + "/junit/"
        };
        options.concurrency = 1;
        options.browserNoActivityTimeout = 30000;
        options.browserDisconnectTolerance = 3;
        
        options.browserStack = {
            project: process.env.CIRCLE_PROJECT_REPONAME + "_" + process.env.CIRCLE_BRANCH
        };
        
        let customLaunchers = {};

        switch (process.env.CIRCLE_NODE_INDEX) {
            case "0":
                // Test on BrowserStack
                const bsCaps = require("browserstack-capabilities")(process.env.BROWSER_STACK_USERNAME, process.env.BROWSER_STACK_ACCESS_KEY);
                const capabilities = bsCaps.create([{
                    "browser": ["chrome", "firefox", "ie", "opera", "edge"],
                    "browser_version": "latest",
                    "os": "Windows",
                    "os_version": ["10", "7"]
                }, {
                    "browser": ["chrome", "firefox", "opera", "safari"],
                    "browser_version": "latest",
                    "os": "OS X",
                    "os_version": ["Sierra", "El Capitan"]
                }]).filter((capability) => capability);
                const browsers = [];
                capabilities.forEach((capability) => {
                    const browser = [
                        "os",
                        "os_version",
                        "browser",
                        "browser_version"
                    ].map((key) => capability[key]).join(" ");
                    browsers.push(browser);
                    capability.base = "BrowserStack";
                    customLaunchers[browser] = capability;
                });
                options.customLaunchers = customLaunchers;
                options.browsers = browsers;
                break;
            case "1":
                // Test on SauceLabs
                customLaunchers = {
                    sl_chrome: {
                        base: 'SauceLabs',
                        browserName: 'chrome',
                        platform: 'Windows 7',
                        version: '35'
                    },
                    sl_firefox: {
                        base: 'SauceLabs',
                        browserName: 'firefox',
                        version: '30'
                    },
                    sl_ios_safari: {
                        base: 'SauceLabs',
                        browserName: 'iphone',
                        platform: 'OS X 10.9',
                        version: '7.1'
                    },
                    sl_ie_11: {
                        base: 'SauceLabs',
                        browserName: 'internet explorer',
                        platform: 'Windows 8.1',
                        version: '11'
                    }
                };
                options.customLaunchers = customLaunchers;
                options.browsers = Object.keys(customLaunchers);
                break;
            case "2":
                break;
            case "3":
                break;
            default:
        }
    }

    config.set(options);
};

@@ -2612,10 +2612,9 @@
   sl
-_c
+C
 hrom
@@ -2640,33 +2640,33 @@
           base: 
-'
+%22
 SauceLabs',%0A    
@@ -2650,33 +2650,33 @@
 base: %22SauceLabs
-'
+%22
 ,%0A              
@@ -2702,16 +2702,16 @@
 me: 
-'
+%22
 chrome
-'
+%22
 ,%0A  
@@ -2734,33 +2734,33 @@
       platform: 
-'
+%22
 Windows 7',%0A    
@@ -2752,17 +2752,17 @@
 indows 7
-'
+%22
 ,%0A      
@@ -2792,12 +2792,12 @@
 on: 
-'35'
+%2235%22
 %0A   
@@ -2842,10 +2842,9 @@
   sl
-_f
+F
 iref
@@ -2871,33 +2871,33 @@
           base: 
-'
+%22
 SauceLabs',%0A    
@@ -2881,33 +2881,33 @@
 base: %22SauceLabs
-'
+%22
 ,%0A              
@@ -2933,17 +2933,17 @@
 me: 
-'
+%22
 firefox
-'
+%22
 ,%0A  
@@ -2977,12 +2977,12 @@
 on: 
-'30'
+%2230%22
 %0A   
@@ -3027,14 +3027,9 @@
   sl
-_ios_s
+S
 afar
@@ -3055,33 +3055,33 @@
           base: 
-'
+%22
 SauceLabs',%0A    
@@ -3065,33 +3065,33 @@
 base: %22SauceLabs
-'
+%22
 ,%0A              
@@ -3117,16 +3117,16 @@
 me: 
-'iphone'
+%22safari%22
 ,%0A  
@@ -3161,19 +3161,21 @@
 rm: 
-'
+%22mac
 OS 
-X 
 10.
-9'
+12%22
 ,%0A  
@@ -3209,13 +3209,14 @@
 on: 
-'7.1'
+%2210.0%22
 %0A   
@@ -3261,12 +3261,10 @@
   sl
-_ie_
+IE
 11: 
@@ -3295,17 +3295,17 @@
   base: 
-'
+%22
 SauceLab
@@ -3305,17 +3305,17 @@
 auceLabs
-'
+%22
 ,%0A      
@@ -3345,17 +3345,17 @@
 erName: 
-'
+%22
 internet
@@ -3363,17 +3363,17 @@
 explorer
-'
+%22
 ,%0A      
@@ -3400,17 +3400,17 @@
 atform: 
-'
+%22
 Windows 
@@ -3412,17 +3412,17 @@
 dows 8.1
-'
+%22
 ,%0A      
@@ -3452,12 +3452,12 @@
 on: 
-'11'
+%2211%22
 %0A   

Improve codestyle.