/* eslint-env es6 */

import _ from 'lodash'
import path from 'path'
import webpack from 'webpack'
import autoprefixer from 'autoprefixer'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import ExtractTextPlugin from 'extract-text-webpack-plugin'
import IndexBuilder from './webpack-plugins/IndexBuilder'
import S3Plugin from 'webpack-s3-plugin'
import {vendor} from './vendors.json'

const CONTEXT = path.resolve(__dirname),
      DEV_SERVER_PORT = 4000,
      APP_ROOT = path.resolve(CONTEXT, 'src'),
      PUBLIC_PATH = path.resolve(CONTEXT, 'public')

var devtool
var createPath = function(nPath) {
  return path.resolve(CONTEXT, nPath)
}

const TS_INGORES = [
  2403,
  2300,
  2374,
  2375,
  1005
]

const {NODE_ENV, AWS_ACCESS_KEY, AWS_SECRET_KEY, AWS_BUCKET} = process.env,
      CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin,
      BUILD_PATH = createPath('build'),
      SASS_LOADER = `${IS_BUILD ? 'postcss!' : ''}sass?sourceMap`

const ENV = {
  __DEV__: NODE_ENV === 'development',
  __PROD__: NODE_ENV === 'production',
  __TEST__: NODE_ENV === 'test',
  __STAGING__: NODE_ENV === 'staging'
}

const IS_BUILD = ENV.__STAGING__ || ENV.__PROD__

var preLoaders = {
  tslint: {
    test: /\.ts/,
    loader: 'tslint',
    exclude: [createPath('node_modules')],
    include: [createPath('src')]
  }
}

var loaders = {
  javascript: {
    test: /\.ts/,
    loader: `babel!ts?${TS_INGORES.map(num => `ignoreDiagnostics[]=${num}`).join('&')}`,
    exclude: [createPath('node_modules')],
    include: [createPath('src')]
  },

  html: {
    test: /\.jade/,
    loader: 'jade',
    include: [createPath('src')]
  },

  globalCss: {
    test: /\.s?css/,
    loader: `style!css?sourceMap!${SASS_LOADER}`,
    include: [createPath('src/style')]
  },

  // For to-string removes the ability to cache css so we use raw in development
  componentCss: {
    test: /\.s?css/,
    loader: `${IS_BUILD ? 'to-string' : 'raw'}!${SASS_LOADER}`,
    exclude: [createPath('src/style')]
  },

  json: {
    test: /\.json/,
    loader: 'json'
  },

  file: {
    test: /\.(png|gif|jpg|jpeg)$/,
    loader: `file${IS_BUILD ? '?name=[hash].[ext]' : ''}!image-webpack?bypassOnDebug`,
    include: [createPath('public/img')]
  },

  svg: {
    test: /\.svg/,
    loader: 'image-webpack?bypassOnDebug!svg-inline',
    include: [createPath('public/svg')]
  }
}

if (ENV.__PROD__)
  devtool = false
else if (ENV.__DEV__)
  devtool = 'source-map'
else if (ENV.__STAGING__ || ENV.__TEST__)
  devtool = 'inline-source-map'

var config = {
  context: CONTEXT,
  devtool,
  debug: !ENV.__PROD__ && !ENV.__STAGING__,

  entry: {
    vendor,
    app: './src/boot.ts'
  },

  output: {
    path: BUILD_PATH,
    filename: IS_BUILD ? '[name]-[hash].js' : '[name].js',
    sourceMapFilename: '[name].map',
    chunkFilename: '[id].chunk.js'
  },

  resolve: {
    extensions: ['', '.ts', '.js', '.json'],
    root: [APP_ROOT, PUBLIC_PATH],
    alias: {
      vendor: createPath('vendor')
    }
  },

  module: {
    noParse: [/.+zone\.js\/dist\/.+/, /.+angular2\/bundles\/.+/],
    preLoaders: Object.values(preLoaders),
    loaders: Object.values(loaders)
  },

  plugins: [
    new webpack.DefinePlugin(ENV),
    new HtmlWebpackPlugin({
      templateContent: IndexBuilder(APP_ROOT),
      favicon: path.resolve(__dirname, 'favicon.ico')
    })
  ],

  tslint: {
    emitErrors: false,
    failOnHint: false
  },

  postcss() {
    return [autoprefixer]
  },

  devServer: {
    port: DEV_SERVER_PORT,
    // Sample Proxy Config
    //proxy: [{
      //path: '/api/*',
      //target: 'http://localhost:4000'
    //}],

    historyApiFallback: {
      // If you have multiple entrypoints add them here
      rewrites: [{
        from: /.*/,
        to: '/index.html'
      }]
    },

    watchOptions: {
      aggregateTimeout: 300
    }
  }
}

