var DocsPlugin = require('docs-webpack-plugin'),
    path = require('path');

module.exports = function(config) {
    config.set({
        basePath: '',
        files: [
            '../../node_modules/babel-polyfill/dist/polyfill.js',
            '../index.js'
        ],

        preprocessors: {
            '../index.js': [ 'webpack', 'sourcemap' ]
        },

        frameworks: [ 'phantomjs-shim', 'mocha', 'chai', 'sinon' ],

        webpack: {
            resolve: {
                extensions: [ '', '.js', '.less', '.jsx' ],
                alias: {
                    'atoms': path.resolve(__dirname, '../../atoms'),
                    'components': path.resolve(__dirname, '../../components'),
                    'sinon': 'sinon/pkg/sinon',
                    'web-foundation': path.resolve(__dirname, '..', '..')
                }
            },
            isparta: {
                embedSource: true,
                noAutoWrap: true,
                babel: {
                    presets: ['es2015', 'react', 'stage-0']
                }
            },
            externals: {
                'jsdom': 'window',
                'cheerio': 'window',
                'react/addons': true,
                'react/lib/ExecutionEnvironment': true,
                'react/lib/ReactContext': 'window',
                'text-encoding': 'window'
            },
            devtool: 'inline-source-map',
            node: {
                fs: 'empty'
            },
            module: {
                noParse: [
                    /node_modules\/sinon\//,
                ],
                preLoaders: [ {
                    test: /\.(js|jsx)$/,
                    exclude: ['node_modules/**/*', '!node_modules/dd/**.*'],
                    loader: 'babel-loader',
                    query: {
                        compact: false,
                        presets: ['es2015', 'react', 'stage-0'],
                        plugins: ['transform-object-rest-spread']
                    }
                }, {
                    test: /\.(js|jsx)$/,
                    loader: 'isparta',
                    exclude: /(shims|tests|node_modules)\//
                } ],
                loaders: [ {
                    test: /\.less$/,
                    loader: "style-loader!css-loader!less-loader?{\"globalVars\":{\"theme\":\"buyer\"},\"sourceMap\":true}"
                }, {
                    test: /\.(jpe?g|png|gif|svg)$/i,
                    loaders: [
                        'file?name=/static/img/' + config.project + '/[hash:8].[ext]',
                        'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false'
                    ]
                }, {
                    test: /\.(eot|otf|svg|ttf|woff(2)|woff)$/i,
                    loader: 'file?name=static/fonts/[hash:8].[ext]',
                }, {
                    test: /\.(json)$/i,
                    loader: 'json'
                } ]
            }
            // plugins: [
            //     new DocsPlugin({
            //         file: path.resolve(__dirname, 'docs.json')
            //     })
            // ]
        },

        webpackMiddleware: {
            stats: 'minimal',
            noInfo: true
        },

        mochaReporter: {
            showDiff: true
        },

        coverageReporter: {
            type: 'html',
            dir: '../coverage/',

            check: {
                each: {
                    statements: 94,
                    branches: 91,
                    functions: 96,
                    lines: 96
                }
            }
        },

        client: {
            captureConsole: true
        },

        reporters: [ 'coverage', 'mocha' ],
        port: 9876,
        colors: true,
        logLevel: config.LOG_ERROR,
        autoWatch: true,
        browsers: [ 'PhantomJS' ],
        singleRun: true
    });
};