if (!ENV.__TEST__)
  config.plugins.push(
    new CommonsChunkPlugin({
      name: 'vendor',
      filename: IS_BUILD ? 'vendor-[chunkhash].js' : 'vendor.js',
      minChunks: Infinity
    }),
    new CommonsChunkPlugin({
      name: 'common',
      filename: IS_BUILD ? 'common-[hash].js' : 'common.js',
      minChunks: 2,
      chunks: ['app', 'vendor']
    })
  )

if (ENV.__DEV__) {
  var WebpackNotifierPlugin = require('webpack-notifier')

  config.plugins.push(new WebpackNotifierPlugin({
    title: 'Angular2 App',
    contentImage: createPath('./favicon.ico')
  }))
} else if (IS_BUILD) {
  loaders.globalCss.loader = ExtractTextPlugin.extract('style', loaders.globalCss.loader.replace('style', ''))

  config.plugins.push(
    new webpack.optimize.OccurenceOrderPlugin(),
    new ExtractTextPlugin('[name]-[chunkhash].css'),
    new webpack.optimize.LimitChunkCountPlugin({maxChunks: 15}),
    new webpack.optimize.MinChunkSizePlugin({minChunkSize: 10000}),
    new webpack.optimize.UglifyJsPlugin()
  )
} else if (ENV.__TEST__) {
  config.resolve.cache = false
  config.stats = {
    colors: true,
    reasons: true
  }

  config.node = _.extend(config.node, {
    global: 'window',
    progress: false,
    crypto: 'empty',
    module: false,
    clearImmediate: false,
    setImmediate: false
  })

  config.module.noParse = [
    /zone\.js\/dist\/zone-microtask\.js/,
    /zone\.js\/dist\/long-stack-trace-zone\.js/,
    /zone\.js\/dist\/jasmine-patch\.js/
  ]
}

if (!ENV.__PROD__)
  vendor.push('zone.js/dist/long-stack-trace-zone')
else
  config.plugins.push(
    new S3Plugin({
      exclude: /.*\.html$/,
      s3Options: {
        accessKeyId: AWS_ACCESS_KEY,
        secretAccessKey: AWS_SECRET_KEY
      },
      s3UploadOptions: {
        Bucket: AWS_BUCKET,
        CacheControl: 'max-age=315360000, no-transform, public'
      },
      cdnizerOptions: {
        defaultCDNBase: `https://s3-us-west-2.amazonaws.com/${AWS_BUCKET}`
      }
    }),
    new S3Plugin({
      directory: PUBLIC_PATH,
      basePath: 'public/',
      s3Options: {
        accessKeyId: AWS_ACCESS_KEY,
        secretAccessKey: AWS_SECRET_KEY
      },
      s3UploadOptions: {
        Bucket: AWS_BUCKET,
        CacheControl: 'max-age=315360000, no-transform, public'
      },
      cdnizerOptions: {
        defaultCDNBase: `https://s3-us-west-2.amazonaws.com/${AWS_BUCKET}`
      }
    })
  )

module.exports = config

@@ -5874,16 +5874,41 @@
 blic/',%0A
+      exclude: /%5C.svg$/,%0A
       s3

exclude uploading svg cause they will bake in